PHP XML Output from mySQL Database -
i pulling information sql database , forming xml via php. attempting make xml format this:
<channel> <channel_name>asn</channel_name> <program id="1"> <title>the simpson</title> <info>melfi considers turning homer help...</info> <season>s01</season> <episode>e09</episode> ... <channel>asn</channel> </program> </channel> <channel> <channel_name>nbc</channel_name> <program id="2"> <title>the simpson</title> <info>melfi considers turning homer help...</info> <season>s02</season> <episode>e04</episode> ... <channel>nbc</channel> </program> </channel> ...etc
this php attempting mimic xml structure above:
<?php header('content-type: application/xml; charset=utf-8'); include_once "db_connect.php"; //sql query $program_sql = <<<sql select * programs sql; if ($result = $db->query($program_sql)) { /* fetch associative array */ while ($row = mysqli_fetch_assoc($result)) { //data #descriptions $program_title = $row['program_title']; $program_id = $row['program_id']; $program_info = $row['program_info']; #guides $program_season = $row['program_season']; $program_episode = $row['program_episode']; $program_genre = $row['program_genre']; #times $program_duration_hours = $row['program_duration_hours']; $program_duration_mins = $row['program_duration_mins']; $program_day = $row['program_day']; $program_time_start = $row['program_time_start']; $program_time_end = $row['program_time_end']; #assets $program_image_fullsize = $row['program_image_fullsize']; $program_image_thumbnail = $row['program_image_thumbnail']; #channel $channel .= $row['channel']; //xml output $output .= "<program id='".$program_id."'> <title>".$program_title."</title> <info>".$program_info."</info> <season>s".$program_season."</season> <episode>e".$program_episode."</episode> <duration> <hours>".$program_duration_hours."</hours> <minutes>".$program_duration_mins."</minutes> </duration> <day>".$program_day."</day> <time> <start>".$program_time_start."</start> <end>".$program_time_end."</end> </time> <genre>".$program_genre."</genre> <image> <fullsize>/images/program_images/".$program_image_fullsize."</fullsize> <thumbnail>/images/program_images/thumbnails/".$program_image_thumbnail."</thumbnail> </image> <channel>".$channel."</channel> </program>"; }//channel while loop }//if echo $output; mysqli_close($db); ?>
my php outputting program node; how go wrapping these in corresponding channel tags?
try this.
$rows = [ ['channel_name' => 'asn', 'program_id' => 1, 'program_title' => 'program 1'], ['channel_name' => 'asn', 'program_id' => 2, 'program_title' => 'program 2'], ['channel_name' => 'nbc', 'program_id' => 3, 'program_title' => 'program 3'], ]; // pre grouping pattern $grouped = array_reduce($rows, function($channels, $row) { if (!array_key_exists($row['channel_name'], $channels)) { $channels[$row['channel_name']] = []; } $channels[$row['channel_name']][] = $row; return $channels; }, []); foreach ($grouped $channelname => $program) { // build xml } // or simplexml pattern $root = new simplexmlelement('<root />'); foreach ($rows $row) { $channel = $root->xpath("/root/channel/channel_name[text()='{$row['channel_name']}']/parent::node()"); if (count($channel) === 0) { $channel = $root->addchild('channel'); $channel->addchild('channel_name', $row['channel_name']); } else { $channel = $channel[0]; } $program = $channel->addchild('program'); $program->addattribute('id', $row['program_id']); $program->addchild('title', $row['program_title']); } echo $root->asxml();
Comments
Post a Comment