Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge rows of item column having the same date column and display items in single row in PHP

I’m making an online shopping cart website and the customer must view all his/her orders in a table with all the order information like item names, price, date ordered, etc. And I want to group each order by date and display the items with same date in one row.

My table looks like this:

Orders Date Ordered
item1 11-23-2021
item2 11-23-2021
item3 12-30-2021

I want it to look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Orders Date Ordered
item1, item2 11-23-2021
item3 12-30-2021

PHP:

<table>
  <tr>
    <th>Orders</th>
    <th>Date Ordered</th>
  </tr>

  <?php
   $showOrder = mysqli_query($conn, "SELECT * FROM orderdetails WHERE dateOrdered = '11-23-2021'
                            GROUP BY orderDate");

   while ($row = mysqli_fetch_array($showOrder)){
                    $item = $row['item'];
                    $date = $row['dateOrdered']; ?>
  <tr>
    <td><?php echo "<p>".$item."</p>"; ?></td>
    <td><?php echo "<p>".$date."</p>"; ?></td>
  </tr>
<?php } ?>
</table>

>Solution :

You can use GROUP_CONCAT in MySQL query. For example,

SELECT GROUP_CONCAT(orders), date_ordered FROM orderdetails WHERE dateOrdered = '11-23-2021' GROUP BY orderDate

I presume your orderDate table has orders and date_ordered fields, You can modify according to your actual table column names.

Hope this should help you!

You can read details like your own delimeters etc. here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading