Vise flere forfattere i samme række?
Jeg er i gang med et skoleprojekt hvor jeg skal vise bøger med tilknyttede forfattere og kan ikke gennemskue hvordan jeg viser flere forfattere for én bog.Jeg får kun vist en bog flere gange, hvis der er flere forfattere tilknyttet, og er interesseret i at det kun bliver vist én gang, altså at forfatterne står i samme række. Tror måske det er min if-statement i min foreach-løkke?
Jeg har følgende tabeller author, book og author_book. Her er min kode:
require_once('connect.inc.php');
$conn = dbConnect('pdo') or die('no connection');
$sqlquery = "SELECT book_tbl.*, type_tbl.*, status_tbl.*, category_tbl.*, author_tbl.*, author_book_tbl.*
FROM book_tbl
INNER JOIN author_book_tbl ON book_tbl.book_id=author_book_tbl.book_id
INNER JOIN author_tbl ON author_book_tbl.author_id = author_tbl.author_id
INNER JOIN status_tbl ON book_tbl.status_id= status_tbl.status_id
INNER JOIN type_tbl ON book_tbl.type_id= type_tbl.type_id
INNER JOIN category_tbl ON book_tbl.category_id = category_tbl.category_id";
$result = $conn->query($sqlquery);
$prow = array('first_name' => NULL, 'last_name' => NULL);
$rows=0;
foreach($result as $row)
{
$number=$row['book_id'];
$isbn=$row['isbn'];
$title=$row['title'];
$year=$row['year'];
$price=$row['price'];
$description=$row['description'];
$type=$row['type'];
$status=$row['status'];
$category=$row['category'];
$authorbook=$row['author_book_id'];
$author=$row['author_id'];
$first_name=$row['first_name'];
$last_name=$row['last_name'];
echo"<tr>";
echo"<td>";
echo$title;
echo"</td>";
echo"<td>";
if ($prow['first_name'] = $first_name){
echo $first_name . ' ' . $last_name;
}
echo"</td>";
echo"<td>";
echo$year;
echo"</td>";
echo"<td>";
echo$price . ' $';
echo"</td>";
echo"<td>";
echo$description;
echo"</td>";
echo"<td>";
echo$type;
echo"</td>";
echo"<td>";
echo$status;
echo"</td>";
echo"<td>";
echo$category;
echo"</td>";
echo"<td>";
echo$isbn;
echo"</td>";
echo '<td>';
echo '<a href= update.php?book_id='.$number.'&author_book_id='.$authorbook.'&author_id='.$author.' class="btn btn-success">Update</a>';
echo '</td>';
echo '<td>';
echo '<a href=delete.php?book_id='.$number.'&author_book_id='.$authorbook.' class="btn btn-danger">Delete</a>';
echo '</td>';
echo"</tr>";
$rows++;
$prow=$row;
}
echo '</table>';
echo '<p> Der er ';
echo $rows;
echo ' bøger i øjeblikket</p>';
echo '</br>';
echo '</br>';
?>
På forhånd tak :)