dsadsadsa1231 2014-03-14 22:15
浏览 10
已采纳

使用php在解析的xml文件中创建动态URL

I'm new in php.
I'm trying to find a way to match the right url dynamically in every description.
For example in the case where the description = Xios i would like the url to become :
<li><div id="Left1"><a href="main.php?description=Xios">' . make_safe( $item['description'] ) . '</a></div></li>
Now in the case where the description = Kiklades i would like the url to become : <li><div id="Left1"><a href="main.php?description=Kiklades">' . make_safe( $item['description'] ) . '</a></div></li>
Endly in the case where the description = Mikonos i would like the url to become :
<li><div id="Left1"><a href="main.php?description=Mikonos">' . make_safe( $item['description'] ) . '</a></div></li>

foreach ($item_array as $item) {
            if ( make_safe($item['diamerisma']) == $_GET['diamerisma'] ) {
                if (!in_array($item['description'], $used_values) ) {//This will check if this value hasn't been listed yet
                    $html .= '<ul data-role="listview" id="weatherList" data-theme="b" data-insert="true" >';
                    $html .= '<li><div id="Left1"><a href="main.php?description=Κυκλάδες">' . make_safe( $item['description'] ) . '</a></div></li>';
                    $html .= '</ul>';
                    $html .= '</dd>';
                    $used_values[] = $item['description'];//here you preserve used values for avoiding repetition
                }
            }
        }

the xml code

    <item>
        <title>Xios center</title>
        <description>Xios</description>
        <diamerisma>Aigaio</diamerisma>
        <metar>1010</metar>
    </item>
    <item>
        <title>Siros</title>
        <description>Kiklades</description>
        <diamerisma>Aigaio</diamerisma>
        <metar>1011</metar>
    </item>
    <item>
        <title>Naxos center</title>
        <description>Kiklades</description>
        <diamerisma>Aigaio</diamerisma>
        <metar>1012</metar>
    </item>
    <item>
        <title>Mikonos center</title>
        <description>Mikonos</description>
        <diamerisma>Aigaio</diamerisma>
        <metar>1013</metar>
    </item>

Any help will be appreciated!thanks in advance guys!

  • 写回答

2条回答 默认 最新

  • dongmaomou4117 2014-03-15 09:57
    关注

    As I don't know how $item_array is created, I'll go back one step from your code and create $item_array in a different way:

    $xml = simplexml_load_string($x); // assume XML in $x
    $dia = "Aigaio"; // use $_GET... in your code
    $item_array = $xml->xpath("//item[diamerisma = '$dia']");
    

    Now, $item_array contains only <item>-nodes with the desired <diamerisma>-child. We do not have to check for <diamerisma> in the loop:

    foreach ($item_array as $item) {
    
        // simplified
        echo '<a href="main.php?description=' . $item->description . '">' . $item->description . '</a>';    
    
    }
    

    see it working: https://eval.in/120611

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况