Forum Moderators: coopster
Order ID...Customer Name...Product......Qty...Price
3..........John Doe........Item One.....1.....15.50
...........................Item Three...2.....5.75
...........................Item Five....1.....3.50
................................Order Total...47.50
5..........Mike Brown......Item One.....2.....10.00
...........................Item Two.....1.....5.00
................................Order Total...15.00
6..........Jane Doe........Item Four....1.....12.00
................................Order Total...12.00
And so on. I can easily display everything without totals in one table with a simple while loop:
while ($row = mysql_fetch_array($result)) {
echo '<TR><TD>'.$row['order_id'].'</TD>';
echo '<TD>'.$row['customer_name'].'</TD>';
echo '<TD>'.$row['product_name'].'</TD>';
echo '<TD>'.$row['product_quantity'].'</TD>';
echo '<TD>'.$row['total_value'].'</TD></TR>';
}
But of course it displays everything repeatedly, like this:
Order ID...Customer Name...Product......Qty...Price
3..........John Doe........Item One.....1.....15.50
3..........John Doe........Item Three...2.....5.75
3..........John Doe........Item Five....1.....3.50
5..........Mike Brown......Item One.....2.....10.00
5..........Mike Brown......Item Two.....1.....5.00
6..........Jane Doe........Item Four....1.....12.00
What would be the best way to get what I'm looking for here? Some sort of foreach loop? The query I'm using here is a very basic SELECT query to just pull all the orders from the table. If I used GROUP BY order_id in the query, that would of course eliminate the duplicates, but I still want to display each individual product, quantity, and price for each order (plus the order total). Drawing a blank, any pointers?
Before the while loop, set a flag, say $newOrder, to true.
Then in your while loop if $newOrder is true then print custName and set it ($newOrder) to false, otherwise don't. Then examine lineNumber - if it's null, it's a row with total so print a total row & set $newOrder back to true (for the next iteration). If lineNumber isn't null, print prodName, qty, and price.
$lastorderid = '';
while ($row = mysql_fetch_array($result)) {
$thisorderid=$row['order_id'];
echo '<TR>';
if($thisorderid<>$lastorderid){
echo '<TD>'.$row['order_id'].'</TD>';
echo '<TD>'.$row['customer_name'].'</TD>';
}
else {
echo '<td></td><td></td>';
}
echo '<TD>'.$row['product_name'].'</TD>';
echo '<TD>'.$row['product_quantity'].'</TD>';
echo '<TD>'.$row['total_value'].'</TD></TR>';
}
I did not test the snippet, so be careful. but the logic should work in your case.
$lastorderid = '';
while ($row = mysql_fetch_array($result)) {
$thisorderid=$row['order_id'];
echo '<TR>';
if($thisorderid<>$lastorderid){
echo '<TD>'.$row['order_id'].'</TD>';
echo '<TD>'.$row['customer_name'].'</TD>';
$lastorderid=$thisorderid; //forgot this line, lol
}
else {
echo '<td></td><td></td>';
}
echo '<TD>'.$row['product_name'].'</TD>';
echo '<TD>'.$row['product_quantity'].'</TD>';
echo '<TD>'.$row['total_value'].'</TD></TR>';
}
if you drew your order sums from a single mysql-request using the sum()-function (group by order id) you might probably get the number of items in the same breath using the count()-function. Something like
$sql = "select orderid, count(price) as numberofitems, sum(price) as ordertotal from orders group by orderid";
That would be a second query to the database, but I think such a solution is best, because of the one-to-many relation between your orders-table and your produts-sold-table. You might connect this query to the same query by means of which you got the customer-data using some join on customerid.
Just think about it a while: Your order sums and customer data have a set-level different from your products-sold.
You may easily embed two sql-queries like this:
echo '<table>;
$sql1 = "query customer data and order sums according to orderid"
....read out variables
foreach ($orderid as thisid){
echo "<tr>customerdata-headerline<tr>";
$sql2 = "query products-data for thisorderid";
....read out variables
while ($row = mysql_fetch_array($result)) {//this is your script
echo '<TR><TD></TD>';
echo '<TD></TD>';
echo '<TD>'.$row['product_name'].'</TD>';
echo '<TD>'.$row['product_quantity'].'</TD>';
echo '<TD>'.$row['total_value'].'</TD></TR>';
}
echo "<tr>ordersums baseline</TR>";
}
echo "</table>";
This code is of course not complete and usable.
It should only illustrate the logic.