...

View Full Version : why does these same pieces of code produce different results?



LJackson
01-22-2010, 02:21 PM
Hi All

I have these two pieces of code

$booleansearch = array('sQuery'=> '-region 1 -console -bundle', 'oActiveRefineByGroup' => $oRefineBy, 'bAdult' => true, 'iLimit'=>3, 'sColumnToReturn' => $returnedcolumns, 'sSort'=>'popularity', 'sMode' => 'boolean');


and


$booleansearch = array('sQuery'=> '$keywords', 'oActiveRefineByGroup' => $oRefineBy, 'bAdult' => true, 'iLimit'=>3, 'sColumnToReturn' => $returnedcolumns, 'sSort'=>'popularity', 'sMode' => 'boolean');


but both produce different results, but the value of $keywords is exactly the same as in the first example.

here is where i define $keywords

switch($category)
{
case "DVD";
$aw_id = "240";
$aw_name = "DVDs";
$keywords = '-region 1 -console -bundle';
break;
case "CD";
$aw_id = "241";
$aw_name = "Music";
$keywords = '-region 1 -console -bundle';
break;
case "Video Games";
$aw_id = "579";
$aw_name = "Video Games";
$keywords = '-region 1 -console -bundle';
break;
case "Blu-Ray";
$aw_id = "550";
$aw_name = "Blu-Ray";
$keywords = '-region 1 -console -bundle';
break;
case "Books";
$aw_id = "538";
$aw_name = "Books";
$keywords = '-region 1 -console -bundle';
break;
}


i need to use the varable in the query because the api service only allows upto four negative vales and im performing searches in a total of 30 different categories but i cant understand why the two produce different results

is the $keywords being interpreted as something else? ive tried printing out the value and it is as expected.

any help would be greatly appreciated
cheers

Fou-Lu
01-22-2010, 02:22 PM
They are not the same:


$booleansearch = array('sQuery'=> '$keywords', 'oActiveRefineByGroup' => $oRefineBy, 'bAdult' => true, 'iLimit'=>3, 'sColumnToReturn' => $returnedcolumns, 'sSort'=>'popularity', 'sMode' => 'boolean');

// This should be:
$booleansearch = array('sQuery'=> $keywords, 'oActiveRefineByGroup' => $oRefineBy, 'bAdult' => true, 'iLimit'=>3, 'sColumnToReturn' => $returnedcolumns, 'sSort'=>'popularity', 'sMode' => 'boolean');


$keywords is wrapped in single quotations, so it is treating it as an unparsable string.

LJackson
01-22-2010, 02:29 PM
hi mate, if i remove the 's it still produces the same results as with 's but different to that if i type in the value of $keywords instead of $keyword.

it is very odd :)

thanks for your reply

Fou-Lu
01-22-2010, 02:35 PM
I'm assuming that category has been determine? You don't have a default case there, so if there is no valid category you won't get a valid $keywords.

In any case, if the value of $keywords is what you're expecting, than the results should be the same. Ensure you're not running against a cached page.

LJackson
01-22-2010, 02:39 PM
hi mate,

yeah category is defined

$category_list = array('DVD' => 'The latest blockbuster DVDs.',
'CD' => 'Chart hit CDs from the hottest artists.',
'Video Games' => 'Multi platform video games',
'Blu-Ray' => 'Picture perfect Blu-Ray DVDs.',
'Books' => 'From bestselling authors');


here

foreach ($category_list as $category => $sub_category)
{


how does one 'Ensure you're not running against a cached page'?

thanks
Luke

LJackson
01-22-2010, 02:52 PM
something even stranger :) i have changed all the $keywords to $keywordz and it now seems to match the expected results??? and i've checked my code and cant see where i may of defined $keywords already!

but at least its now working :)

thanks
Luke

Fou-Lu
01-22-2010, 03:08 PM
something even stranger :) i have changed all the $keywords to $keywordz and it now seems to match the expected results??? and i've checked my code and cant see where i may of defined $keywords already!

but at least its now working :)

thanks
Luke

