douju2053
douju2053
2018-08-24 19:22

数据库组按列

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duaabhuv188411 duaabhuv188411 3年前

    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.

    点赞 评论 复制链接分享
  • duanhuang1699 duanhuang1699 3年前

    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.)

    点赞 评论 复制链接分享

为你推荐