Forum Moderators: coopster
THanks!
<?php
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
define('MAGPIE_DIR', 'magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');
if ( isset($_GET['url']) ) {
$url = $_GET['url'];
}
else {
$url = 'http://www.example.com/rss/sportonline_uk_edition/front_page/rss.xml';
}
$rss = fetch_rss( $url );
$items = array_rand($rss->items,3);
if ($rss) {
foreach ($items as $item) {
$href = $item['link'];
$title = $item['title'];
$description = $item['description'];
echo "<p class='dark'>> <b><a class='dark' href=$href>$title</a></b><br />$description</p>";
}
}
else {
echo "Error: " . magpie_error();
}
?>
[edited by: dreamcatcher at 11:11 am (utc) on Aug. 20, 2007]
[edit reason] Use example.com, thanks. [/edit]
<?php
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
define('MAGPIE_DIR', 'magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');
if ( isset($_GET['url']) ) {
$url = $_GET['url'];
}
else {
$url = 'http://www.example.com/rss/sportonline_uk_edition/front_page/rss.xml';
}
$rss = fetch_rss( $url );
$count = 3;
for ($i = 0, $i < $count, $i++;
$items[] = $rss->items[rand(0, count($rss->items))];
)
if ($rss) {
foreach ($items as $item) {
$href = $item['link'];
$title = $item['title'];
$description = $item['description'];
echo "<p class='dark'>> <b><a class='dark' href=$href>$title</a></b><br />$description</p>";
}
}
else {
echo "Error: " . magpie_error();
}
?>
[edited by: dreamcatcher at 11:11 am (utc) on Aug. 20, 2007]
[edit reason] Use example.com, thanks. [/edit]
for ($i = 0; $i < count; $i++)
should be:
for ($i = 0; $i < $count; $i++)
? is that right?
It seems to work but occasionally one of the 3 items is blank...hmmm...any ideas? Am i missing something?
$count = 3;
for ($i = 0; $i < $count; $i++)
{$items[] = $rss->items[rand(0, count($rss->items))];}
if ($rss) {
foreach ($items as $item) {
I cant seem to get it so that i dont get duplicate items - can anyone help - the code at present is:
<?php
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
define('MAGPIE_DIR', 'magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');
if ( isset($_GET['url']) ) {
$url = $_GET['url'];
}
else {
$url = 'http://www.example.com/rss/sportonline_uk_edition/front_page/rss.xml';
}
$rss = fetch_rss( $url );
//Change the number to change the number of items shown
$count = 3;
for ($i = 0; $i < $count; $i++)
{$items[] = $rss->items[rand(0, count($rss->items)-1)];}
if ($rss) {
foreach ($items as $item) {
$href = $item['link'];
$title = $item['title'];
$description = $item['description'];
echo "<p class='dark'>> <b><a class='dark' href=$href>$title</a></b><br />$description</p>";
}
}
else {
echo "Error: " . magpie_error();
}
?>
[edited by: dreamcatcher at 11:12 am (utc) on Aug. 20, 2007]
[edit reason] Use example.com, thanks. [/edit]
Add the following (this is not a solution, it is just to show you the content of that array, and is to be removed later)
echo "<pre>";
print_r($items);
echo "</pre>";
From what is printed, are there any duplicates?
Habtom
(
[0] => Array
(
[title] => Sunday's SPL photos
[description] => Pictures from Sunday's match in the SPL
[link] => http://www.example.com/go/rss/-/sport1/hi/scotland/6953904.stm
[guid] => http://www.example.com/sport1/hi/scotland/6953904.stm
[pubdate] => Sun, 19 Aug 2007 14:14:09 GMT
[category] => Scotland
[summary] => Pictures from Sunday's match in the SPL
[date_timestamp] => 1187532849
)
=> Array [2] => Array ) [1][edited by: dreamcatcher at 11:11 am (utc) on Aug. 20, 2007]
(
[title] => Coulthard reveals bulimia battle
[description] => David Coulthard reveals how he suffered from an eating disorder in a bid to get into Formula One.
[link] => http://www.example.com/go/rss/-/sport1/hi/motorsport/formula_one/6954569.stm
[guid] => http://www.example.com/sport1/hi/motorsport/formula_one/6954569.stm
[pubdate] => Mon, 20 Aug 2007 09:27:55 GMT
[category] => Formula One
[summary] => David Coulthard reveals how he suffered from an eating disorder in a bid to get into Formula One.
[date_timestamp] => 1187602075
)
(
[title] => Sunday's SPL photos
[description] => Pictures from Sunday's match in the SPL
[link] => http://www.example.com/go/rss/-/sport1/hi/scotland/6953904.stm
[guid] => http://www.example.com/sport1/hi/scotland/6953904.stm
[pubdate] => Sun, 19 Aug 2007 14:14:09 GMT
[category] => Scotland
[summary] => Pictures from Sunday's match in the SPL
[date_timestamp] => 1187532849
)
[edit reason] Use example.com, thanks. [/edit]
Habtom
{$items[] = $rss->items[rand(0, count($rss->items)-1)];}if ($rss) {
foreach ($items as $item) {
$href = $item['link'];
$title = $item['title'];
$description = $item['description'];
echo "<p class='dark'>> <b><a class='dark' href=$href>$title</a></b><br />$description</p>";
}
Replace with the following:
{$items2[] = $rss->items[rand(0, count($rss->items)-1)];}
if ($rss) {
foreach ($items2 as $item) {
$href = $item['link'];
$title = $item['title'];
$description = $item['description'];
echo "<p class='dark'>> <b><a class='dark' href=$href>$title</a></b><br />$description</p>";
}
If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries.
Note how array_rand returns an array of keys, not an array of random elements. That means, rather than:
you want something more like:$items = array_rand($rss->items, 3);
foreach ($items as $item) { /* use $item */ }
$item_keys = array_rand($rss->items, 3);
foreach ($item_keys as $key) {
$item = $rss->items[$key];
/* use $item */
}
Or (untested, but should give you the general idea):
function array_rand_values($array, $num) {
if ($num == 1)
return $array[array_rand($array)];
else {
$ret = array();
foreach (array_rand($array, $num) as $key)
$ret[] = $array[$key];
return $ret;
}
}
...
$items = array_rand_values($rss->items, 3);
...
Or
shuffle($rss->items); $items = array_slice($rss->items, 0, 3);, if you don't mind $rss->items getting reordered. Incidentally, the reason
$items[] = $rss->items[rand(0, count($rss->items)-1)]; is giving you duplicate items is because that's how random numbers work. Each call to rand() picks a number independent of each prior call to rand, which means sometimes you'll get the same number twice in a row, just like sometimes you'll get "heads" thrice in a row when flipping a coin.