dcuellar Posted January 29, 2008 Share Posted January 29, 2008 I want to be able to display the amount like this $7.00. Instead it is displaying like this 7. This is the code in my PHP script: $cartplog['product_price'] = round(floatval($cartplog_product_info['price']), 2); Am I looking at the right code? How would I change this to reflect the desired result? Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/ Share on other sites More sharing options...
rhodesa Posted January 29, 2008 Share Posted January 29, 2008 Checkout http://us.php.net/number_format <?php echo number_format(7,2); ?> Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452360 Share on other sites More sharing options...
dcuellar Posted January 29, 2008 Author Share Posted January 29, 2008 Where would I insert this? sorry, I really am new at this. Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452368 Share on other sites More sharing options...
rhodesa Posted January 29, 2008 Share Posted January 29, 2008 <?php $cartplog['product_price'] = '$' . number_format($cartplog_product_info['price']), 2); ?> This will print prices in a USD format. Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452372 Share on other sites More sharing options...
cooldude832 Posted January 29, 2008 Share Posted January 29, 2008 there is a money_format too and you can define a default currency in php 5 too Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452373 Share on other sites More sharing options...
rhodesa Posted January 29, 2008 Share Posted January 29, 2008 wow...i guess you really do learn something new everyday Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452378 Share on other sites More sharing options...
dcuellar Posted January 29, 2008 Author Share Posted January 29, 2008 rhodesa, I put that info in there and came out with a blank page on the website. Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452380 Share on other sites More sharing options...
rhodesa Posted January 29, 2008 Share Posted January 29, 2008 oops...extra parenthesis in there: <?php $cartplog['product_price'] = '$' . number_format($cartplog_product_info['price'], 2); ?> Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452382 Share on other sites More sharing options...
dcuellar Posted January 29, 2008 Author Share Posted January 29, 2008 Very Nice! Perfect. Thanks a bunch rhodesa!! This website has been a blessing thus far. This Friday I will be making my first donation. Thanks again!! Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452391 Share on other sites More sharing options...
dcuellar Posted January 29, 2008 Author Share Posted January 29, 2008 Ok. Sorry to bring this back up, but I'm getting an error. When I click to view cart it is showing a 0 for Total price. With the original code I got a price of 7, a quantity of 1, and a total of 7 USD: if ($cartplog_basket_check['products']) { $cartplog_basket_check['products'] = unserialize($cartplog_basket_check['products']); if (!is_array($cartplog_basket_check['products'])) { $cartplog_basket_check['products'] = array(); } foreach($cartplog_basket_check['products'] AS $cartplog_basket_productid => $cartplog_basket_array) { $cartplog['basket_productid'] = intval($cartplog_basket_productid); $cartplog['basket_quantity'] = intval($cartplog_basket_array['quantity']); $cartplog['basket_thumb'] = strval($cartplog_basket_array['thumb']); $cartplog['basket_title'] = htmlspecialchars_uni($cartplog_basket_array['title']); $cartplog['basket_price'] = round(floatval($cartplog_basket_array['price']), 2); $cartplog['basket_options'] = nl2br(htmlspecialchars_uni($cartplog_basket_array['options'])); $cartplog['basket_lineprice'] = round($cartplog['basket_quantity'] * $cartplog['basket_price'], 2); $cartplog['basket_total'] += $cartplog['basket_lineprice']; eval('$cartplog[\'basket_items\'] .= "' . fetch_template('cartplog_basket_item') . '";'); But when I make the change to $cartplog['basket_price'] I get a price of $7.00, a quantity of 1, and a total price of 0 USD. This is the change I made: if ($cartplog_basket_check['products']) { $cartplog_basket_check['products'] = unserialize($cartplog_basket_check['products']); if (!is_array($cartplog_basket_check['products'])) { $cartplog_basket_check['products'] = array(); } foreach($cartplog_basket_check['products'] AS $cartplog_basket_productid => $cartplog_basket_array) { $cartplog['basket_productid'] = intval($cartplog_basket_productid); $cartplog['basket_quantity'] = intval($cartplog_basket_array['quantity']); $cartplog['basket_thumb'] = strval($cartplog_basket_array['thumb']); $cartplog['basket_title'] = htmlspecialchars_uni($cartplog_basket_array['title']); $cartplog['basket_price'] = '$' . number_format($cartplog_basket_array['price'], 2); $cartplog['basket_options'] = nl2br(htmlspecialchars_uni($cartplog_basket_array['options'])); $cartplog['basket_lineprice'] = round($cartplog['basket_quantity'] * $cartplog['basket_price'], 2); $cartplog['basket_total'] += round(floatval($cartplog['basket_lineprice']), 2); eval('$cartplog[\'basket_items\'] .= "' . fetch_template('cartplog_basket_item') . '";'); I need it to show a price of $7.00, a quantity of 1 and a total of $7.00 USD. Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452497 Share on other sites More sharing options...
rhodesa Posted January 29, 2008 Share Posted January 29, 2008 The change SHOULD be made in the template instead of this code. But this should also make it work: if ($cartplog_basket_check['products']) { $cartplog_basket_check['products'] = unserialize($cartplog_basket_check['products']); if (!is_array($cartplog_basket_check['products'])) { $cartplog_basket_check['products'] = array(); } foreach($cartplog_basket_check['products'] AS $cartplog_basket_productid => $cartplog_basket_array) { $cartplog['basket_productid'] = intval($cartplog_basket_productid); $cartplog['basket_quantity'] = intval($cartplog_basket_array['quantity']); $cartplog['basket_thumb'] = strval($cartplog_basket_array['thumb']); $cartplog['basket_title'] = htmlspecialchars_uni($cartplog_basket_array['title']); $cartplog['basket_price'] = round(floatval($cartplog_basket_array['price']), 2); $cartplog['basket_options'] = nl2br(htmlspecialchars_uni($cartplog_basket_array['options'])); $cartplog['basket_lineprice'] = round($cartplog['basket_quantity'] * $cartplog['basket_price'], 2); $cartplog['basket_total'] += $cartplog['basket_lineprice']; //Fix Basketprice $cartplog['basket_price'] = '$' . number_format($cartplog['basket_price'], 2); eval('$cartplog[\'basket_items\'] .= "' . fetch_template('cartplog_basket_item') . '";'); Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452587 Share on other sites More sharing options...
dcuellar Posted January 29, 2008 Author Share Posted January 29, 2008 It's now actually giving me an amount, but the total line amount is not using the $xx.xx format. Check it out: http://www.unifiedchamp.com/ucforum/cartplog.php?do=viewcart It's not a big deal though. I appreciate all your help. Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452626 Share on other sites More sharing options...
rhodesa Posted January 29, 2008 Share Posted January 29, 2008 after //Fix Basketprice $cartplog['basket_price'] = '$' . number_format($cartplog['basket_price'], 2); add this line: $cartplog['basket_lineprice'] = '$' . number_format($cartplog['basket_lineprice'], 2); Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452641 Share on other sites More sharing options...
dcuellar Posted January 29, 2008 Author Share Posted January 29, 2008 Yep. That did it. Very nice! Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/88391-solved-pricing-in-php/#findComment-452652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.