简体   繁体   English

使用两个表并在 mysql 和 php 中分组

[英]Working with two tables and grouping in mysql and php

I have two tables like this.我有两张这样的桌子。

    books
    =====
    b_id    a_id    book_name
    1       1       ABC
    2       1       DEF
    3       2       GHI


    authors
    =======

    a_id    author_name
    1       A1
    2       A2

I need a table like this.我需要一张这样的桌子。

        S.No    BookName
        -----------------

        A1
        ==
        1       ABC
        2       DEF


        A2
        ==

        1       GHI

What i'm planning to do is 1. Do a while loop and get the author name first and print it 2. Use the author id inside the first loop and do another one iteration to get the list of book list for each author.我打算做的是 1. 做一个 while 循环并首先获取作者姓名并打印它 2. 在第一个循环中使用作者 id 并再进行一次迭代以获取每个作者的书单列表。

A sample code is like this:一个示例代码是这样的:

    <table cellpadding="0" cellspacing="0" border="0">
      <thead>
        <tr>
          <th>S.No</th>
          <th>Book Name</th>
        </tr>
      </thead>
      <?php
    $mysql_query1 = "my sql query to get the author name list first";
    $results = mysql_query ( $mysql_query1 ) or die ( mysql_error () );
    while($row = mysql_fetch_array($results)){
    ?>
      <tbody>
        <tr>
          <td style="background:none;"><u><?php echo $row['author_name']; ?></u></td>
        </tr>
        <?php
    $result = mysql_query ( "mysql query to get the list of books for each author with a where condition like this where a_id=$row['a_id']" ) or die ( mysql_error () );
    $book_count = mysql_num_rows($result);
    while($row1 = mysql_fetch_array($result)){
    ?>
        <tr>
          <?php for($i=1;$i<=$book_count;$i++){ ?>
          <td><?php echo $i; ?> </td>
          <td><?php echo $row1['book_name']; ?> </td>
          <?php } ?>
        </tr>
        <?php 
    }
    ?>
      </tbody>
      <?php } ?>
    </table>

My friends insisted me that the above method is a old one, and there is something to do with just few line codes.我的朋友坚持说,上面的方法是旧的,只是几行代码就有些关系。 Is it?是吗?

If yes, could someone redefine my code and give me.如果是的话,有人可以重新定义我的代码并给我。

Thanks, Kimz谢谢,金兹

Сan this be so?是这样吗?

$mysql_query1 = "select * from authors"

$result = mysql_query ( "select * from books where a_id=".$row['a_id']) or die ( mysql_error () );

The PHP method would be something like: PHP 方法类似于:

//First let's get all the values we need and store them in array format
// so that we don't need to make expensive calls to the database
<?php
$result = mysql_query("SELECT author_name, book_name FROM books b, authors a WHERE b.a_id=a.a_id");
$arr = array();
while($row=mysql_fetch_assoc($query)){
      //storing books by author
      $arr[$row['author_name']][]=$row['book_name'];
    }
//so now we have an array like ['A1'=>'ABC','DEF'],['A2'=>'GHI']
?>
<table cellpadding="0" cellspacing="0" border="0">
  <thead>
    <tr>
      <th>S.No</th>
      <th>Book Name</th>
    </tr>
  </thead>
  <tbody>
    <?php
         //let's print out the contents of our array in table format
         foreach($arr as $author=>$val){
             echo "<tr>
                      <td style='background:none;'><u>".$author."</u></td>
                      <td></td>
                   </tr>";
             foreach($val as $ind=>$book_name)
             {
               echo "<tr>
                         <td>".$ind."</td>
                         <td>".$book_name."</td>
                    </tr>";
             }
         }
      ?>
  </tbody>
</table>


Note:笔记:

Please, don't use mysql_* functions in new code .请不要在新代码中使用mysql_*函数 They are no longer maintained and are officially deprecated .它们不再被维护并被正式弃用 See the red box ?看到红框了吗? Learn about prepared statements instead, and use PDO , or MySQLi - this article will help you decide which.了解准备好的语句,并使用PDOMySQLi -这篇文章将帮助您决定哪个。 If you choose PDO, here is a good tutorial .如果您选择 PDO,这里有一个很好的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM