drgmszo076956 2012-12-07 19:14
浏览 51
已采纳

如何从xml节点动态创建php页面,然后将索引页面链接到它们

I'm looking for a way to create a details page dynamically that correspond to an XML feed with PHP.

For example, say this is my xml feed:

<root>
   <person>
      <name>Jim</name>
      <desc>A hard working guy.</desc>
    </person>
    <person>
      <name>Tom</name>
      <desc>As lazy as an obese house cat.</desc>
    </person>
</root>

And say this was my php/html:

<?php
$dom = simplexml_load_file('people.xml');
?>

<ul>
<?php
for ($i = 0; $i < 10; $i++) {
   $name = $dom->person[$i]->name;
   $desc = $dom->person[$i]->desc;
$post = 
   '
    <li>
        <a href="details.php">'.$name.'</a>
        <p>'.$desc.'</p>
    </li>
    ';

 echo $post;
}
?>
</ul>

I would like to have that <a> tag that's being created link to a page that has the content of the corresponding person from the XML.

So I guess my two questions would be, how do I dynamically create a page with xml data, and how do I set the href attribute in the code above to link to it, in php?

Thank you for your insights and answers! :)

  • 写回答

1条回答 默认 最新

  • doushui3061 2012-12-07 19:35
    关注

    Try to use create using php in build function file_put_content(filename, content)

    <?php
    for ($i = 0; $i < 10; $i++) {
       $name = $dom->person[$i]->name;
       $desc = $dom->person[$i]->desc;
       $file_name = 'usercontent'.$i.'.php';
       $post = 
       '
        <li>
            <a href=$file_name>'.$name.'</a>
            <p>'.$desc.'</p>
        </li>
        ';
         file_put_contents($file_name, $post);      
     echo $post;
    }
    

    here i have created a files with usercontent0.php, usercontent1.php and so on you can also specify some other random filenames and any thing that you want to add to the page will be added to the $post value

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line