正如标题所说,我想用xml中的所有内容创建动态的新页面,然后将这些页面链接到read more链接。我的代码是这样的:
<?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新手,我正在尝试让这个脚本工作两周。
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>
没有想到任何逻辑,因为我不知道从哪里开始。非常感谢。
编辑1:这是一个代码板测试:http://codepad.viper-7.com/9gj3C0我的剧本和一些修改。
您可以使用以下内容。$file_name
必须是相对url;
<?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);
}
?>
使用文件名,或者在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);
}
?>