...

View Full Version : please assist me with this code, cant display certain data if feed count is <10



LJackson
11-01-2009, 07:38 PM
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
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 >=1 && $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

student101
11-01-2009, 08:11 PM
Why not use an amazon class like Amazon API Access with PHP and XML (http://www.phpclasses.org/browse/package/642.html) ?

My knowledge of RSS is limited that's why I rely on a class like the rssdisplaybox (http://www.dynamicdrive.com/dynamicindex18/rssdisplaybox/index.htm), you can change the feeds and settings as needed.

LJackson
11-01-2009, 10:46 PM
hi mate thanks for your reply

i think i have figured it out now thanks.

here is the code that ive used

$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 >=1 && $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 && $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 > 0 && $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

student101
11-02-2009, 05:07 AM
Glad you got it sorted :thumbsup:

LJackson
11-04-2009, 11:06 PM
cheers mate :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum