dougan7657 2010-09-16 10:13
浏览 40
已采纳

使用php和'if changed'选项创建xml

I am creating an address book xml feed from a MySQL database, everything is working fine, but I have a section tag which gets the first letter of the surname and pops it in that tag. I only want this to display if it has changed, but for some reason my brain isn't working this morning!

Current code:

<?php

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
echo "<data>";

do {

    $char = $row_fetch["surname_add"];
    $section = $char[0];

    //if(changed???){
        echo "<section><character>".$section."</character>";
    //}
    echo "<person>";
    echo "<name>".$row_fetch["firstname_add"]." ".$row_fetch["surname_add"]."</name>";
    echo "<title>".$row_fetch["title_add"]."</title>";
    echo "</person>";

    //if(){
        echo "</section>";
    //}


} while ($row_fetch = mysql_fetch_assoc($fetch));

echo "</data>";

?>

Any help on this welcome, don't know why I can't think of it!

  • 写回答

2条回答 默认 最新

  • dsu5188 2010-09-16 10:27
    关注

    And if you still want to generate XML manually, I suppose, something like this will work:

    $section = "NoSectionStartedYet";
    while ($row_fetch = mysql_fetch_assoc($fetch)) {
    
        $char = $row_fetch["surname_add"];
        if ($char[0] != $section)
        {
            if ($section != "NoSectionStartedYet")
            {
                echo "</section>";
            }
            $section = $char[0];
            echo "<section><character>".$section."</character>";
        }
    
        echo "<person>";
        echo "<name>".$row_fetch["firstname_add"]." ".$row_fetch["surname_add"]."</name>";
        echo "<title>".$row_fetch["title_add"]."</title>";
        echo "</person>";
    
    }
    echo "</section>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用