2

I am currently developing an ordering system where a customer can order many items. I also have an admin where he/she can see all the orders on that day. The Admin can view the name of the customer, the total payable, the products and the quantity of the product the customer have ordered.

I am currently seeing this results using my query.

 Name   | Payable | Product   | Quantity
 Test   |   165   | keychain  | 3
 Test   |   165   | Tumbler   | 1
 Miguel |   525   | Keychain  | 3
 Miguel |   525   | Magic Mug | 3
 Dandel |   1010  | keychain  | 3
 Dandel |   1010  | T-shirt   | 2
 Dandel |   1010  | Keychain  | 3
 Dandel |   1010  | Mug       | 5

This is my query.

 $result = mysql_query("
        SELECT reservation.firstname, reservation.lastname, reservation.payable, reservation.city, orders.product, orders.qty, reservation.date
        FROM orders
        INNER JOIN reservation
        ON orders.confirmation = reservation.confirmation 
        WHERE reservation.date = CURDATE() && reservation.city = '24th Floor'
        ");
    while($row = mysql_fetch_array($result))
        {
            echo '<tr>';
            echo '<td style="border: 1px solid black;">'.$row['firstname'].'</td>';
            echo '<td>'.$row['payable'].'</td>';
            echo '<td>'.$row['product'].'</td>';
            echo '<td>'.$row['qty'].'</td>';
            echo '</tr>';
        } 

I want to get results like this. How can I do it?

 Name   | Payable | Product   | Quantity
 Test   |   165   | keychain  | 3
        |         | Tumbler   | 1
 Miguel |   525   | Keychain  | 3
        |         | Magic Mug | 3
 Dandel |   1010  | keychain  | 3
        |         | T-shirt   | 2
        |         | Keychain  | 3
        |         | Mug       | 5
Benj
  • 57
  • 8
  • **Don't** use the **deprecated and insecure** `mysql_*`-functions. They have been deprecated since PHP 5.5 (in 2013) and were completely removed in PHP 7 (in 2015). Use MySQLi or PDO instead. – M. Eriksson Oct 04 '18 at 05:19
  • How would it look if there are multiple users width the same name? How would you differentiate between them? Is there some user id? – M. Eriksson Oct 04 '18 at 05:21

2 Answers2

0

You can maintain some state while iterating which keeps track of whether the current row is a new name/payable:

$last_name_seen = NULL;

while ($row = mysql_fetch_array($result)) {
    $firstname = "";
    $payable = "";
    if ($last_name_seen === NULL || $row['firstname'] != $last_name_seen) {
        $last_name_seen = $row['firstname'];
        $firstname = $row['firstname'];
        $payable = $row['payable'];
    }
    echo '<tr>';
    echo '<td style="border: 1px solid black;">'.$firstname.'</td>';
    echo '<td>'.$payable.'</td>';
    echo '<td>'.$row['product'].'</td>';
    echo '<td>'.$row['qty'].'</td>';
    echo '</tr>';
}

Note that your MySQL query should have some ORDER BY clause, to generate the ordering you want, e.g.

ORDER BY Name, Product;

As the comments above also suggest, if you are using a deprecated PHP API, you should consider upgrading to something more modern. But, the logic used in the above loop would not change much with changing the MySQL API.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Sir Tim, this code works like a charm. Thanks for your help. But I got another question. – Benj Oct 05 '18 at 01:09
  • @Benj If you have a small follow-up question, then feel free to ask in a comment. If it be an entirely new question, then please ask another question later. – Tim Biegeleisen Oct 05 '18 at 02:49
  • Sir @Tim the question is now posted. Hoping that you can help me again this time. Thank you! – Benj Oct 05 '18 at 04:43
  • Sir @Tim I posted a new question. Hoping that you can help me again this time. Thank you! – Benj Oct 18 '18 at 02:38
0

You have to set flag for name & payable column & use array_column(),array_unique() to get unique column values :

<?php

$result = array(array("Name"=>"Test","Payable"=>'165',"Product"=>"keychain","Quantity"=>3),array("Name"=>"Test","Payable"=>'165',"Product"=>"Tumbler","Quantity"=>1));

$name_arr = array_unique(array_column($result, 'Name'));
$payable_arr = array_unique(array_column($result, 'Payable'));

$name_flag = 0;
$pay_flag = 0;

echo "<table border='1'>";

for($i=0;$i<count($name_arr);$i++)
{

    for($j=0;$j<count($payable_arr);$j++)
    {
        foreach($result as $list)
        {
            if($list['Name'] == $name_arr[$i] && $list['Payable'] == $payable_arr[$j])
            {
                if($name_flag==0 && $pay_flag==0)
                {
                    echo "<tr>";
                    echo "<td>".$name_arr[$i]."</td>";
                    echo "<td>".$payable_arr[$j]."</td>";
                    echo "<td>".$list['Product']."</td>";
                    echo "<td>".$list['Quantity']."</td>";
                    echo "</tr>";
                }
                else
                {
                    echo "<tr>";
                    echo "<td>&nbsp;</td>";
                    echo "<td>&nbsp;</td>";
                    echo "<td>".$list['Product']."</td>";
                    echo "<td>".$list['Quantity']."</td>";
                    echo "<tr>";
                }
                $name_flag++;
                $pay_flag++;
            }
        }
    }
    $name_flag=0;
    $pay_flag=0;
}
echo "</table>";
?> 
Ashu
  • 1,320
  • 2
  • 10
  • 24