duandou9931 2014-12-02 22:08
浏览 39
已采纳

如何使用带有条件嵌套的PHP和MYSQL创建XML

I am attempting to make a TV Guide by using a database to create the XML file via MYSQL and PHP. I have had good success so far and have managed to output the various channels for each day.

The problem is when I try to bring the various programmes in. Within the database, each programme is assigned a day and channel it will be shown on. However, the XML being generated is placing every programme on every channel for every day.

Here is what I have so far:

$xml = new DOMDocument("1.0", "UTF-8");
$xml->formatOutput = TRUE;

$day_query = "SELECT day_name FROM day;";

$dq_result = mysql_query($day_query) or die(mysql_error());

$tvguide = $xml->createElement("guide");
$tvguide = $xml->appendChild($tvguide);

    if($dq_result)
    while($dayrow = mysql_fetch_row($dq_result)){
        $day = $xml->createElement("day");
        $day->setAttribute("name", $dayrow[0]);
        $day = $tvguide->appendChild($day);

        $channel_query = "SELECT channel_ID, channel_Name FROM channel ORDER BY channel_Name ASC;";

        $cq_result = mysql_query($channel_query) or die(mysql_error());

        if($cq_result)
            while($channelrow = mysql_fetch_row($cq_result)){

            $channel = $xml->createElement("channel");
            $channel->setAttribute("id", $channelrow[0]);
            $channel = $day->appendChild($channel);

            $channel_name = $xml->createElement("channel_name");
            $channelNameText = $xml->createTextnode($channelrow[1]);
            $channel_name->appendChild($channelNameText);
            $channel_name = $channel->appendChild($channel_name);


            $show_query = "SELECT programme_ID, programme_Name, start_Time, duration, description, day_Name, channel_Name FROM programme ORDER BY start_Time ASC;";

            $sq_result = mysql_query($show_query) or die(mysql_error());

            if($sq_result)
                while($showrow = mysql_fetch_row($sq_result)){


            $show = $xml->createElement("show");
            $show = $channel->appendChild($show);

            $show_name = $xml->createElement('show_name');
            $show_name->setAttribute("id", $showrow[0]);
            $showNameText = $xml->createTextnode($showrow[1]);
            $show_name->appendChild($showNameText);
            $show->appendChild($show_name);



            }

        }
}

Basically, I want to know how I can nest all the shows whose day_Name is equal to Monday, for example, within the 'Monday' XML tag. Thank you.

  • 写回答

1条回答 默认 最新

  • douwen2158 2014-12-02 23:00
    关注

    OK, to answer your question quickly, it be as easy as changing:

    $show_query = "SELECT programme_ID, programme_Name, start_Time, duration, description, day_Name, channel_Name FROM programme ORDER BY start_Time ASC;";

    Into

    $show_query = "SELECT programme_ID, programme_Name, start_Time, duration, description, day_Name, channel_Name FROM programme WHERE day_Name='$dayrow[0]' AND channel_Name='$channelNameText' ORDER BY start_Time ASC;";

    However, there are a couple more things to change:

    1. You shouldn't use the mysql_ interface, use either mysqli or PDO_mysql.
    2. You are doing inside the while a query (SELECT channel_ID, channel_Name FROM channel ORDER BY channel_Name ASC;) which is constant. You probably should make it only once, before the while, and cache its results.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