tobeyt23 Posted April 30, 2007 Share Posted April 30, 2007 I am looping a database and getting product pricing and markup to get the actual cost of an item. All seems to work well until I get to something that cost over a thousand. Any suggestions: while ($datarow = mysql_fetch_assoc($result3)) { $markup = $datarow['price']*$datarow['markup']/100; $prodcost = number_format($datarow['price']+$markup,2); } //This is where it breaks!!!! $prod_cost += $prodcost; Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/ Share on other sites More sharing options...
AndyB Posted April 30, 2007 Share Posted April 30, 2007 define what you mean by 'breaks'. Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/#findComment-241475 Share on other sites More sharing options...
tobeyt23 Posted April 30, 2007 Author Share Posted April 30, 2007 it does not show the correct total, it comes back with 2 instead of 2,407.92 Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/#findComment-241476 Share on other sites More sharing options...
tobeyt23 Posted April 30, 2007 Author Share Posted April 30, 2007 Do i need to do some sort of conversion on the variable $prodcost? Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/#findComment-241485 Share on other sites More sharing options...
obsidian Posted April 30, 2007 Share Posted April 30, 2007 First question, and sorry if it's incredibly obvious, but you do realize you're playing with two separate variables, right? ($prod_cost and $prodcost). Second question, can you explain what it is you're attempting to do? At first glance, it seems as though your addition should be included within your while() statement, but without fully understanding what you're attempting, I don't want to make that call. Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/#findComment-241515 Share on other sites More sharing options...
tobeyt23 Posted April 30, 2007 Author Share Posted April 30, 2007 Yes I understand (or i think i do), i am attempting to create a new variable $prod_cost based on the $prodcost which may return more than one. I am taking the product based on its quantity and getting the price. Then based on the user logged in account type getting the markup. Then that gives me the total price. complete code: $query3 = "SELECT product_pricing.price, pricing_markup.markup FROM product_pricing JOIN pricing_markup ON product_pricing.product_id=pricing_markup.product_id WHERE product_pricing.product_id=".$sqldatarow['product_id']." AND pricing_markup.account_type='".$_SESSION['eshop_account_type']."' AND product_pricing.quantity=".$sqldatarow['quantity'].""; if (mysql_connect($defaults["db_server"], $defaults["db_username"], $defaults["db_password"])) { if (mysql_select_db($defaults["db_database"])) { if ($result3 = mysql_query($query3)) { if (mysql_num_rows($result3) > 0) { while ($datarow = mysql_fetch_assoc($result3)) { $markup = $datarow['price']*$datarow['markup']/100; $prodcost = number_format($datarow['price']+$markup,2); } } mysql_free_result($result3); } else { die ("<h3>SQL Error #".mysql_errno()."</h3>\n<h4>".mysql_error()."</h4>\n<h4>Line ".__LINE__." in ".__FILE__."</h4>\n"); } } else { die ("<h3>SQL Error #".mysql_errno()."</h3>\n<h4>".mysql_error()."</h4>\n<h4>Line ".__LINE__." in ".__FILE__."</h4>\n"); } } else { die ("<h3>SQL Error #".mysql_errno()."</h3>\n<h4>".mysql_error()."</h4>\n<h4>Line ".__LINE__." in ".__FILE__."</h4>\n"); } //$prodcost = money_format('2,407.92'); //echo "".$prodcost."\n"; $prod_cost += $prodcost; //echo $prod_cost; //exit; } Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/#findComment-241552 Share on other sites More sharing options...
obsidian Posted April 30, 2007 Share Posted April 30, 2007 OK, try this out... First off, you need to define your $prod_cost variable before you try to add to it, or you may have problems. Also, you need to be adding to it within your loop to be sure that all records are added successfully. Try something like this as a test: <?php if (mysql_num_rows($result3) > 0) { $prod_cost = 0; while ($datarow = mysql_fetch_assoc($result3)) { $markup = $datarow['price']*$datarow['markup']/100; $prodcost = number_format($datarow['price']+$markup,2); $prod_cost += $prodcost; } } echo $prod_cost; ?> Use this within your query as you have it and see if that works out any better. Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/#findComment-241570 Share on other sites More sharing options...
tobeyt23 Posted April 30, 2007 Author Share Posted April 30, 2007 No getting same results Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/#findComment-241590 Share on other sites More sharing options...
kenrbnsn Posted April 30, 2007 Share Posted April 30, 2007 You're problem is occurring because of these lines: <?php $prodcost = number_format($datarow['price']+$markup,2); } //This is where it breaks!!!! $prod_cost += $prodcost; ?> The number_format() function returns a string which may have commas in it (if the value input to the function is > 999) You can not add a string to a number. Remove the number_format function in you loop and only use it when you want to display the final result. Ken Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/#findComment-241711 Share on other sites More sharing options...
tobeyt23 Posted April 30, 2007 Author Share Posted April 30, 2007 Thank you so much that is right! Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/#findComment-241728 Share on other sites More sharing options...
obsidian Posted April 30, 2007 Share Posted April 30, 2007 The number_format() function returns a string which may have commas in it (if the value input to the function is > 999) You can not add a string to a number. Remove the number_format function in you loop and only use it when you want to display the final result. Ken *DOH* - thanks for the cleanup, Ken... completely missed that one Link to comment https://forums.phpfreaks.com/topic/49282-solved-math-help/#findComment-241731 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.