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 5 of 5
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Thumbs down please assist me with this code, cant display certain data if feed count is <10

    Hi All,

    i am trying to dispaly amazon feeds on my website however where im stuck is if the feed has less than 10 items in it.

    so i check the number of items and if there are 10 (max number = 10 per feed) then set the varible values and insert then in the db.

    if there are less than 10 items in the feed i am trying to set record varibles for the number of items in the feed and then set different values for the varibles for the spare records

    so if there are 6 items in the feed there are 4 spares or blanks as there called in the code.

    here is my code
    PHP Code:
    <?php
        
    function get_string_between($string$start$end){
            
    $string " ".$string;
            
    $ini strpos($string,$start);
            if (
    $ini == 0) return "";
            
    $ini += strlen($start);   
            
    $len strpos($string,$end,$ini) - $ini;
            return 
    substr($string,$ini,$len);
        }    
        
        
    //Entertainment Update
        
        
    $today date("d-M-Y, h:i a");
        
            
    $feeds = array(
    "http://www.amazon.co.uk/rss/bestsellers/electronics/560860/ref=pd_ts_rss_link",
    "http://www.amazon.co.uk/rss/new-releases/electronics/560860/ref=pd_nr_rss_link",
    "http://www.amazon.co.uk/rss/most-gifted/electronics/560860/ref=pd_mg_rss_link");
        
        
    $today date("d-M-Y, h:i a");
        
        
    $itemno 1;
        
    $itemtype 0;
        
    $product_Number 1;
        
        foreach (
    $feeds as $feed)
        {
            
    $count 1;
              
    $feedreader simplexml_load_file("$feed");
            
    $p_cnt count($feedreader->channel->item);    
        
            if(
    $p_cnt <=9)
            {
                
    $blanks = (10 $p_cnt);
                print 
    $blanks;
            }
                else
                {
                    
    $blanks 0;
                    print 
    $blanks;
                }
        
            if(
    $product_Number <=30)
                {
                    
    $subCat "DVD";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=60)
                {
                    
    $subCat "CD";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=110)
                {
                    
    $subCat "Games";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=140)
                {
                    
    $subCat "Blu-Ray";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=170)
                {
                    
    $subCat "Books";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=200)
                {
                    
    $subCat "Games2";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=230)
                {
                    
    $subCat "Desktop";
                    
    $category "Computing";
                }
                elseif(
    $product_Number <=260)
                {
                    
    $subCat "Notebook";
                    
    $category "Computing";
                }
                elseif(
    $product_Number <=290)
                {
                    
    $subCat "Netbook";
                    
    $category "Computing";
                }
                elseif(
    $product_Number <=320)
                {
                    
    $subCat "Printer";
                    
    $category "Computing";
                }        
                elseif(
    $product_Number <=350)
                {
                    
    $subCat "Scanner";
                    
    $category "Computing";
                }
                elseif(
    $product_Number <=380)
                {
                    
    $subCat "Software";
                    
    $category "Computing";
                }    
                elseif(
    $product_Number <=410)
                {
                    
    $subCat "Accessories";
                    
    $category "Computing";
                }
                elseif(
    $product_Number <=440)
                {
                    
    $subCat "LCD TVs";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=470)
                {
                    
    $subCat "Plasma TVs";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=500)
                {
                    
    $subCat "Full HD TVs";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=530)
                {
                    
    $subCat "MP3 and Digital Audio Players";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=560)
                {
                    
    $subCat "Digital Cameras";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=590)
                {
                    
    $subCat "Home Cinema Systems";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=620)
                {
                    
    $subCat "DVD Players";
                    
    $category "Electronics";
                }
                
          if
            (
    $itemno >=&& $itemno <=10 || $itemno >=31 && $itemno <=40 || $itemno >=61 && $itemno <=110 || $itemno >=111 && $itemno <=120 || $itemno >=141 && $itemno <=150 || $itemno >=171 && $itemno <=180 || $itemno >=201 && $itemno <=210 || $itemno >=231 && $itemno <=240 || $itemno >=261 && $itemno <=270 || $itemno >=291 && $itemno <=300 || $itemno >=321 && $itemno <=330 || $itemno >=351 && $itemno <=360 || $itemno >=381 && $itemno <=390 || $itemno >=411 && $itemno <=420 || $itemno >=441 && $itemno <=450 || $itemno >=471 && $itemno <=480 || $itemno >=501 && $itemno <=510 || $itemno >=531 && $itemno <=540 || $itemno >=561 && $itemno <=570 || $itemno >=591 && $itemno <=600){
            
    $subcat "Bestsellers";
            }
          elseif
        (
    $itemno >=11 && $itemno <=20 || $itemno >=41 && $itemno <=50 || $itemno >=121 && $itemno <=130|| $itemno >=151 && $itemno <=160  || $itemno >=181 && $itemno <=190  || $itemno >=211 && $itemno <=220 || $itemno >=241 && $itemno <=250 || $itemno >=271 && $itemno <=280 || $itemno >=301 && $itemno <=310 || $itemno >=331 && $itemno <=340 || $itemno >=361 && $itemno <=370 || $itemno >=391 && $itemno <=400 || $itemno >=421 && $itemno <=430 || $itemno >=451 && $itemno <=460 || $itemno >=481 && $itemno <=490 || $itemno >=511 && $itemno <=520 || $itemno >=541 && $itemno <=550 || $itemno >=571 && $itemno <=580 || $itemno >=601 && $itemno <=610){
            
    $subcat "New Releases";
            }
          elseif
        (
    $itemno >=21 && $itemno <=30 || $itemno >=51 && $itemno <=60 || $itemno >=131 && $itemno <=140|| $itemno >=161 && $itemno <=170 || $itemno >=191 && $itemno <=200 || $itemno >=221 && $itemno <=230 || $itemno >=251 && $itemno <=260 || $itemno >=281 && $itemno <=290 || $itemno >=311 && $itemno <=320 || $itemno >=341 && $itemno <=350 || $itemno >=371 && $itemno <=380 || $itemno >=401 && $itemno <=410 || $itemno >=431 && $itemno <=440 || $itemno >=461 && $itemno <=470 || $itemno >=491 && $itemno <=500 || $itemno >=421 && $itemno <=430 || $itemno >=551 && $itemno <=560 || $itemno >=581 && $itemno <=590 || $itemno >=611 && $itemno <=620){
            
    $subcat "Gifted";
            }    
        
            foreach (
    $feedreader->channel->item as $item)
            {
                if(
    $blanks == 0)
                {
                    
    $title addslashes ($item->title);
                    
    $title preg_replace('/#\d+:\s/'""$title);
                    
    $short_title preg_replace("/#\d: |\s*\(.*\)|\s*\[.*\]/U"""$title);
                    
    $photosource get_string_between($item->description'<img src="','"');
                    
    $otherinfo get_string_between($item->description'<span class="riRssContributor">','</span>');    
                    
    $otherinfo strip_tags($otherinfo);
                    
    $otherinfo preg_replace("/DVD|~ |\(Artist\)|\(Author\)/"""$otherinfo);
                    
    $otherinfo trim($otherinfo);
                    
    $i++;
                }
                    elseif(
    $blanks <> 0)
                    {
                        for(
    $a=0$a <= $p_cnt$a++)
                        {
                            
    $title addslashes ($item->title);
                            
    $title preg_replace('/#\d+:\s/'""$title);
                            
    $short_title preg_replace("/#\d: |\s*\(.*\)|\s*\[.*\]/U"""$title);
                            
    $photosource get_string_between($item->description'<img src="','"');
                            
    $otherinfo get_string_between($item->description'<span class="riRssContributor">','</span>');    
                            
    $otherinfo strip_tags($otherinfo);
                            
    $otherinfo preg_replace("/DVD|~ |\(Artist\)|\(Author\)/"""$otherinfo);
                            
    $otherinfo trim($otherinfo);
                            
    $a++;
                            print 
    "<p>";
                            print 
    $count;
                            print 
    $category;
                            print 
    $subcat;
                            print 
    $product_Number;
                            print 
    $title;
                            print 
    $subCat;
                            print 
    "<br />";
                            print 
    "</p>";
                            
    $count++;
                            
    $itemno++;
                            
    $product_Number ++;
                        }
                    
                        for(
    $b=0$b <= $blanks$b++)
                        {
                            
    $title "No Item";
                            
    $short_title "No Item";
                            
    $photosource "";
                            
    $otherinfo "";
                            
    $b++;                
                            print 
    "<p>";
                            print 
    $count;
                            print 
    $category;
                            print 
    $subcat;
                            print 
    $product_Number;
                            print 
    $title;
                            print 
    $subCat;
                            print 
    "<br />";
                            print 
    "</p>";
                            
    $count++;
                            
    $itemno++;
                            
    $product_Number ++;
                        }    
                    }
            print 
    "<p>";
            print 
    $count;
            print 
    $category;
            print 
    $subcat;
            print 
    $product_Number;
            print 
    $title;
            print 
    $subCat;
            print 
    "<br />";
            print 
    "</p>";
            
    $count++;
            
    $itemno++;
            
    $product_Number ++;
        }
    }
    ?>
    the problem with this is it is for the feeds with less than 10 items its printing No Item for each of the items in the feed but then nothing for the blanks

    can anyone help me please
    many thanks
    Luke
    Last edited by LJackson; 11-01-2009 at 10:47 PM.

  • #2
    Regular Coder student101's Avatar
    Join Date
    Nov 2007
    Posts
    634
    Thanks
    80
    Thanked 15 Times in 15 Posts
    Why not use an amazon class like Amazon API Access with PHP and XML ?

    My knowledge of RSS is limited that's why I rely on a class like the rssdisplaybox, you can change the feeds and settings as needed.
    Thanks for your support!
    Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

    Give thanks & resolve when done :thumbsup:

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi mate thanks for your reply

    i think i have figured it out now thanks.

    here is the code that ive used
    PHP Code:
        $today date("d-M-Y, h:i a");
        
            
    $feeds = array(
    "http://www.amazon.co.uk/rss/bestsellers/electronics/560860/ref=pd_ts_rss_link",
    "http://www.amazon.co.uk/rss/new-releases/electronics/560860/ref=pd_nr_rss_link",
    "http://www.amazon.co.uk/rss/most-gifted/electronics/560860/ref=pd_mg_rss_link");
        
        
    $today date("d-M-Y, h:i a");
        
        
    $itemno 1;
        
    $itemtype 0;
        
    $product_Number 1;
        
        foreach (
    $feeds as $feed)
        {
            
    $count 1;
              
    $feedreader simplexml_load_file("$feed");
            
    $p_cnt count($feedreader->channel->item);    
        
            if(
    $p_cnt <=10)
            {
                
    $blanks = (10 $p_cnt);
                print 
    $blanks;
            }
                else
                {
                    
    $blanks 0;
                    print 
    $blanks;
                }
        
            if(
    $product_Number <=30)
                {
                    
    $subCat "DVD";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=60)
                {
                    
    $subCat "CD";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=110)
                {
                    
    $subCat "Games";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=140)
                {
                    
    $subCat "Blu-Ray";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=170)
                {
                    
    $subCat "Books";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=200)
                {
                    
    $subCat "Games2";
                    
    $category "Entertainment";
                }
                elseif(
    $product_Number <=230)
                {
                    
    $subCat "Desktop";
                    
    $category "Computing";
                }
                elseif(
    $product_Number <=260)
                {
                    
    $subCat "Notebook";
                    
    $category "Computing";
                }
                elseif(
    $product_Number <=290)
                {
                    
    $subCat "Netbook";
                    
    $category "Computing";
                }
                elseif(
    $product_Number <=320)
                {
                    
    $subCat "Printer";
                    
    $category "Computing";
                }        
                elseif(
    $product_Number <=350)
                {
                    
    $subCat "Scanner";
                    
    $category "Computing";
                }
                elseif(
    $product_Number <=380)
                {
                    
    $subCat "Software";
                    
    $category "Computing";
                }    
                elseif(
    $product_Number <=410)
                {
                    
    $subCat "Accessories";
                    
    $category "Computing";
                }
                elseif(
    $product_Number <=440)
                {
                    
    $subCat "LCD TVs";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=470)
                {
                    
    $subCat "Plasma TVs";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=500)
                {
                    
    $subCat "Full HD TVs";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=530)
                {
                    
    $subCat "MP3 and Digital Audio Players";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=560)
                {
                    
    $subCat "Digital Cameras";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=590)
                {
                    
    $subCat "Home Cinema Systems";
                    
    $category "Electronics";
                }
                elseif(
    $product_Number <=620)
                {
                    
    $subCat "DVD Players";
                    
    $category "Electronics";
                }
                
          if
            (
    $itemno >=&& $itemno <=10 || $itemno >=31 && $itemno <=40 || $itemno >=61 && $itemno <=110 || $itemno >=111 && $itemno <=120 || $itemno >=141 && $itemno <=150 || $itemno >=171 && $itemno <=180 || $itemno >=201 && $itemno <=210 || $itemno >=231 && $itemno <=240 || $itemno >=261 && $itemno <=270 || $itemno >=291 && $itemno <=300 || $itemno >=321 && $itemno <=330 || $itemno >=351 && $itemno <=360 || $itemno >=381 && $itemno <=390 || $itemno >=411 && $itemno <=420 || $itemno >=441 && $itemno <=450 || $itemno >=471 && $itemno <=480 || $itemno >=501 && $itemno <=510 || $itemno >=531 && $itemno <=540 || $itemno >=561 && $itemno <=570 || $itemno >=591 && $itemno <=600){
            
    $subcat "Bestsellers";
            }
          elseif
        (
    $itemno >=11 && $itemno <=20 || $itemno >=41 && $itemno <=50 || $itemno >=121 && $itemno <=130|| $itemno >=151 && $itemno <=160  || $itemno >=181 && $itemno <=190  || $itemno >=211 && $itemno <=220 || $itemno >=241 && $itemno <=250 || $itemno >=271 && $itemno <=280 || $itemno >=301 && $itemno <=310 || $itemno >=331 && $itemno <=340 || $itemno >=361 && $itemno <=370 || $itemno >=391 && $itemno <=400 || $itemno >=421 && $itemno <=430 || $itemno >=451 && $itemno <=460 || $itemno >=481 && $itemno <=490 || $itemno >=511 && $itemno <=520 || $itemno >=541 && $itemno <=550 || $itemno >=571 && $itemno <=580 || $itemno >=601 && $itemno <=610){
            
    $subcat "New Releases";
            }
          elseif
        (
    $itemno >=21 && $itemno <=30 || $itemno >=51 && $itemno <=60 || $itemno >=131 && $itemno <=140|| $itemno >=161 && $itemno <=170 || $itemno >=191 && $itemno <=200 || $itemno >=221 && $itemno <=230 || $itemno >=251 && $itemno <=260 || $itemno >=281 && $itemno <=290 || $itemno >=311 && $itemno <=320 || $itemno >=341 && $itemno <=350 || $itemno >=371 && $itemno <=380 || $itemno >=401 && $itemno <=410 || $itemno >=431 && $itemno <=440 || $itemno >=461 && $itemno <=470 || $itemno >=491 && $itemno <=500 || $itemno >=421 && $itemno <=430 || $itemno >=551 && $itemno <=560 || $itemno >=581 && $itemno <=590 || $itemno >=611 && $itemno <=620){
            
    $subcat "Gifted";
            }    
        
            foreach (
    $feedreader->channel->item as $item)
            {
                if(
    $blanks == && $count <= 10)
                {
                    
    $title addslashes ($item->title);
                    
    $title preg_replace('/#\d+:\s/'""$title);
                    
    $short_title preg_replace("/#\d: |\s*\(.*\)|\s*\[.*\]/U"""$title);
                    
    $photosource get_string_between($item->description'<img src="','"');
                    
    $otherinfo get_string_between($item->description'<span class="riRssContributor">','</span>');    
                    
    $otherinfo strip_tags($otherinfo);
                    
    $otherinfo preg_replace("/DVD|~ |\(Artist\)|\(Author\)/"""$otherinfo);
                    
    $otherinfo trim($otherinfo);
                    print 
    "<p>";
                    print 
    $count;
                    print 
    $category;
                    print 
    $subcat;
                    print 
    $product_Number;
                    print 
    $title;
                    print 
    $subCat;
                    print 
    "<br />";
                    print 
    "</p>";
                    
    $count++;
                    
    $itemno++;
                    
    $product_Number ++;

                }
                    elseif(
    $blanks && $count <= 10)
                    {
                        if(
    $count <= $p_cnt)
                        {
                            
    $title addslashes ($item->title);
                            
    $title preg_replace('/#\d+:\s/'""$title);
                            
    $short_title preg_replace("/#\d: |\s*\(.*\)|\s*\[.*\]/U"""$title);
                            
    $photosource get_string_between($item->description'<img src="','"');
                            
    $otherinfo get_string_between($item->description'<span class="riRssContributor">','</span>');    
                            
    $otherinfo strip_tags($otherinfo);
                            
    $otherinfo preg_replace("/DVD|~ |\(Artist\)|\(Author\)/"""$otherinfo);
                            
    $otherinfo trim($otherinfo);
                            print 
    "<p>";
                            print 
    $count;
                            print 
    $category;
                            print 
    $subcat;
                            print 
    $product_Number;
                            print 
    $title;
                            print 
    $subCat;
                            print 
    "<br />";
                            print 
    "</p>";
                            
    $count++;
                            
    $itemno++;
                            
    $product_Number ++;
                        }
                        for(
    $i=1;$i<=$blanks;$i++){
                        if(
    $count $p_cnt)
                        {
                            
    $title "No Item";
                            
    $short_title "No Item";
                            
    $photosource "";
                            
    $otherinfo "";
                            
    $b++;                
                            print 
    "<p>";
                            print 
    $count;
                            print 
    $category;
                            print 
    $subcat;
                            print 
    $product_Number;
                            print 
    $title;
                            print 
    $subCat;
                            print 
    "<br />";
                            print 
    "</p>";
                            
    $count++;
                            
    $itemno++;
                            
    $product_Number ++;
                            
                        }    }
                    }

        }

    seems to work on the test i did need to give it a more vigerous testing to check it works properly.

    cheers
    Luke

  • #4
    Regular Coder student101's Avatar
    Join Date
    Nov 2007
    Posts
    634
    Thanks
    80
    Thanked 15 Times in 15 Posts

    Thumbs up

    Glad you got it sorted
    Thanks for your support!
    Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

    Give thanks & resolve when done :thumbsup:

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    cheers mate


  •  

    Posting Permissions

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