Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 5th, 2010, 04:03 AM
Registered User
 
Join Date: Mar 2010
Posts: 1
Thanks: 2
Thanked 0 Times in 0 Posts
Default Summing up errror

Hi everyone,
I am a biginner to PHP and I've tried to write code for online store. I got problem with the summing of variables where the sum is correct only if the value less than 1000, but when the sum value more than 1000, it gives wrong answer such as if the summation value is 1,200, the answer given is only 1 but if the summation value is 120, the answer is correct 120. It is kind of weird, because the summation is actually works..Please anybody can help, I would appreciate very much...
Here is the code...

<table align="center">
<td>
<table border="1" align="left" cellpadding="5">
<tr>
<td>Quantity</td>
<td>Item Image</td>
<td>Item Name</td>
<td>Price Each</td>
<td>Unit</td>
<td>Extended Price</td>
<td></td>
</tr>
<?php
$sessid = session_id();
$query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";
$results = mysql_query($query)
or die (mysql_query());
$total = 0;
while ($row = mysql_fetch_array($results)) {
extract($row);
$prod = "SELECT * FROM products WHERE
products_prodnum = '$carttemp_prodnum'";
$prod2 = mysql_query($prod);
$prod3 = mysql_fetch_array($prod2);
extract($prod3);
echo "<tr><td>";
echo $carttemp_quan;
echo "</td>";
echo "<td>";
echo "<a href=\"getprod.php?prodid=" .
$products_prodnum . "\">";
echo '<IMG SRC="',$products_image,
'" WIDTH="100" HEIGHT="80" BORDER="0" ALT=""/>';
echo "</a></td> ";
echo "<td>";
echo "<a href=\"getprod.php?prodid=" .
$products_prodnum . "\">";
echo $products_name;
echo "</td></a>";
echo "<td align=\"right\">";
echo $products_price;
echo "</td>";
echo "<td align=\"center\">";
echo $products_unit;
echo "</td>";
echo "<td align=\"right\">";
//get extended price
$extprice = number_format($products_price * $carttemp_quan, 2);
echo $extprice;
echo "</td>";
echo "<td>";
echo "<a href=\"cart.php\">Make Changes to Cart</a>";
echo "</td>";
//add extended price to total
$total += $extprice;
$total2 = number_format($extprice,2);
echo "<td>";
echo $total2;
echo "</td>";
echo "</tr>";
}
?>
<tr>
<td colspan="5" align="right">Your total before shipping is:</td>
<td align="right"> <?php echo number_format($total, 2); ?></td>
<td></td>
<td></td>
</tr>
</table>
 
Old March 13th, 2010, 10:32 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default mixed string and number

That's because you mixed string and number. I will use the following example to explain:
Code:
<html>
<body>
<?php
	$total = 10;
	$extprice = number_format(1000, 2);
	echo "<p>extprice = " . $extprice . "</p>";
	echo "<p>convert to float, extprice = " . (float)$extprice . "</p>";
	$total += $extprice;
	echo "<p>total = " . $total . "</p>";
?>
</body>
</html>
Since your $extprice is the result returned from number_format(), it is a string, when you later involve it in summation, it needs to be first converted from string to float, which in above case, it is converted to 1. That's why you had problem with numbers greater than 1000.
The Following 2 Users Say Thank You to PeterPeiGuo For This Useful Post:
ditr (March 19th, 2010), zary (March 14th, 2010)
 
Old March 13th, 2010, 10:40 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

The following shows you couple of examples how string is converted to number. It does a greedy conversion from the left most character up to the first character that cannot be converted - in the case of number3, it is converted to 0, as the left most character cannot be converted and the conversion simply stops there.
Code:
<html>
<body>
<?php
	$number1 = (float)"12,234.56";
	$number2 = (float)"23,456,789.12";
	$number3 = (float)"$23,456,789.12";
	echo "<p>number 1 = " . $number1 . "</p>";
	echo "<p>number 2 = " . $number2 . "</p>";
	echo "<p>number 3 = " . $number3 . "</p>";
?>
</body>
</html>
The Following User Says Thank You to PeterPeiGuo For This Useful Post:
zary (March 14th, 2010)





Similar Threads
Thread Thread Starter Forum Replies Last Post
errror connecting to database prrao C# 2005 1 June 25th, 2007 05:14 AM
Summing values swwallace XSLT 4 March 4th, 2006 12:13 PM
Summing up in a loop Tschuri XSLT 0 May 3rd, 2005 07:16 AM
OpenReport Action Cancelled errror in Access 2003 JJ Access VBA 1 August 20th, 2004 09:27 AM
Summing and Exporting imhotep666 Excel VBA 3 August 18th, 2004 11:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.