o.O
Serously? That is weird. It wouldn't matter if it was previously defined and just reused as long as its not changed between you're switch and you're later implementation of it.
Now just do a search and replace all for $keywordz to $keywords to see if it still behaves.

The easiest way to ensure you're not cached is to just add a query string. I assume you're on localhost for testing, so rather than play around with a pile of settings, just use http://127.0.0.1/path/to/file.php?t=4adfj449339
where t is anything you want. As soon as the querystring has changed to one that is not cached, it will serve out a new page.

LJackson
01-22-2010, 03:22 PM
tried the replace all and lowembyhold the wrong results again :(

this is my entire code

<?php
if($_SESSION['page'] == "entertainment")
{
$category_list = array('DVD' => 'The latest blockbuster DVDs.',
'CD' => 'Chart hit CDs from the hottest artists.',
'Video Games' => 'Multi platform video games',
'Blu-Ray' => 'Picture perfect Blu-Ray DVDs.',
'Books' => 'From bestselling authors');
}?>

<div id="category_left">
<div class="links_container">
<?php
foreach ($category_list as $category => $sub_category)
{?>
<div class="individual_link_container">

<h2 class="red">
<?php echo $category?>
</h2>

<div class="small">
<?php echo $sub_category;?>
</div>

</div>
<?php
$x++;
}?>
</div>
</div>


<div id="category_right">
<?php
foreach ($category_list as $category => $sub_category)
{
switch($category)
{
case "DVD";
$aw_id = "240";
$aw_name = "DVDs";
$keywords = "-region 1";
break;
case "CD";
$aw_id = "241";
$aw_name = "Music";
$keywords = "cd -christmas";
break;
case "Video Games";
$aw_id = "579";
$aw_name = "Video Games";
$keywords = "video games -console";
break;
case "Blu-Ray";
$aw_id = "550";
$aw_name = "Blu-Ray";
$keywords = "-region 1";
break;
case "Books";
$aw_id = "538";
$aw_name = "Books";
$keywords = "novel -Cannabis -logbook -Wetshoes";
break;
}

print "<div class='category_container'>";
print "<h2 class='category_header red'>$category</h2>";

#GET TOP 5 ITEMS FOR EACH CATEGORY
$AW_catid = $aw_id;
$AW_cat = $aw_name;
$keywords = "";
$oRefineBy = new stdClass();
$oRefineBy -> iId = 4;
$oRefineBy -> sName = 'Category';
$oRefineByDefinition = new stdClass();
$oRefineByDefinition -> sId = $AW_catid;
$oRefineByDefinition -> sName = $AW_cat;
$oRefineBy -> oRefineByDefinition = $oRefineByDefinition;
$returnedcolumns = array(iCategoryId, iMerchantId, sAwImageUrl, fStorePrice, sAwThumbUrl, sName,sAwImageUrl,sMerchantImageUrl,sMerchantThumbUrl,sDescription,sBrand);
$booleansearch = array('sQuery'=> $keywords, 'oActiveRefineByGroup' => $oRefineBy, 'bAdult' => true, 'iLimit'=>3, 'sColumnToReturn' => $returnedcolumns, 'sSort'=>'popularity', 'sMode' => 'boolean');
$oResponse= $oClient->call('getProductList', $booleansearch);
foreach($oResponse->oProduct as $oProduct)
{
$img = htmlspecialchars($oProduct->sMerchantImageUrl);
$img2 = htmlspecialchars($oProduct->sAwThumbUrl);
$price = $oProduct->fPrice;
$store = $oProduct->iMerchantId;
$catid = $oProduct->iCategoryId;
$desc = $oProduct->sDescription;?>
<div class="individual_top5">
<h3 class="individual_top5_headers"><?php print $oProduct->sName;?></h3>
<div class="individual_top5_images">
<?php
print "<img src='$img2' height='85px' alt='$desc' title='$desc' /></div>";
print $price." ".$store." ".$catid;?>
</div>
<?php
}

print "</div>";
}

?>
</div>
<?php footer();?>

thanks for the catched info btw :)

Fou-Lu
01-22-2010, 03:52 PM
o.O
You did make an error:


