1

Ok, i know this is stupid but I'm stuck and I need a kick in the head. I have 3 tables (orders, orderitems, products) and I'm using 2 left outer joins in a query to display the orders history in the clients section:

SELECT * FROM orderitems oi, orders o
LEFT OUTER JOIN product p ON p.dbarcode = oi.orderitems_item
WHERE o.order_code = oi.orderitems_ordercode 
    AND oi.orderitems_ordercode = '".$_GET['ordercode']."'` 

I can display a row for each product in the table order_items but I can't seem to display (echo) the total amount which is held in the field orders.order_amount, i presume it has to do with the fact that the field has multiple instances or whatsoever. How do I display data as a result of several tables joined?

Edit for Quassnoi:

I loop through the results and create a table row for each one and, after the loop, i want to display the general total:

  <? do { ?>
  <tr>
    <td><? echo $row_OrderItems['dbarcode']; ?></td>
    <td><? echo $row_OrderItems['name']; ?></td>
    <td><? echo $row_OrderItems['orderitems_quantity']; ?></td>
    <td><? echo $row_OrderItems['price']; ?></td>
  </tr>
  <? } while ($row_OrderItems = mysql_fetch_assoc($OrderItems)); ?>

  <tr>
    <td colspan="5">Total order amount (incl. shipping and handling): &euro; <? echo $row_OrderItems['order_amounteuro']; ?></td>
  </tr>
4
  • Would adding o.order_amount AS order_amount in the SELECT help? Commented Apr 14, 2011 at 11:49
  • you need to store the total before the while loop ends. Commented Apr 14, 2011 at 12:33
  • Yes, I've realised that the reason I cant display the total is because it's outside the loop, however i need to display it there and not inside the repeated region. Any ideas? Commented Apr 14, 2011 at 13:45
  • see my answer below, i updated it Commented Apr 14, 2011 at 19:56

3 Answers 3

3

o.order_amount will be repeated in every row, just take the first one.

And, of course, never use this:

oi.orderitems_ordercode = '".$_GET['ordercode']."'` 

without mysql_real_escape_string.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I do sanitize the inputs, I just used the above as an example! I loved the cartoon strip though! :)
Forgot to ask though, HOW do I display only the first instance? If i simply echo out the field ie echo $row_orderinfo('order_amout') it shows nothing although the data has been fetched (i can see it with print_r)
@bikey77: please post a sample of your code (as an update to your original post)
1
<? do { ?>
  <tr>
    <td><? echo $row_OrderItems['dbarcode']; ?></td>
    <td><? echo $row_OrderItems['name']; ?></td>
    <td><? echo $row_OrderItems['orderitems_quantity']; ?></td>
    <td><? echo $row_OrderItems['price']; ?></td>
  </tr>
  <? 
$total = $row_OrderItems['order_amounteuro']; // store the total, before you loop past the last row
} while ($row_OrderItems = mysql_fetch_assoc($OrderItems)); ?>

  <tr>
    <td colspan="5">Total order amount (incl. shipping and handling): &euro; <? echo $total; ?></td>
  </tr>

1 Comment

Yes, I have, i'm using print_r and I can see all the data correctly.
0

Just an FYI, this part of your statement:

SELECT * FROM orderitems oi, orders o 
LEFT OUTER JOIN product p ON p.dbarcode = oi.orderitems_item

is fetching everything from orderitems, orders and products. Prefix the * with the table you want all the fields from, or rewrite it to fetch specific fields.

1 Comment

I get your point but doesnt the condition "AND oi.orderitems_ordercode = '".$_GET['ordercode']."'" do the job and just pull the specific records from the database?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.