...

View Full Version : Resolved magpieRSS help. Cannot use object of type MagpieRSS as array :(



LJackson
12-04-2008, 01:11 AM
Hi All,

well as the title says i am getting a fatal error "Cannot use object of type MagpieRSS as array"

i am trying to display multiple rss/product feeds on one page with the feeds being determined by a record in a database which i use an array to fetch the correct data.

here is my code:

<?php


$user_name = "";
$password = "";
$database = "";
$server = "";


$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);


if ($db_found) {

// ORDER STORES BY HIGHEST RATING FIRST
$sql = "SELECT * FROM stores WHERE category = 'Entertainment' ORDER BY rating DESC";
$result = mysql_query($sql);
$rows = mysql_num_rows($result);



if ($result){


//for each of the stores in the database
for ($i = 0; $i < $rows; $i++) {

$row = mysql_fetch_array($result);
$storeID = $row['storeID'];

$store = $row['name'];
$stores[$i]=$store;

$feedtype = $row['feedID'];
$feed[$i] = $feedtype;



//GETTING OFFER INFO (NEW)
$rss_feed = "SELECT link FROM rss_feeds WHERE store = '$store' AND description ='top'";
$rss_result = mysql_query($rss_feed);
$rss_row = mysql_fetch_array($rss_result);
$rssIndividual = $rss_row['link'];
$rss[$i] = $rssIndividual;

define("MAGPIE_INPUT_ENCODING", "UTF-8");
define("MAGPIE_OUTPUT_ENCODING", "UTF-8");
define("MAGPIE_CACHE_DIR", "./magpierss/cache");
define("MAGPIE_CACHE_ON", 1);
define("MAGPIE_CACHE_AGE", 3600);
include_once("./magpierss/rss_fetch.inc");




//PASS THE STORE ID TO THE ADDRESS

$idReference = "?sID=" . $id;
$linkHTML = "<A HREF = \"stores.php".$idReference."\">";
$linkHTMLEnd = "</A>";

}
}
}
?>
<body>
<?php
print "<div class='recommends'>";

if ($rss[0] == ""){
print "No Information Currently Available Please Check Back Soon";
}
elseif ($rss[0] <> "" and $feed[0] == '2'){
$rss = fetch_rss($rss[0]);

$loop_count = 0;
for ($r = 0; $r <= $loop_count; $r++) {

foreach ($rss->items as $item){
if ($r <= $loop_count){
$href = $item['link'];
$title = $item['title'];
$desc = $item['description'];
$ref_date = $item['pubdate'];
$content = $item['atom_content'];

print "<br>";
print $content;
$r++;
}
}
}
}

elseif ($rss[0] <> "" and $feed[0] == '1'){
$xml=simplexml_load_file($rss[0]);
$max = 1;
for ($m = 0; $m < $max; $m++) {
// if counter is greater than max, break loop
foreach($xml as $feed2){
// create counter on first loop

if ($m < $max){
echo "<a href='{$feed2->deepLink}' target='_blank'><div class='producttitle'>{$feed2->productname}</a></div><br>";
echo "<a href='{$feed2->deepLink}' target='_blank'><img src='{$feed2->imageURL}' alt='{$feed2->productname}' border='0' hspace='5' /></a>";
$m++;
}
}
}
}

print "</div>";


print "<div class='recommends'>";

if ($rss[1] == ""){
print "No Information Currently Available Please Check Back Soon";
}
elseif ($rss[1] <> "" and $feed[1] == '2'){
$rss1 = fetch_rss($rss[1]);

$loop_count1 = 0;
for ($r = 0; $r <= $loop_count1; $r++) {

foreach ($rss1->items as $item1){
if ($r <= $loop_count1){
$href = $item1['link'];
$title = $item1['title'];
$desc = $item1['description'];
$ref_date = $item1['pubdate'];
$content = $item1['atom_content'];

print "<br>";
print $content;
$r++;
}
}
}
}