$keywords = ""; // <-- you dumped the $keywords here :P
$oRefineBy = new stdClass();
$oRefineBy -> iId = 4;
$oRefineBy -> sName = 'Category';
$oRefineByDefinition = new stdClass();
$oRefineByDefinition -> sId = $AW_catid;
$oRefineByDefinition -> sName = $AW_cat;
$oRefineBy -> oRefineByDefinition = $oRefineByDefinition;
$returnedcolumns = array(iCategoryId, iMerchantId, sAwImageUrl, fStorePrice, sAwThumbUrl, sName,sAwImageUrl,sMerchantImageUrl,sMerchantThumbUrl,sDescription,sBrand);
$booleansearch = array('sQuery'=> $keywords, 'oActiveRefineByGroup' => $oRefineBy, 'bAdult' => true, 'iLimit'=>3, 'sColumnToReturn' => $returnedcolumns, 'sSort'=>'popularity', 'sMode' => 'boolean');


Also, you probably should change you're $returnedcolumns to strings. I assume they are to be strings?

I'd actually keep the $keywords initializer, but move it above you're switch. This way if for whatever reason there is no base case match, you still have $keywords available, but its no longer a non-existant variable.

LJackson
01-22-2010, 04:36 PM
crikey.. cant believe i missed that :(

thanks for spotting it at least we now know what was causing the cockup :)

i have another little problem array based i think

i have pulled out all the product data as i want including the price and the store id but i am trying to perform another call to the api to get the store name out which relates to the id. but i keep getting the same name out everytime, its a long shot but could you look at my syntax to see if there are any obvious mistakes in there for me please.

here is my full code

#GET TOP 5 ITEMS FOR EACH CATEGORY
$AW_catid = $aw_id;
$AW_cat = $aw_name;
$oRefineBy = new stdClass();
$oRefineBy -> iId = 4;
$oRefineBy -> sName = 'Category';
$oRefineByDefinition = new stdClass();
$oRefineByDefinition -> sId = $AW_catid;
$oRefineByDefinition -> sName = $AW_cat;
$oRefineBy -> oRefineByDefinition = $oRefineByDefinition;
$returnedcolumns = array(iCategoryId, iMerchantId, sAwImageUrl, fStorePrice, sAwThumbUrl, sName,sAwImageUrl,sMerchantImageUrl,sMerchantThumbUrl,sDescription,sBrand);
$booleansearch = array('sQuery'=> $keywords, 'oActiveRefineByGroup' => $oRefineBy, 'bAdult' => true, 'iLimit'=>3, 'sColumnToReturn' => $returnedcolumns, 'sSort'=>'popularity', 'sMode' => 'boolean');
$oResponse= $oClient->call('getProductList', $booleansearch);
foreach($oResponse->oProduct as $oProduct)
{
$img = htmlspecialchars($oProduct->sMerchantImageUrl);
$img2 = htmlspecialchars($oProduct->sAwThumbUrl);
$price = $oProduct->fPrice;
$store = $oProduct->iMerchantId;
$catid = $oProduct->iCategoryId;
$desc = $oProduct->sDescription;
$merchantid = $oProduct->iMerchantId;//the id i am trying to get the store name for
print $merchantid;

#GET MERCHANT NAME
$merchantcolumns = array(iId,sName,sStrapline,sDescription,sLogoUrl,sDisplayUrl,sClickThroughUrl,oDiscountCode);
$merchantrows = array("iId" => $merchantid, "sColumnToReturn" => $merchantcolumns, "iAdult" => 1);
$oResponse= $oClient->call('getMerchant', $merchantrows);
print $oResponse->oMerchant->sName;
foreach($oResponse->oMerchant as $details)
{
$merchName =$details->sName;//the store name
}

?>
<div class="individual_top5">
<h3 class="individual_top5_headers"><?php print $oProduct->sName;?></h3>
<div class="individual_top5_images">

<img src='<?php echo $img2;?>' height='85px' alt='<?php echo $desc;?>' title='<?php echo $desc;?>' /></div>
<div class='buyitnow_container'>

<div class="buyitnow_store"><span class="bold red">Buy it now</span><br />from <?php echo $merchName/*echoing the store name*/." for &pound;".$price;?></div>
</div>

<div class='compareit_container'>
<div class="compareit_link">OR <br />compare prices</div>
</div>

</div>
<?php
}


