Jump to content

calculating in php


TGM

Recommended Posts

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :D I know what you meant.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :D I know what you meant.

 

 

 

But the parenthesis are needed there :).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.