Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts

    magpieRSS help. Cannot use object of type MagpieRSS as array :(

    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 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
    Last edited by LJackson; 12-04-2008 at 07:06 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    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
    PHP Code:
        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

    and this doesnt
    PHP Code:
        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

    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
    Last edited by LJackson; 12-04-2008 at 02:37 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Ok, lets take a closer look at this.
    Looking at you're original post, lets start here:
    PHP Code:
        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()?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    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:
    PHP Code:
    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

    thanks for you time Fou-Lu

    Luke

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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:
    PHP Code:
     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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Thanks Ok Mate!!

    RE: changing the first line Good Plan

    Many Thanks
    Luke


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •