Math unable to account for 1000??

Please support our PHP advertiser: 50% off 6 Months Dedicated Server Hosting from 1&1!
Reply

Join Date: Apr 2009
Posts: 24
Reputation: kssi89 is an unknown quantity at this point 
Solved Threads: 0
kssi89 kssi89 is offline Offline
Newbie Poster

Math unable to account for 1000??

 
0
  #1
May 1st, 2009
This was mentioned in a previous thread but I am thinking this problem may warrant a new one. The code I am working with here:

  1. <?php
  2. //connect to the database
  3. $connect = mysql_connect("localhost", "root", "password") or
  4. die ("Hey loser, check your server connection.");
  5.  
  6. mysql_select_db("ecommerce");
  7. ?>
  8. <html>
  9. <head>
  10. <title>Here is Your Shopping Cart!</title>
  11. </head>
  12. <body>
  13. <dev align="center">
  14. You currently have
  15.  
  16. <?php
  17. $sessid = session_id();
  18.  
  19. //display number of products in cart
  20. $query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";
  21. $results = mysql_query($query)
  22. or die (mysql_query());
  23. $rows = mysql_num_rows($results);
  24. echo $rows;
  25. ?>
  26.  
  27. product(s) in your cart.<br>
  28.  
  29. <table border="1" align="center" cellpadding="5">
  30. <tr>
  31. <td>Quantity</td>
  32. <td>Item Image</td>
  33. <td>Item Name</td>
  34. <td>Price Each</td>
  35. <td>Extended Price</td>
  36. <td></td>
  37. <td></td>
  38. <?php
  39. $total = "0";
  40.  
  41. echo"<tr>";
  42. while ($row = mysql_fetch_array($results)) {
  43. extract($row);
  44. $prod = "SELECT * FROM products " .
  45. "WHERE products_prodnum='$carttemp_prodnum'";
  46. $prod2 = mysql_query($prod);
  47. $prod3 = mysql_fetch_array($prod2);
  48. extract ($prod3);
  49. echo "<td>
  50. <form method=\"POST\" action=\"modcart.php?action=change\">
  51. <input type=\"hidden\" name=\"modified_hidden\"
  52. value=\"$carttemp_hidden\">
  53. <input type=\"text\" name=\"modified_quan\" size=\"2\"
  54. value=\"$carttemp_quan\">";
  55. echo "</td>";
  56. echo "<td>";
  57. echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";
  58. echo "THUMBNAIL<br>IMAGE</a></td>";
  59. echo "<td>";
  60. echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";
  61. echo $products_name;
  62. echo "</a></td>";
  63. echo "<td align=\"right\">";
  64. //changed to number format instead of just var april 29 2009
  65. echo number_format($products_price, 2);
  66. echo "</td>";
  67. echo "<td align=\"right\">";
  68. //get extended price
  69. $extprice = number_format($products_price * $carttemp_quan, 2);
  70. echo $extprice;
  71. echo "</td>";
  72. echo "<td>";
  73. echo "<input type=\"submit\" name=\"Submit\"
  74. value=\"Change Qty\">
  75. </form></td>";
  76. echo "<td>";
  77. echo "<form method=\"POST\" action=\"modcart.php?action=delete\">
  78. <input type=\"hidden\" align=\"center\" name=\"modified_hidden\"
  79. value=\"$carttemp_hidden\">";
  80. echo "<input type=\"submit\" name=\"Submit\"
  81. value=\"Delete Item\">
  82. </form></td>";
  83. echo "</tr>";
  84. //add extended price to total
  85. $total = $extprice + $total;
  86.  
  87. }
  88. ?>
  89. <tr>
  90. <td colspan=\"4\" align=\"right\">
  91. Your total before shipping is:</td>
  92. <td align=\"right\"> <?php echo number_format($total, 2); echo "@ $total"; ?></td>
  93. <td></td>
  94. <td>
  95. <?php // removed space from value below
  96. echo "<form method=\"POST\" action=\"modcart.php?action=empty\">
  97. <input type=\"hidden\" name=\"carttemp_hidden\
  98. value=\"";
  99. if (isset($carttemp_hidden)) {
  100. echo $carttemp_hidden;
  101. }
  102.  
  103. echo "\">";
  104. echo "<input type=\"submit\" name=\"Submit\" value=\"Empty Cart\">
  105. </form>";
  106. ?>
  107.  
  108. </tr>
  109. </table>
  110. <form method="POST" action="checkout.php">
  111. <input type="submit" name="Submit" value="Proceed to Checkout">
  112. </form>
  113. <a href="shop.php">Go back to the main page</a>
  114. </div>
  115. </body>
  116. </html>

The $total can account for items written like this:

item 1 - $900.00
(table row)
item 1 - $900.00
(table row)
total - 1800.00

item 2 - $50.00 x19 - ExtPrice: 950.00
total - 950.00


But not like this:

item 1 - $900.00 x2 - ExtPrice: 1800.00
total = 1.00

item 1 - $900.00 x3 - ExtPrice: 2700.00
total = 2.00

---

I am about at the end of my rope with this, do you php gurus have any suggestions?
Last edited by kssi89; May 1st, 2009 at 10:58 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 169
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 19
Fest3er Fest3er is offline Offline
Junior Poster

Re: Math unable to account for 1000??

 
0
  #2
May 1st, 2009
May I suggest keeping all numbers as true numbers? Only convert them to formatted strings when you display them to the user. You might be mixing numbers and strings in your arithmetic operations--strings don't always convert nicely to numbers.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,558
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 206
Sponsor
ardav's Avatar
ardav ardav is offline Offline
Anthrax Poster

Re: Math unable to account for 1000??

 
0
  #3
May 1st, 2009
Second that. The numbers returned for $total seem to be from the first non-zero 'integer' part of the price. Keep prices as float datatype in your DB. I assume you have it as varchar to include the '$'? If so, I think php is struggling to understand the 'price' variable as a float.

Hold on, just though of something - does it happen when you have a price of below $100 with a multiplier of >10 (to force a total of >$1000)? If not, your problem is definitely the price (and possibly the use of numberformat with it).
Last edited by ardav; May 1st, 2009 at 5:52 pm. Reason: made a boob
Twpsyn cythraul. Cawr y dom tarw. Gwybod dim, gofyn dim.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,797
Reputation: almostbob is a jewel in the rough almostbob is a jewel in the rough almostbob is a jewel in the rough 
Solved Threads: 213
almostbob's Avatar
almostbob almostbob is online now Online
Posting Virtuoso

Re: Math unable to account for 1000??

 
0
  #4
May 1st, 2009
[Rant = Slightly off topic, My favorite Subject]

simplify the database
change any text dates "October 15 2008"(smalltext 15 characters) etc to timestamps
strip out the $signs
you only need them when you print out information
echo '$ '.$extPrice; is just as fast and causes no $_ errors
the database should contain
no formatting
no text dates or times
nothing but data in its simplest form,
its easier to manipulate, smaller, faster, cheaper to maintain
and you can calculate on the fields, which is more difficult to do with text representations,
not so much now for 50 items, 50Bytes extra per row isnt much
but when the db grows to 500 000 items and there are millions of transactions in the transaction table, 50Bytes per row is a lot

[/Rant]
Last edited by almostbob; May 1st, 2009 at 10:26 pm.
Failure is not an option It's included free, you don't have to do anything to get it
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it

Please mark solved problems, solved
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum


Views: 390 | Replies: 3
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC