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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100