[英]How to get data from many to many relationship table and print all rows in one line PHP
I am currently working on a library management system as a project and I have been struggling with many to many relationship tables.我目前正在研究一个图书馆管理系统作为一个项目,我一直在努力处理多对多的关系表。 So, in my database, I got books, book_has_authors and authors table as shown in a picture below.
所以,在我的数据库中,我得到了书籍、book_has_authors 和authors 表,如下图所示。
Picture of tables and relations between them表的图片和它们之间的关系
So, I have created view_books.php file where I am trying to list all the books from the database and it looks as follows:所以,我创建了 view_books.php 文件,我试图在其中列出数据库中的所有书籍,它看起来如下:
<table class="table table-hover">
<!-- table table-striped table-bordered table-hover begin -->
<thead>
<!-- thead begin -->
<tr>
<!-- tr begin -->
<th scope="col"> ISBN: </th>
<th scope="col"> Book Title: </th>
<th scope="col"> Publisher: </th>
<th scope="col"> Publication Year: </th>
<th scope="col"> Category: </th>
<th scope="col"> Number of copies: </th>
<th scope="col"> Edit: </th>
<th scope="col"> Delete: </th>
</tr><!-- tr finish -->
</thead><!-- thead finish -->
<tbody>
<!-- tbody begin -->
<?php
$i = 0;
$get_books = "SELECT book.ISBN, book.bookTitle, book.bookPublisher, book.bookPublicationDate, bookcategory.categoryName FROM book INNER JOIN bookcategory ON book.bookCategory = bookcategory.categoryID ";
// $get_payments = "SELECT * FROM books";
$run_books = mysqli_query($conn, $get_books);
while ($book_row = mysqli_fetch_array($run_books)) {
$ISBN = $book_row['ISBN'];
$title = $book_row['bookTitle'];
$publisher = $book_row['bookPublisher'];
$pubdate = $book_row['bookPublicationDate'];
$cat_name = $book_row['categoryName'];
// get total number of copies for each book
$count_copies = "SELECT * FROM bookcopy WHERE ISBN = $ISBN ";
$run_count = mysqli_query($conn, $count_copies);
$copy_num = mysqli_num_rows($run_count);
// get all authors
$authors_array = array();
$get_authors = "SELECT author.authorsFullName FROM book INNER JOIN book_has_authors ON book_has_authors.book_ISBN = book.ISBN INNER JOIN author ON author.authorID = book_has_authors.authors_authorID WHERE ISBN=$ISBN;";
$run_authors = mysqli_query($conn, $get_authors);
while($author_row = mysqli_fetch_array($run_authors)){
$authors_array[] = $author_row;
}
$i++;
echo '<tr scope="row"><!-- tr begin -->
<td>' . $ISBN . '</td>
<td>' . $title . '</td>
<td>' . $publisher . '</td>
<td>' . $pubdate . '</td>
<td>' . $cat_name . '</td>
<td>' . $copy_num. '</td>
<td><a href="index.php?delete_product' . $ISBN . '>
<i class="fas fa-trash"></i> Delete
</a>
</td>
<td>
<a href="index.php?edit_product' . $ISBN . '>
<i class="fa fa-pencil"></i> Edit
</a>
</td>
</tr><!-- tr finish -->
';
}
?>
</tbody><!-- tbody finish -->
</table>
As this is many to many relationships, one book can have many authors and many authors can have one book.由于这是多对多的关系,因此一本书可以有很多作者,而很多作者可以拥有一本书。 In the view_books.php I have created the while loop that iterates through all books and prints them in the table.
在 view_books.php 中,我创建了循环遍历所有书籍并将它们打印在表中。 At the moment I can get all data but not the authors as the query returns multiple rows(authors) for each book.
目前我可以获取所有数据,但不能获取作者,因为查询会为每本书返回多行(作者)。 As I mentioned before I would like to list all the books in the form of HTML table.
正如我之前提到的,我想以 HTML 表格的形式列出所有书籍。 Could someone explain to me please how can I concatenate all authors for each book and print them in a single table cell next to each other?
有人可以向我解释一下如何将每本书的所有作者连接起来并将它们打印在一个单独的表格单元格中? That is what I would like to achieve:
这就是我想要实现的目标:
First check this: $authors_array[] = $author_row;
首先检查这个:
$authors_array[] = $author_row;
(you're missing $author_row["authorsFullName"]
) . (您缺少
$author_row["authorsFullName"]
) 。
And then you can IMPLODE $authors_array
然后你可以IMPLODE
$authors_array
authors_array
and $stringAuthors
should be used to show the output:和
$stringAuthors
应该用于显示输出:
$stringAuthors = implode(',',$authors_array);
//Author1, Author2, Author3, etc... //作者1、作者2、作者3等...
implode — Join array elements with a string
implode — 用字符串连接数组元素
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
// Empty string when using an empty array:
var_dump(implode('hello', array())); // string(0) ""
?>
use the below query to get your result.使用下面的查询来获得你的结果。
SELECT A.ISBN, A.bookTitle, A.bookPublisher, A.bookPublicationDate, A.categoryName, GROUP_CONCAT(A.authorsFullName) AS 'Authors' FROM (
SELECT book.ISBN, book.bookTitle, book.bookPublisher, book.bookPublicationDate, bookcategory.categoryName, `authors`.`authorsFullName`
FROM book
INNER JOIN bookcategory ON book.bookCategory = bookcategory.categoryID
LEFT JOIN `book_has_authors` ON book_has_authors.`book_ISBN` = book.ISBN
LEFT JOIN `authors` ON `authors`.`authorID` = `book_has_authors`.`authors_authorID`
) AS A
GROUP BY A.ISBN, A.bookTitle, A.bookPublisher, A.bookPublicationDate, A.categoryName
Hope this will give your expected result.希望这会给你预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.