SZ | name | Date | back_number
1234 | John | 18.8.2018 | 5512345
2391 | Bels | 18.8.2018 | 6685800
3498 | ESON | 18.8.2018 | 6685800
4531 | me | 18.8.2018 | 5512345
4531 | me | 18.8.2018 | 8812345
4531 | me | 18.8.2018 | 8812345
this is my data from my database.
I need to get the output after SQL and PHP processing.
I need to filter by back_number. I need to display the records in the table by the column back_number. And every table must be new. In each table, I need to assign as many records as the records of back_number.
table:
back_number - 5512345
-
SZ - 1234 | name - John | Date 18.8.2018
SZ - 4531 | name - me | Date 18.8.2018
next table
back_number - 6685800
-
SZ - 2391 | name - Bels | Date 18.8.2018
SZ - 3498 | name - ESON | Date 18.8.2018
and next table
back_number - 8812345
-
SZ - 4531 | name - me | Date 18.8.2018
SZ - 4531 | name - me | Date 18.8.2018
actual code:
$sql = "SELECT * FROM my_DB GROUP BY back_number ";
<?php
foreach ($back_number as $row) {
if( $row['back_number '] != null ){
?>
<article>
<div class="header header_green_grad">
<?php
echo "back_number - " . $row['back_number '];
?>
</div>
<table>
<tr>
<?php
echo "<td>" . $row['SZ'] . "</td>";
echo "<td>" . $row['back_number '] . "</td>";
echo "<td>" . date('d.m.Y', strtotime($row['date'])) . "</td>";
echo "<td>" . $row['name'] . "</td>";
?>
</tr>
</table>
</article>
I have somewhere to add more foreach or what to do. I do not know the advice.
Like @sticky bit said you cant use group by, it doesn't return groups of rows separately. It only returns a single for each group...
Another way you can do this is by
$sql = "select distinct(back_number) from FROM my_db";
$back_numbers = mysqli_query($Conn, $sql);
Now,
foreach ($back_numbers as $back_number){
$mysql = "select * from FROM my_db where back_number =".$back_number;
$rows = mysqli_query($Conn, $mysql);
foreach($rows as $row){
echo $row;
}
}
Ps one advantage here is that you can create a new table for each back_number inside the first loop and store the $row inside it.
First of all, don't GROUP BY
, ORDER BY
.
SELECT *
FROM my_db
ORDER BY back_number;
Then you can iterate through the result. When ever the back_number
changes print your header. Then or else print a row.
Some sketch for that:
$current_back_number = "";
foreach ($rows as $row) {
if ($row["back_number"] != $current_back_number) {
// print header
$current_back_number = $row["back_number"];
}
// print row
}
(Assuming, that back_number
cannot be null or empty. That assumption might be wrong and you need to refine the logic accordingly.)