cheers
Luke

LJackson
01-23-2010, 12:48 AM
still unable to figure this out :( anyone know why i am unable to pull out the correct merchant name that matches the id???

im puzzeled :)
thanks

Fou-Lu
01-23-2010, 01:55 AM
I can't test anything that you're doing, I'm not even certain what you're $oClient is. I presume a webservice handle of some sort.
Anyway, the first thing I'd do is change this:


#GET MERCHANT NAME
$merchantcolumns = array(iId,sName,sStrapline,sDescription,sLogoUrl,sDisplayUrl,sClickThroughUrl,oDiscountCode);
$merchantrows = array("iId" => $merchantid, "sColumnToReturn" => $merchantcolumns, "iAdult" => 1);
$oResponse= $oClient->call('getMerchant', $merchantrows);
print $oResponse->oMerchant->sName;
foreach($oResponse->oMerchant as $details)
{
$merchName =$details->sName;//the store name
}


In particular, the $oResponse part. Since you're outer loop is also controlled by the $oResponse variable, changing it in the inside can lead to undesired results. Try that, see if that works.

LJackson
01-23-2010, 02:16 AM
hi mate,

the oClient i think is what you said it is or something similar, as i am making two different calls to the api 1 for product data and one for the relivant merchant data which relates to the product data.

the oRespose is just a varible which states the call procedure so each call to the api requires an oRespose (or any other name you want to give it)

thats why i thought using my loop setup the way it is it would get the merchantid e.g 245 and then have another call to the api in the same loop to get the merchant name that matches 245, but as per usual it doesnt wanna play :(

sorry for being difficult and i relise that it is difficult for you to test things out or to understand what each bit does (not even i know/understand that fully lol) but at the moment your my best shot at solving this :D

p.s i trieds changing the name of the second oResponse but no change, i know the original merchant values are correct its just getting the second call to use them as i intend, and im not sure if its an error at my end, which is very likley or if im doing the call incorrectly?

cheers
Luke

LJackson
01-23-2010, 02:31 AM
ok if i

print_r($oResponse);

i get the list of all the merchants and their ids and names which is a good thing :)

so you'd of thought a simple while statment would work to print out the name? but nope unless the code is wrong it is 1.30am uk time :(

here is what ive done

#GET MERCHANT NAME
$merchantcolumns = array(iId,sName);
$merchantrows = array("sColumnToReturn" => $merchantcolumns, "iAdult" => 1);
$oResponse= $oClient->call('getMerchant', $merchantrows);
$storeid = $oResponse->oMerchant->iId;
//print_r($oResponse);
while($storeid == $merchantid){
print $oResponse->oMerchant->sName;
}


but nothing prints out again?

Fou-Lu
01-23-2010, 02:59 AM
The while is incorrect, it will only continue while the store matches the merchant. There is no guarantee that it will be the first or the sequential in the list.
Assuming that it follows the above, you should be able to loop the $oResponse->oMerchant for each merchants, and pull out and compare the $merchantid to $storeid.

LJackson
01-24-2010, 02:24 AM
hi mate, seems like a night on the town really helps lol just got in and had a brain wave and managed to get the code working :) :)

basically did what you said to do

#GET MERCHANT NAME
$merchantcolumns = array(iId,sName);
$merchantrows = array("sColumnToReturn" => $merchantcolumns, "iAdult" => 1);
$oResponse= $oClient->call('getMerchant', $merchantrows);
foreach($oResponse->oMerchant as $merchID)
{
if($merchID->iId == $merchantid)
{
$test = $merchID->sName;
}
}



and its working as expected.... sweet!!!!
booze and programming seem to go hand in hand lol, just dont want to wake up tomorrow to find out ive wrecked everything else :D

cheers mate for your help, really appreciate it
Luke



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum