Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mySQL数据库的PHP-XML输出_Php_Mysql_Xml - Fatal编程技术网

mySQL数据库的PHP-XML输出

mySQL数据库的PHP-XML输出,php,mysql,xml,Php,Mysql,Xml,我从一个SQL数据库中提取信息,并通过PHP将其形成XML。我正试图使我的XML格式如下所示: <channel> <channel_name>ASN</channel_name> <program id="1"> <title>The Simpson</title> <info>Melfi considers turning to Homer for help..

我从一个SQL数据库中提取信息,并通过PHP将其形成XML。我正试图使我的XML格式如下所示:

<channel>
    <channel_name>ASN</channel_name>
    <program id="1">
        <title>The Simpson</title>
        <info>Melfi considers turning to Homer for 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 to Homer for help...</info>
        <season>S02</season>
        <episode>E04</episode>
        ...
        <channel>NBC</channel>
    </program>
</channel>
...etc

ASN
辛普森
梅尔菲考虑求助于荷马。。。
S01
E09
...
ASN
全国广播公司
辛普森
梅尔菲考虑求助于荷马。。。
S02
E04
...
全国广播公司
等
这是我的PHP,它试图模仿上面的XML结构:

<?php
header('Content-Type: application/xml; charset=utf-8');
    include_once "db_connect.php";
    //SQL Query
    $program_sql = <<<SQL
    SELECT * FROM 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);
?>

我的PHP目前只输出程序节点;我如何将它们包装到相应的频道标签中?

试试这个

$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 as $channelName => $program) {
    // build up XML
}

// Or SimpleXML pattern
$root = new SimpleXMLElement('<root />');

foreach ($rows as $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();
$rows=[
[“频道名称”=>“ASN”,“节目id”=>1,“节目标题”=>“节目1”],
[“频道名称”=>“ASN”,“节目id”=>2,“节目标题”=>“节目2”],
[“频道名称”=>“NBC”,“节目id”=>3,“节目标题”=>“节目3”],
];
//预分组模式
$grouped=array\u reduce($rows,function($channels,$row){
如果(!array_key_存在($row['channel_name',$channels)){
$channels[$row['channel_name']]=[];
}
$channels[$row['channel\u name'][]=$row;
返回$channels;
}, []);
foreach($channelName=>$program){
//构建XML
}
//或SimpleXML模式
$root=新的SimpleXMLElement(“”);
foreach($行作为$行){
$channel=$root->xpath(“/root/channel/channel\u name[text()='{$row['channel\u name']}']/parent::node()”;
如果(计数($channel)==0){
$channel=$root->addChild('channel');
$channel->addChild('channel_name',$row['channel_name']);
}否则{
$channel=$channel[0];
}
$program=$channel->addChild('program');
$program->addAttribute('id',$row['program_id']);
$program->addChild('title',$row['program_title']);
}
echo$root->asXML();

频道标签有多个节目标签?我们不知道这些表里面是什么,但请尝试执行与您已经完成的相同的逻辑。获取值,然后为通道创建另一个值。您已经完成了程序,对吗?是的@alu有多个程序标记:(这太棒了,非常感谢您-只需一个快速查询,我将如何填充$rows(数组对吗?)来自数据库?抱歉,我对PHP有点生疏。再次感谢!示例是
$rows
。您可以在原始代码上使用while循环(foreach到while)。