【发布时间】:2014-10-22 18:53:47
【问题描述】:
我创建了一个包含许多项目的 XML 文件,如下例所示:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>http://www.example.com/index.php?pagina=video-categories</id>
<title><![CDATA[Example.com Categories RSS]]></title>
<author>
<name>Example.com</name>
<email>email@example.com</email>
</author>
<updated></updated>
<link rel="alternate" href="http://www.example.com/categories"/>
<subtitle><![CDATA[HQ example videos in any category]]></subtitle>
<rights>Copyrights reserved. Feel free to use the embed function.</rights>
<item>
<categoryname><![CDATA[100 Latest Videos]]></categoryname>
<shortcategoryname><![CDATA[100 New Sex Clips]]></shortcategoryname>
<categoryurl>http://www.example.com/1.html</categoryurl>
<categoryimage>http://www.example.com/12347.jpg</categoryimage>
</item>
<item>
<categoryname><![CDATA[100 Latest Videos]]></categoryname>
<shortcategoryname><![CDATA[100 New Sex Clips]]></shortcategoryname>
<categoryurl>http://www.example.com/2.html</categoryurl>
<categoryimage>http://www.example.com/12346.jpg</categoryimage>
</item>
<item>
<categoryname><![CDATA[100 Latest Videos]]></categoryname>
<shortcategoryname><![CDATA[100 New Sex Clips]]></shortcategoryname>
<categoryurl>http://www.example.com/3.html</categoryurl>
<categoryimage>http://www.example.com/12345.jpg</categoryimage>
</item>
... and more items ...
</feed>
我有以下代码,输出 XML 文件中的所有链接
<?php
$html = ""; // var full of emptyness
$url = "http://www.example.com/categories.xml";
$xml = simplexml_load_file($url);
for($i = 0; $i < 35; $i++){ // Number of category here, I use a lower number at this moment...
$categoryname = $xml->item[$i]->categoryname;
$shortcategoryname = $xml->item[$i]->shortcategoryname;
$categoryurl = $xml->item[$i]->categoryurl;
$html .= '<a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '">' . $shortcategoryname . '</a>';
}
echo $html;
?>
我只想显示 6 个链接,并从 XML 提要中随机显示它们的位置。我想从 xml 项目中获得随机链接。我应该添加或更改什么,我应该使用rand() 或shuffle() 来回显这 6 个随机链接?
php 代码是我现在用来回显一些链接的,但它不是随机的...
【问题讨论】:
-
你的 PHP 是什么?
-
我在我的问题中使用了显示的 php 代码,但 xml 文件 url 只是一个示例 url,而不是真正的 url..
-
抱歉,我没有滚动。
-
我更改了问题以使其更具可读性,很抱歉没有将 php 添加到它自己的代码块中,但编辑后无需滚动即可阅读,谢谢..