martinlucas Posted February 19, 2009 Share Posted February 19, 2009 i have built a website and a developer friend of mine built a very simple paypal shopping cart for me. http://www.fabricrehab.co.uk there is a problem with the shopping cart, if a product is £3.70 and the quantity is 1, the total for that will show as £3.7 - not £3.70, if the product price is multiplied by 10, for example if the price for a single unit is £2.99 the the total will display as £29.9 - not £29.90. here's the part of the include for the table row which displays the product that is in the cart, then takes the quantity in the text box multiplies this and displays it as the total. foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM products WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td width="45px"><img src="images/fabric/thumb/'.$thumb.'" width="45px" height="45px"></td>'; $output[] = '<td class="carttext" align="center"><a href="cart.php?action=delete&id='.$id.'" class="r"><img src="images/deletecart.gif" border="0"></a></td>'; $output[] = '<td class="carttext">'.$name.'</td>'; $output[] = '<td class="carttext">£'.$price.'</td>'; $output[] = '<td class="carttext" align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td class="carttext" align="right">£'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '</tr>'; } i do hope all that makes sense. i guess i just need a way of formatting the part '.($price * $qty).' with 2 decimal places at all times. thanks in advance for your help and *fingers crossed* solutions. Link to comment https://forums.phpfreaks.com/topic/145953-issue-with-displaying-price-on-shopping-cart/ Share on other sites More sharing options...
milesap Posted February 19, 2009 Share Posted February 19, 2009 If you're getting the price from a database, ensure the price field is set to 'float' and will therefore hold 2 decimal places. Link to comment https://forums.phpfreaks.com/topic/145953-issue-with-displaying-price-on-shopping-cart/#findComment-766230 Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 You can look into the number_format() function. Link to comment https://forums.phpfreaks.com/topic/145953-issue-with-displaying-price-on-shopping-cart/#findComment-766231 Share on other sites More sharing options...
martinlucas Posted February 19, 2009 Author Share Posted February 19, 2009 @milesp I know this is really bad, but the price field is actually text - i had some problems writing prices with .00 decimal points to the database using integers via dreamweaver and had to change it to text - which seems to work, as you can see, the line $output[] = '<td class="carttext">£'.$price.'</td>'; spits out a properly formatted price. @allworknoplay I did start to have a look at this, but I'm below a beginner at php and couldn't get my head around the exact formatting - nothing that i tried actually worked. Link to comment https://forums.phpfreaks.com/topic/145953-issue-with-displaying-price-on-shopping-cart/#findComment-766261 Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 No problem here are some examples: <?php $price = "1.2566"; $price2 = "1.2"; echo number_format($price,2); echo number_format($price2,2); ?> You will get: 1.25 and 1.20 respectively....so you will always get the 2 decimal places to the right that you are looking for.. Please mark as SOLVED. Link to comment https://forums.phpfreaks.com/topic/145953-issue-with-displaying-price-on-shopping-cart/#findComment-766265 Share on other sites More sharing options...
martinlucas Posted February 20, 2009 Author Share Posted February 20, 2009 i need my hand held for just a little bit longer. (i think) because this is an include, the formatting of the php is a little different to your example. how do i wrap the number_format around the following; '.($price * $qty).' i've tried; 'number_format(.($price * £qty).,2)' but that didn't work - it screwed up the whole page - the cart no longer showed up! Link to comment https://forums.phpfreaks.com/topic/145953-issue-with-displaying-price-on-shopping-cart/#findComment-766886 Share on other sites More sharing options...
martinlucas Posted February 22, 2009 Author Share Posted February 22, 2009 bumping this back to the top hoping for just a little more help with the exact formatting for the number_function() code. i've got a client with holding their invoice payment until this is fixed so it's really important. Link to comment https://forums.phpfreaks.com/topic/145953-issue-with-displaying-price-on-shopping-cart/#findComment-768671 Share on other sites More sharing options...
allworknoplay Posted February 23, 2009 Share Posted February 23, 2009 i need my hand held for just a little bit longer. (i think) because this is an include, the formatting of the php is a little different to your example. how do i wrap the number_format around the following; '.($price * $qty).' i've tried; 'number_format(.($price * £qty).,2)' but that didn't work - it screwed up the whole page - the cart no longer showed up! Do it like this: $final_price = number_format(($price * £qty),2); Link to comment https://forums.phpfreaks.com/topic/145953-issue-with-displaying-price-on-shopping-cart/#findComment-769236 Share on other sites More sharing options...
martinlucas Posted February 24, 2009 Author Share Posted February 24, 2009 thanks for your continued help, but that didn't work. i amended the code as per your example; $rowtotal += number_format(($price * $qty),2); $output[] = '<td class="carttext" align="right">£'.$rowtotal.'</td>'; but it still shows up as £6.5 rather than £6.50. Link to comment https://forums.phpfreaks.com/topic/145953-issue-with-displaying-price-on-shopping-cart/#findComment-769922 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.