Reputation: 922
I created a XML file with a number of items like the example below:
<?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 protected]</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>
I have the following code with as output all links from the XML file
<?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;
?>
I'd like to show only 6 links and make their position radom from the XML feed. I'd like to have random links from the xml items. what should I add or change and should I use rand()
or shuffle()
to echo the 6 random links?
The php code is what I'm using at this moment to echo some links but it's not random...
Upvotes: 1
Views: 626
Reputation: 78994
$xml->item
is an array, so shuffle
it and then get the first 6
, something like:
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;
Or with current code you could store the HTML in an array in the loop:
$html[] = '<a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '">' . $shortcategoryname . '</a>';
And after the loop:
shuffle($html);
echo implode("\n", array_slice($html, 0, 6));
Upvotes: 1