【问题标题】:rand() shuffle xml outputrand() 随机播放 xml 输出
【发布时间】: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 添加到它自己的代码块中,但编辑后无需滚动即可阅读,谢谢..

标签: php xml random shuffle


【解决方案1】:

$xml-&gt;item 是一个数组,所以shuffle 它然后得到第一个6,类似于:

shuffle($xml->item);

foreach(array_slice($xml->item, 0, 6) as $item) {
    $categoryname = $item->categoryname;
    $shortcategoryname = $item->shortcategoryname;
    $categoryurl = $item->categoryurl;
    $html .= '<a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '">' . $shortcategoryname . '</a>';
}
echo $html;

或者使用当前代码,您可以将 HTML 存储在循环中的数组中:

$html[] = '<a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '">' . $shortcategoryname . '</a>';

在循环之后:

shuffle($html);
echo implode("\n", array_slice($html, 0, 6));

【讨论】:

  • 非常感谢,效果很好,我会在您的回答中使用第二个选项,再次感谢您的回答!
  • foreach 会更简单、更通用,例如,如果您将项目添加到 XML 中,则无需更改循环。
  • 我使用的代码:&lt;?php $html = ""; $url = "categories.xml"; $xml = simplexml_load_file($url); for($i = 0; $i &lt; 35; $i++){ $categoryname = $xml-&gt;item[$i]-&gt;categoryname; $shortcategoryname = $xml-&gt;item[$i]-&gt;shortcategoryname; $categoryurl = $xml-&gt;item[$i]-&gt;categoryurl; $html[] = '&lt;a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '"&gt;' . $shortcategoryname . '&lt;/a&gt;'; } shuffle($html); echo implode("\n", array_slice($html, 0, 6)); ?&gt; 如果我在 xml 提要中删除/添加类别,我只需要更改 35 数字,在我使用缓存的页面上,我每天只运行 4 次页面
  • @AbraCadaver 这太棒了。我最初也使用了for 循环,因为我听说它更快。有没有办法用for 而不是foreach 做到这一点?
猜你喜欢
  • 2023-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
相关资源
最近更新 更多