I am running two almost identical programs with the exception that one needs to only look at the lowest price, the second needs to look at the lowest 4 prices and get the average. my problem is that when running the second program, I get an out of memory php fatal error. My hosting provider has set the memory limit to 3000M for me and I still get the error.
Here is the code for the first program:
$parsed_xml = amazon_xml($isbn);
$current = $parsed_xml->ListMatchingProductsResult->Products->Product;
$asin = $current->Identifiers->MarketplaceASIN->ASIN;
//print_r($asin);
// get information based on the items ASIN
$price_xml = amazonPrice_xml($asin, $ItemCondition);
$currentPrice = $price_xml ->GetLowestOfferListingsForASINResult->Product->LowestOfferListings->LowestOfferListing;
$listPrice = $currentPrice->Price->ListingPrice->Amount;
// check to see if there are values
if(!empty($listPrice))
{
//print_r($listPrice); die;
//if($currentPrice->Price->ListingPrice->Amount > 0) {
while(count($lowestPrices) < 2)
{
foreach($currentPrice as $offer){
$totalFeedback = $offer->SellerFeedbackCount;
$condition = $offer->Qualifiers->ItemSubcondition;
//amazon condition matching algorithm (so we can match our condition up against amazons conditions)
switch ($condition) {
case "New":
$amazonCondition = 5;
break;
case "Mint":
$amazonCondition = 4;
break;
case "VeryGood":
$amazonCondition = 3;
break;
case "Good":
$amazonCondition = 2;
break;
case "Acceptable":
$amazonCondition = 1;
break;
default:
$amazonCondition = 0;
} // end of switch statement
//echo $condition . "|" . $ourCondition . "|" . $amazonCondition . "|" . count($lowestPrices) . "|" . $totalFeedback . "|" . $merchantId . "<br/>";
/* default lowest 1 */
if(count($lowestPricesDefault[$a] <2)){
$lowestPricesDefault[$a] = str_replace('$','',$offer->Price->ListingPrice->Amount);
$a++;
}
if( ($ourCondition <= $amazonCondition) && ($totalFeedback >= 1500) && (count($lowestPrices) <2) )
{
$lowestPrices[$x] = str_replace('$','',$offer->Price->ListingPrice->Amount);
$x++;
}
}
$z++;
}
if(count($lowestPrices) > 0){
$avgPrice = (array_sum($lowestPrices)/count($lowestPrices)) - .10;
$source = "Amazon Condition Price";
}else{
//$avgPrice = $listPrice - ($listPrice * 0.25);
$avgPrice = (array_sum($lowestPricesDefault)/count($lowestPricesDefault)) - .10;
$source = "Default Pricing";
}
//make sure avg price is atleast 5.50, >236% of follett price, and >=200% of our cost
if($avgPrice < ($follettPrice * 2.37)){
$avgPrice = $follettPrice * 2.37;
$source = "Follett Pricing";
}
if($avgPrice < ($row['cost'] * 2)){
$avgPrice = $row['cost'] * 2;
$source = "Double Cost";
}
if($avgPrice < 5.50){
$avgPrice = 5.50;
$source = "Lowest Base Cost";
}
//update Prices
$conn->query("UPDATE inventory SET ourPrice = $avgPrice WHERE sku=" . $row['sku']);
Here is the second program:
$parsed_xml = amazon_xml($isbn);
$current = $parsed_xml->ListMatchingProductsResult->Products->Product;
$asin = $current->Identifiers->MarketplaceASIN->ASIN;
// get information based on the items ASIN
$price_xml = amazonPrice_xml($asin, $ItemCondition);
$currentPrice = $price_xml ->GetLowestOfferListingsForASINResult->Product->LowestOfferListings->LowestOfferListing;
$listPrice = $currentPrice->Price->ListingPrice->Amount;
// check to see if there are values
if(!empty($listPrice))
{
//if($price_xml) {
while(count($lowestPrices) < 4 ) // changed count to 4 per loralee's email 5-1-2012
{
foreach($currentPrice as $offer){
$totalFeedback = $offer->SellerFeedbackCount;
$condition = $offer->Qualifiers->ItemSubcondition;
//amazon condition matching algorithm (so we can match our condition up against amazons conditions)
switch ($condition) {
case "New":
$amazonCondition = 5;
break;
case "Mint":
$amazonCondition = 4;
break;
case "VeryGood":
$amazonCondition = 3;
break;
case "Good":
$amazonCondition = 2;
break;
case "Acceptable":
$amazonCondition = 1;
break;
default:
$amazonCondition = 0;
}
//echo $condition . "|" . $ourCondition . "|" . $amazonCondition . "|" . count($lowestPrices) . "|" . $totalFeedback . "|" . $merchantId . "<br/>";
/* default lowest 4 */
if(count($lowestPricesDefault[$a] < 4)){ //changed to 4 per new pricing specs
$lowestPricesDefault[$a] = str_replace('$','',$offer->Price->ListingPrice->Amount);
$a++;
}
if( ($ourCondition <= $amazonCondition) && ($totalFeedback >= 99) && (count($lowestPrices) < 4) ) //changed to 4 per new pricing specs
{
$lowestPrices[$x] = str_replace('$','',$offer->Price->ListingPrice->Amount);
$x++;
}
}
$z++;
}
if(count($lowestPrices) > 0){
$avgPrice = array_sum($lowestPrices)/count($lowestPrices);
$source = "Amazon Condition Price";
}else{
//$avgPrice = $listPrice - ($listPrice * 0.25);
$avgPrice = array_sum($lowestPricesDefault)/count($lowestPricesDefault);
$source = "Default Pricing";
}
//make sure avg price is atleast 5.50, >236% of follett price, and >=200% of our cost
if($avgPrice < ($follettPrice * 2.37)){
$avgPrice = $follettPrice * 2.37;
$source = "Follett Pricing";
}
if($avgPrice < ($row['cost'] * 2)){
$avgPrice = $row['cost'] * 2;
$source = "Double Cost";
}
if($avgPrice < 5.50){
$avgPrice = 5.50;
$source = "Lowest Base Cost";
}
//update fillzPrice
$conn->query("UPDATE inventory SET ourPrice = $avgPrice WHERE sku=" . $row['sku']);
I have been getting the error on line 86 of the second program which is the condition line ($condition = $offer->Qualifiers->ItemSubcondition;) Does anyone have an idea why this is happening? Also, does anyone have any suggestions to make it run any better?
First, two code-review points:
$a,$x, and$z, all with no initial condition, and all entirely opaque to anyone trying to maintain the code. A single$ias a loop-counter is commonly considered acceptable, but good variable-naming can make debugging a lot easier.$asin = $current->Identifiers->MarketplaceASIN->ASIN;should read$asin = (string)$current->Identifiers->MarketplaceASIN->ASIN;Among other things, forgetting to do this will mean that$asincould never be saved to a session (since SimpleXML objects cannot be serialized).As for why your code is failing, I think the structure of your loops is wrong:
Firstly,
$zdoesn’t seem to be used for anything. Secondly,$lowestPricesmay be added to 0, 1, 4, or even 100 times inside theforeachloop. If it is added to at least 4 times, thewhileloop will immediately exit (so might as well not exist); if it is added to less than 4 times, theforeachloop will simply be run again over the same data. It’s not clear to me that this will make it any more likely to meet the condition of thewhileloop, giving you an infinite loop.The reason you’re running out of memory, rather than just CPU time, is the additional array
$lowestPricesDefaultgrows even when$lowestPricesdoesn’t, so it will carry on growing indefinitely every time thewhileloop (and therefore the whole innerforeachloop) repeats.