TGM Posted May 4, 2009 Share Posted May 4, 2009 referring again with to the restaurant db. I want to calculate the total price for the order. how can I calculate the total price of an order..I know its 'amount' x 'quantity' but how to code this.. Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/ Share on other sites More sharing options...
premiso Posted May 4, 2009 Share Posted May 4, 2009 $total = ($amount * $quantity); Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-826187 Share on other sites More sharing options...
corbin Posted May 4, 2009 Share Posted May 4, 2009 premiso, why the parenthesis? Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-826311 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 premiso, why the parenthesis? Didn't you get the memo? It's the new trend going around. Everyone's doing it nowadays. People started: $query = ("SELECT * FROM table"); Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-826319 Share on other sites More sharing options...
DarkWater Posted May 5, 2009 Share Posted May 5, 2009 (let ((amount 5.00) (quantity 3)) (* amount quantity)) Ahhhhhhhhhhhh! Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-826410 Share on other sites More sharing options...
premiso Posted May 5, 2009 Share Posted May 5, 2009 premiso, why the parenthesis? I always do math operations inside parans. Just a habit. Even in my math classes since I have learned what the parans did and got marked for not using them I just do it. Order of Operations is how I see it. Having them in there for math operations is nice because you know what you write should be executed in the right order. But I only use it on math operations. Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-826422 Share on other sites More sharing options...
TGM Posted May 5, 2009 Author Share Posted May 5, 2009 $total = ($amount * $quantity); aight this is what I got <?php $result = mysql_query("SELECT * FROM orders ORDER BY `id` DESC LIMIT 1"); echo "<table border='1'> <tr> <th>Item</th> <th>Quanity</th> <th>Price per unit</th> <th>Extra</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['item'] . "</td>"; echo "<td>" . $row['quanity'] . "</td>"; //yes its quanity not quality echo "<td>" . $row['price'] . "</td>"; echo "<td>" . $row['extra'] . "</td>"; echo "</tr>"; } echo "</table>"; echo"<br><div align='center'><em>YOUR TOTAL BILL IS:</em></div> <HR WIDTH='75%' SIZE='3' NOSHADE><br />"; //option 1 $result = $row['quanity'] * $row['price']; //doesnt work...outputs 0 //option 2 $result = $quanity * $price; //doesnt work..says Undefined variable: price echo "<p>Bill: $result</p>"; mysql_close($db); ?> It displays the last order but doesnt displays the calculation..for example if an item cost price= 2.25 and amount=2 should be 2.50 (i tink!lol) the option 1 code gives 0 and the option 2 gives an error..how can I read the value from the 'price' and the 'quantity' field? Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-826439 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Oi... learn PHP. Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-826478 Share on other sites More sharing options...
corbin Posted May 5, 2009 Share Posted May 5, 2009 premiso, why the parenthesis? I always do math operations inside parans. Just a habit. Even in my math classes since I have learned what the parans did and got marked for not using them I just do it. Order of Operations is how I see it. Having them in there for math operations is nice because you know what you write should be executed in the right order. But I only use it on math operations. Hrmmm never seen anyone do that before ;p. Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-826496 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 corbin - I'm sure you've seen something like this before: $random_number = intval($_POST['random_number']); $some_formula_result = ($random_number + 4) * 4; I just made that up, but you get the point with parentheses. I'm just being an ass. I know what you meant. Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-826506 Share on other sites More sharing options...
Mark Baker Posted May 5, 2009 Share Posted May 5, 2009 It displays the last order but doesnt displays the calculation..for example if an item cost price= 2.25 and amount=2 should be 2.50 (i tink!lol) the option 1 code gives 0 and the option 2 gives an error..how can I read the value from the 'price' and the 'quantity' field? :polite mode enabled: Because after while($row = mysql_fetch_array($result)) has finished looping, $row no longer contains record data. And because your $result = $row['quanity'] * $row['price']; with an empty row is overwriting the value of the $result resultset returned by your database query, you can't even loop again. Calculate the value of each line in your loop. Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-826678 Share on other sites More sharing options...
corbin Posted May 6, 2009 Share Posted May 6, 2009 corbin - I'm sure you've seen something like this before: $random_number = intval($_POST['random_number']); $some_formula_result = ($random_number + 4) * 4; I just made that up, but you get the point with parentheses. I'm just being an ass. I know what you meant. But the parenthesis are needed there . Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-827166 Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 Hrmmm never seen anyone do that before ;p. Doesn't surprise me I always had to do that in Pre-Calc to keep my orders or operations correct or else I would screw something up. It just kept me straight. My teacher was always bugged by it, but he could not dock me cause it got the right solution. I am decent with math, I just suck at organization lol. $some_formula_result = ($random_number + 4) * 4; You see, I would have written that like: $some_formula_result = (($random_number + 4) * 4); and that is what confuses corbin about my style. Quote Link to comment https://forums.phpfreaks.com/topic/156845-calculating-in-php/#findComment-827549 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.