| | |
Math unable to account for 1000??
![]() |
•
•
Join Date: Apr 2009
Posts: 24
Reputation:
Solved Threads: 0
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:
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?
PHP Syntax (Toggle Plain Text)
<?php //connect to the database $connect = mysql_connect("localhost", "root", "password") or die ("Hey loser, check your server connection."); mysql_select_db("ecommerce"); ?> <html> <head> <title>Here is Your Shopping Cart!</title> </head> <body> <dev align="center"> You currently have <?php $sessid = session_id(); //display number of products in cart $query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'"; $results = mysql_query($query) or die (mysql_query()); $rows = mysql_num_rows($results); echo $rows; ?> product(s) in your cart.<br> <table border="1" align="center" cellpadding="5"> <tr> <td>Quantity</td> <td>Item Image</td> <td>Item Name</td> <td>Price Each</td> <td>Extended Price</td> <td></td> <td></td> <?php $total = "0"; echo"<tr>"; 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 "<td> <form method=\"POST\" action=\"modcart.php?action=change\"> <input type=\"hidden\" name=\"modified_hidden\" value=\"$carttemp_hidden\"> <input type=\"text\" name=\"modified_quan\" size=\"2\" value=\"$carttemp_quan\">"; echo "</td>"; echo "<td>"; echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">"; echo "THUMBNAIL<br>IMAGE</a></td>"; echo "<td>"; echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">"; echo $products_name; echo "</a></td>"; echo "<td align=\"right\">"; //changed to number format instead of just var april 29 2009 echo number_format($products_price, 2); echo "</td>"; echo "<td align=\"right\">"; //get extended price $extprice = number_format($products_price * $carttemp_quan, 2); echo $extprice; echo "</td>"; echo "<td>"; echo "<input type=\"submit\" name=\"Submit\" value=\"Change Qty\"> </form></td>"; echo "<td>"; echo "<form method=\"POST\" action=\"modcart.php?action=delete\"> <input type=\"hidden\" align=\"center\" name=\"modified_hidden\" value=\"$carttemp_hidden\">"; echo "<input type=\"submit\" name=\"Submit\" value=\"Delete Item\"> </form></td>"; echo "</tr>"; //add extended price to total $total = $extprice + $total; } ?> <tr> <td colspan=\"4\" align=\"right\"> Your total before shipping is:</td> <td align=\"right\"> <?php echo number_format($total, 2); echo "@ $total"; ?></td> <td></td> <td> <?php // removed space from value below echo "<form method=\"POST\" action=\"modcart.php?action=empty\"> <input type=\"hidden\" name=\"carttemp_hidden\ value=\""; if (isset($carttemp_hidden)) { echo $carttemp_hidden; } echo "\">"; echo "<input type=\"submit\" name=\"Submit\" value=\"Empty Cart\"> </form>"; ?> </tr> </table> <form method="POST" action="checkout.php"> <input type="submit" name="Submit" value="Proceed to Checkout"> </form> <a href="shop.php">Go back to the main page</a> </div> </body> </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.
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).
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.
[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
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]
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 $_ errorsthe 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
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
![]() |
Similar Threads
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
Other Threads in the PHP Forum
- Previous Thread: pegination
- Next Thread: New structure for PHP pages
Views: 390 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for PHP
access ajax apache array beginner box buttons cakephp check checkbox class cms code cookies database date delete directory display download dropdown drupal dynamic echo email error file files folder form forms function functions header hosting href htaccess html image images include insert ip java javascript joomla jquery limit link list login loop mail menu methods mlm mod_rewrite multiple mysql order parse password paypal php problem query radio random redirect regex remote results script search security select server session sessions shopping soap sort sorting source sql string system table unicode update upload url user validation variable video web website wordpress xml






