Hi Dave
Was wondering if there is anyway to include a script that automatically calculates the difference between the cheapest and most expensive product
eg at present the price difference on Product LG 50PX4D is £1,895.09, I would like to have at the top of the page a script that automatically prints:
Save Up to £1,895.09 on the LG 50PX4D By Comparing on buy24-7.net
is this possible?
regards
phil
Hi Dave
I get this error when i try the above code?
Parse error: parse error, unexpected '}' in /home/buy247/public_html/html/product.php on line 10
any suggestions?
thanks
phil
Hi Phil,
There is a semi-colon missing in that code on the last line... try this:
<?php
$numProducts = count($product["products"]);
if ($numProducts > 1)
{
$saving = $product["products"][($numProducts-1)]["price"] - $product["products"][0]["price"];
}
if ($saving)
{
print "Save Up to £".$saving." on the ".$product["products"][0]["name"]." By Comparing on example.com";
}
?>
Whenever you are debugging PHP, "unexpected '}'" almost always indicates one of 2 things:
a) a missing ";" on a previous line..
b) a mis-matched bracketed expression on a previous line, e.g. (($x+4)+($y+2)
Cheers,
David.
Morning - i like this feature but can't seem to get it working due to my lack of php knowledge!
My code is below...
<div class='product'>
<?php $mainProduct = $product["products"][0]; ?>
<table width='100%'>
<tr>
<td width='200'>
<?php if ($mainProduct["image_url"]): ?>
<img width='180' src='<?php print $mainProduct["image_url"]; ?>' alt='<?php print $mainProduct["name"]; ?>' />
<?php endif; ?>
</td>
<td valign='top'>
<h3><a href='<?php print tapestry_buyURL($mainProduct); ?>' <?php print javascript_statusBar(translate("go to")." ".$mainProduct["merchant"]); ?>><?php print $mainProduct["name"]; ?></a></h3>
<?php if ($mainProduct["description"]): ?>
<p><?php print $mainProduct["description"]; ?></p>
<?php endif; ?>
<table border='0' cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' style='padding:0px;margin:0px;'>
<?php if (count($product["products"]) > 1): ?>
<strong><?php print translate("Best Price"); ?>:</strong>
<?php else: ?>
<strong><?php print translate("Price"); ?>:</strong>
<?php endif; ?>
<?php print $config_currencyHTML.$mainProduct["price"]; ?> <?php print translate("from"); ?>
</td>
<td valign='top' style='padding:0px;margin:0px;'>
<?php foreach($product["products"] as $priceProduct): ?>
<?php
$numProducts = count($product["products"]);
if ($numProducts > 1)
{
$saving = $product["products"][($numProducts-1)]["price"] - $product["products"][0]["price"];
}
if ($saving)
{
print "Save Up to £".$saving." on the ".$product["products"][0]["name"]." By Comparing on ThepriceSite.co.uk";
}
?>
<?php if ($priceProduct["price"] == $mainProduct["price"]): ?>
<a href='<?php print tapestry_buyURL($priceProduct); ?>' <?php print javascript_statusBar(translate("go to")." ".$priceProduct["merchant"]); ?>><strong><?php print $priceProduct["merchant"]; ?></strong></a><br />
<?php else: ?>
<?php break; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php if (isset($mainProduct["extraHTML"])) print $mainProduct["extraHTML"]; ?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
Michael @ ThePriceSite
(added code tags)
Hi Michael,
The code looks OK but it's not quite in the right place - you are inside a loop there so things may go a bit awry.
The following block works OK:
<?php
$numProducts = count($product["products"]);
if ($numProducts > 1)
{
$saving = $product["products"][($numProducts-1)]["price"] - $product["products"][0]["price"];
}
if ($saving)
{
print "Save Up to £".sprintf("%.2f",$saving)." on the ".$product["products"][0]["name"]." By Comparing on ThepriceSite.co.uk";
}
?>
Insert this into html/product.php 4 lines up from the bottom, so immediately afterwards you just have the following:
</td>
</tr>
</table>
</div>
That should do the trick...
Cheers,
David.
Hi Dave
was wondering is there a way to limit this to 2 decimel places, a few products have price differences link £0.0099999999999999999999999999 and things like that
Thanks
phil
Hi Phil,
Yes - that's a good point. Instead of just:
$saving
...within the print statement, use:
sprintf("%.2f",$saving)
...so it looks like this;
print "Save Up to £".sprintf("%.2f",$saving)." on the.....
(i've corrected this in the code above as well)
Cheers,
David.
David i did as you recommended with the below code - but had an error message.
<div class='product'>
<?php $mainProduct = $product["products"][0]; ?>
<table width='100%'>
<tr>
<td width='200'>
<?php if ($mainProduct["image_url"]): ?>
<img width='180' src='<?php print $mainProduct["image_url"]; ?>' alt='<?php print $mainProduct["name"]; ?>' />
<?php endif; ?>
</td>
<td valign='top'>
<h3><a href='<?php print tapestry_buyURL($mainProduct); ?>' <?php print javascript_statusBar(translate("go to")." ".$mainProduct["merchant"]); ?>><?php print $mainProduct["name"]; ?></a></h3>
<?php if ($mainProduct["description"]): ?>
<p><?php print $mainProduct["description"]; ?></p>
<?php endif; ?>
<table border='0' cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' style='padding:0px;margin:0px;'>
<?php if (count($product["products"]) > 1): ?>
<strong><?php print translate("Best Price"); ?>:</strong>
<?php else: ?>
<strong><?php print translate("Price"); ?>:</strong>
<?php endif; ?>
<?php print $config_currencyHTML.$mainProduct["price"]; ?> <?php print translate("from"); ?>
</td>
<td valign='top' style='padding:0px;margin:0px;'>
<?php foreach($product["products"] as $priceProduct): ?>
<?php if ($priceProduct["price"] == $mainProduct["price"]): ?>
<a href='<?php print tapestry_buyURL($priceProduct); ?>' <?php print javascript_statusBar(translate("go to")." ".$priceProduct["merchant"]); ?>><strong><?php print $priceProduct["merchant"]; ?></strong></a><br />
<?php else: ?>
<?php break; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php if (isset($mainProduct["extraHTML"])) print $mainProduct["extraHTML"]; ?>
</td>
</tr>
</table>
<?php
$numProducts = count($product["products"]);
if ($numProducts > 1)
{
$saving = $product["products"][($numProducts-1)]["price"] - $product["products"][0]["price"];
}
if ($saving)
{
print "Save Up to £".sprintf("%.2f",$saving)." on the ".$product["products"][0]["name"]." By Comparing on ThepriceSite.co.uk";
}
?>
</td>
</tr>
</table>
</div>
error message is
<a href='
Fatal error: Call to undefined function: tapestry_buyurl() in /home/t/h/thepricesite_co_uk/html/product1.php on line 11
Michael @ ThePriceSite
Hi Michael,
Your code runs fine on my server - note that you won't be able to browse to product1.php directly because the script relies on the include files having been included earlier.
Are you still getting the error?
"Shouldn't be too tricky; but will need a bit of case handling to deal with the situation where:
i) There is only 1 product
ii) There is no price difference"
What would I need to add and where?
Eddie
Hi Eddie,
Those cases are handled in the code provided above. The first section performs the calculation (only if there is more than one merchant); and the second section displays the saving only if there is a saving - so it's all taken care of.
Cheers,
David.
Hi David
Was wondering what snip of code would i need to change the text to
eg. Buy Miele KFN8762SDed Today and Save Up to £201.70 by Comparing 13 Online Stores at www.buy24-7.net
any code i use does not print the number of stores on the product page, but yet i can get it to work on the search page?
look forward to hearing your answer
thanks
Phil
Hi Phil,
What code are you using to get the number of stores? On the products page (products.php) you can use the following:
count($product["products"])
Cheers,
David.
Shouldn't be too tricky; but will need a bit of case handling to deal with the situation where:
i) There is only 1 product
ii) There is no price difference
Within the product display HTML in html/product.php, all the products in the result set are in the array $product["products"].
To get the number of items in an array, you can use the count() function, so to get the number of products and then get the price difference you could do something like this:
<?php
$numProducts = count($product["products"]);
if ($numProducts > 1)
{
$saving = $product["products"][($numProducts-1)]["price"] - $product["products"][0]["price"];
}
if ($saving)
{
print "Save Up to £".$saving." on the ".$product["products"][0]["name"]." By Comparing on buy24-7.net"
}
?>
should work....!
Cheers,
David.