Reputation: 19
i have a problem making a pdf i have 2 tables in database book and author and the problem is if the book is 2 or more author i can`t show it in just one column below is the image of join book and author and the pdf i make.
My Data There`s a 2 author how to make them one row or join them in one column
The pdf that i make below is the code
$sqlpatient = mysqli_query($connect, "select * from book");
$rowpatient = mysqli_fetch_array($sqlpatient);
do{
if(mysqli_num_rows($sqlpatient) != 0){
$bookid = $rowpatient['bookid'];
$authorsql = mysqli_query($connect, "SELECT author FROM author WHERE bookid = '$bookid' ");
$output .= '<tr>
<td>'.$rowpatient["bookid"].'</td>
<td>'.$rowpatient["callnumber"].'</td>
<td>'.$rowpatient["isbn"].'</td>
<td>'.$rowpatient["bookname"].'</td> ';
while ($authorrow = mysqli_fetch_array($authorsql)) {
$author = $authorrow["author"].",";
$output .= '<td>'. $author.'</td>';
}
$output .= ' <td style="text-transform: capitalize;">'.$rowpatient["genre"].'</td>
<td>'.$rowpatient["publisher"].'</td>
<td>'.$rowpatient["publishdate"].'</td>
</tr>
';
}
}while($rowpatient = mysqli_fetch_array($sqlpatient));
return $output;
Upvotes: 0
Views: 1718
Reputation: 13635
The issue is that you're creating a new <td></td>
pair inside your loop for each author, which is what messes it up if there are more than one.
Try adding all the authors to a string and then have one <td></td>
pair after the loop:
// Define an empty string that we will append all authors to
$authors = '';
while ($authorrow = mysqli_fetch_array($authorsql)) {
$authors .= $authorrow["author"].",";
}
// I've used trim() to trim away the trailing comma.
$output .= '<td>' . trim($authors, ',') . '</td>';
Upvotes: 1
Reputation: 163
As per my understanding of the question, you need to use implode() function
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
output:- Hello World! Beautiful Day!
Upvotes: 0
Reputation: 326
you can create an empty string, and inside your while loop concatenate it with the author's name, when the loop ends, the variable created will have all the authors' names, then u can create a table column and put the variable inside. You can do this or create an array and push the values to it inside the loop, and then use implode function with ',' that returns a string of the array elements, Check https://www.w3schools.com/php/func_string_implode.asp
Upvotes: 1