如何从xml节点动态创建php页面,然后链接到该页面 - simplexml - php

时间:2014-04-25 12:25:46

标签: javascript php xml

正如标题所说,我想创建包含xml中所有内容的动态新页面,然后将这些页面链接到更多链接。我的代码就是这个:

<?php

    $xml = simplexml_load_file('$url');
?>
<?php
for ($i = 0; $i < 10; $i++) {
   $name = $xml->hotel[$i]->hotel['hotelname'];
   $desc = $xml->hotel[$i]->description;
   $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;
}

但是我如何将dynamicaly链接到创建的文件?我也有照片,我必须从xml节点。任何帮助都是相关的。 PHP的新手,我正在努力使这个脚本现在工作2周。

xml看起来像这样:

<hotels>
<hotel hotelcode="ADORA" hotelname="ADORA GOLF RESORT HOTEL" country="TURKEY" location="BELEK">
 <description> bla blah</description>
<images>
<image url="http://46.108.32.196/hotel_images/ADORA___1559.jpg"/>
<image url="http://46.108.32.196/hotel_images/ADORA___1560.jpg"/>
</images>
</hotel>

Haven没有想到任何逻辑,因为我不知道从哪里开始。 谢谢。

编辑1: 这是一个键盘测试:http://codepad.viper-7.com/9gj3C0我的脚本和一些修改。

2 个答案:

答案 0 :(得分:0)

您可以使用以下内容。 $file_name必须是相对网址;

<?php
$xml = simplexml_load_file($url);
foreach ($xml->hotels as $hotel) {
    $name = $hotel->attributes()->hotelname;
    $desc = $hotel->description;
    $file_name = 'usercontent'.$hotel->hotelcode.'.php';

    foreach($hotel->images as $image) {
        $imgUrl = $image->attributes()->url;
    }

    $post = '
    <li>
        <a href=$file_name>'.$name.'</a>
        <p>'.$desc.'</p>
        <p><img src="'.$imgUrl.'"/></p>
    </li>
    ';
}

file_put_contents($file_name, $post);      
}

?>

答案 1 :(得分:0)

使用文件名,或在simple_load_file()函数中插入xml文件位置而不是变量。

<?php
$xml = simplexml_load_file(path/to/your/file.xml);
foreach ($xml->hotels as $hotel) {
$name = $hotel->attributes()->hotelname;
$desc = $hotel->description;
$file_name = 'usercontent'.$hotel->hotelcode.'.php';

foreach($hotel->images as $image) {
    $imgUrl = $image->attributes()->url;
}

$post = '
<li>
    <a href=$file_name>'.$name.'</a>
    <p>'.$desc.'</p>
    <p><img src="'.$imgUrl.'"/></p>
</li>
';
}
file_put_contents($file_name, $post);      
}
?>