elseif ($rss[1] <> "" and $feed[1] == '1'){
$xml1=simplexml_load_file($rss[1]);
$max1 = 1;
for ($m = 0; $m < $max1; $m++) {
// if counter is greater than max, break loop
foreach($xml as $feed2){
// create counter on first loop

if ($m < $max1){
echo "<a href='{$feed2->deepLink}' target='_blank'><div class='producttitle'>{$feed2->productname}</a></div><br>";
echo "<a href='{$feed2->deepLink}' target='_blank'><img src='{$feed2->imageURL}' alt='{$feed2->productname}' border='0' hspace='5' /></a>";
$m++;
}
}
}
}

print "</div>";





?>
</body>
</html>


anyone have any ideas as to why i am getting this error?

many thanks
Luke

Fou-Lu
12-04-2008, 01:25 AM
I'm guessing that the function fetch_rss is returning an object of type MagpieRSS as a result.
The error you're receiving is due to either accessors or to iterable objects (I'm not certain which one its whining about). That is, either you cannot use it in a foreach since $rss->items is not of type iterable or $rss->items is not an array of arrays, rather an array of objects.
If I had to take a guess, I'd say its the use of $rss->items being an array of objects.

LJackson
12-04-2008, 03:01 PM
Hi Fou-Lu,

thanks for your input,

it works on other pages with the foreach $rss->items but there i am only getting a single feed at a time and here i am trying to get 3 feeds at once

here is the code for my other page which works

print "<div class='TabbedPanelsContent'>";

if ($feedID == '1' and $rssIndividualNR == ""){
print "No Data To Display Please Check Back Soon";
}
elseif ($feedID == '1' and $rssIndividualNR <> ""){

$xml=simplexml_load_file($rssIndividualNR);

$productNo = count($xml);
if ( $productNo < 10 ){
$max = $productNo;
}
else {
$max = 10;
}

$i = 1;

foreach($xml as $feed){

if ($i <= $max){
echo "<div class='topitems'>";
echo "<div class='topitemsinner'>";
echo "<a href='{$feed->deepLink}' target='_blank'><img src='{$feed->imageURL}' alt='{$feed->productname}' border='0' hspace='5' /></a>";
echo "<a href='{$feed->deepLink}' target='_blank'><div class='producttitle'>{$feed->productname}</a></div><br><br>";
echo "<div class='productdescription'>{$feed->description}</div><p>";
//echo "<div class='productdescription'>Availability: {$feed2->availability}</div>";
echo "</div>";
echo "</div>";
$i++;// increase counter by 1
}
}
}

elseif ($feedID == '2' and $rssIndividualNR == ""){
print "No Data To Display Please Check Back Soon";
}

elseif ($feedID == '2' and $rssIndividualNR <> ""){
//RSS NEWS FEED SECTION
$rss = fetch_rss($rssIndividualNR);
$loop_count = 1;

foreach ($rss->items as $item){

$href = $item['link'];
$title = $item['title'];
$desc = $item['description'];
$ref_date = $item['pubdate'];
$content = $item['atom_content'];

echo "<div class='topitems'>";
echo "<div class='topitemsinner'>";
print $title;
print "<br>";
print $content;
print $desc;
echo "</div>";
echo "</div>";
}
}

print "</div>";


this above code is repeated 3 times for 3 tab pannels on the same page here (http://www.kernow-connect.com/affiliate/stores.php?sID=Itunes)

and this doesnt

print "<div class='recommends'>";

if ($rss[0] == ""){
print "No Information Currently Available Please Check Back Soon";
}
elseif ($rss[0] <> "" and $feed[0] == '2'){
$rss = fetch_rss($rss[0]);

$loop_count = 0;
for ($r = 0; $r <= $loop_count; $r++) {

foreach ($rss->items as $item){
if ($r <= $loop_count){
$href = $item['link'];
$title = $item['title'];
$desc = $item['description'];
$ref_date = $item['pubdate'];
$content = $item['atom_content'];

print "<br>";
print $content;
$r++;
}
}
}
}

elseif ($rss[0] <> "" and $feed[0] == '1'){
$xml=simplexml_load_file($rss[0]);
$max = 1;
for ($m = 0; $m < $max; $m++) {
// if counter is greater than max, break loop
foreach($xml as $feed2){
// create counter on first loop

if ($m < $max){
echo "<a href='{$feed2->deepLink}' target='_blank'><div class='producttitle'>{$feed2->productname}</a></div><br>";
echo "<a href='{$feed2->deepLink}' target='_blank'><img src='{$feed2->imageURL}' alt='{$feed2->productname}' border='0' hspace='5' /></a>";
$m++;
}
}
}
}

print "</div>";

this is also copied three times but the only real difference that i can spot is the first one only one feed is dislayed at a time and are contained within tab pannels and the second all three are displayed at the same time with in div containers here (http://www.kernow-connect.com/affiliate/entertainment(test3).php)

so it seems odd as to why one works and the other doesnt, is there any obvious differences between the two codes that may prevent the second from displaying properly.

Many Thanks
Luke

Fou-Lu
12-04-2008, 07:50 PM
Ok, lets take a closer look at this.
Looking at you're original post, lets start here:


print "<div class='recommends'>";

if ($rss[0] == ""){
print "No Information Currently Available Please Check Back Soon";
}
elseif ($rss[0] <> "" and $feed[0] == '2'){
$rss = fetch_rss($rss[0]);

$loop_count = 0;
for ($r = 0; $r <= $loop_count; $r++) {

foreach ($rss->items as $item){
if ($r <= $loop_count){
$href = $item['link'];
$title = $item['title'];
$desc = $item['description'];
$ref_date = $item['pubdate'];
$content = $item['atom_content'];

print "<br>";
print $content;
$r++;
}
}

What I'm interested in is $rss. I would assume that what was extracted from the db 'link' is an href for an xml feed is that correct?
So, $rss would be an array of string links. Lets say its three.
This would assume that fetch_rss() retrieves an xml document from a given href. If this is correct, we want to loop through each of the links, where we the proceed through each item. However, I don't know how the fetch_rss() function works, so I don't know what it returns. Can you post the code for fetch_rss()?

LJackson
12-04-2008, 08:05 PM
Hi mate the good news is your are correct on all the above, the even better news is that i have some how got it working :)

here is the code i have used:

for ($i = 0; $i < 3; $i++) {

print "<div class='recommends'>";
if ($rssIndividual[$i] == ""){
print "error no feed";
}

elseif ($feedID[$i] == '1'){
$xml=simplexml_load_file($rssIndividual[$i]);
$max = 1;
for ($m = 0; $m < $max; $m++) {
// if counter is greater than max, break loop
foreach($xml as $feed2){
// create counter on first loop

if ($m < $max){
echo "<a href='{$feed2->deepLink}' target='_blank'><div class='producttitle'>{$feed2->productname}</a></div><br>";
echo "<a href='{$feed2->deepLink}' target='_blank'><img src='{$feed2->imageURL}' alt='{$feed2->productname}' border='0' hspace='5' /></a>";
$m++;
}
}
}
}

elseif ($feedID[$i] == '2'){
$rss = fetch_rss($rssIndividual[$i]);
$loop_count = 0;
for ($r = 0; $r <= $loop_count; $r++) {
foreach ($rss->items as $item){
if ($r <= $loop_count){
$href = $item['link'];
$title = $item['title'];
$desc = $item['description'];
$ref_date = $item['pubdate'];
$content = $item['atom_content'];

print $content;
$r++;
}
}
}
}

elseif ($feedID[$i] == '3'){
$rss = fetch_rss($rssIndividual[$i]);
$loop_count = 0;
for ($r = 0; $r <= $loop_count; $r++) {
foreach ($rss->items as $item){
if ($r <= $loop_count){
$href = $item['link'];
$title = $item['title'];
$desc = $item['description'];
$ref_date = $item['pubdate'];
$content = $item['atom_content'];

//print $content;
print $desc;
$r++;
}
}
}
}


print "</div>";
}


and it seems to be working just as i wanted only took two days :D:D

thanks for you time Fou-Lu :thumbsup:

Luke

Fou-Lu
12-04-2008, 08:14 PM
Ok great to hear. I've never used the magpie rss before, so sorry I couldn't give you a more direct answer.
I'd change you're first line though:


for ($i = 0; $i < 3; $i++) {
// to
for ($i = 0; $i < $iCountFeed; $i++)

And declare $iCountFeed somewhere else (or even just the top) of the script. It would probably simplify any changes you may want to do in the future.

LJackson
12-04-2008, 08:35 PM
Thanks Ok Mate!! :)

RE: changing the first line Good Plan

Many Thanks
Luke



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum