The following two-dimensional array stores information for several images (1.Book that contains image, 2.Author of book that contains image, 3.Name of image, 4.Image ID):
$images = array(array('book_name'=>"BookA", 'author_name'=>"AuthorA",
'image_name'=>"ImageA", 'image_id'=>1),
array('book_name'=>"BookA",'author_name'=>"AuthorB",
'image_name'=>"ImageA", 'image_id'=>1),
array('book_name'=>"BookA",'author_name'=>"AuthorA",
'image_name'=>"ImageB", 'image_id'=>2),
array('book_name'=>"BookA", 'author_name'=>"AuthorB",
'image_name'=>"ImageB", 'image_id'=>2),
array('book_name'=>"BookB", 'author_name'=>"AuthorA",
'image_name'=>"ImageB", 'image_id'=>2));
A book can have several authors.
An author can be associated with several books.
An image can be contained in several books.
I would like to create an html table with the following columns:
- Book(s) – Field lists all the books that contain a specific image
- Author(s) – Field lists all the authors of all the books that contain that specific image
- Image Name – Field contains the name of that image
I am currently using the following code:
<?php function transform($images) {
$result = array();
foreach($images as $key => $image) {
$image_id = $image['image_id'];
if (!isset($result[$image_id])) {
$result[$image_id] = array(
'author_name' => array(),
'book_name' => $image['book_name'],
'image_id' => $image['image_id'],
'image_name' => $image['image_name']
);
}
$result[$image_id]['author_name'] = array_merge($result[$image_id]['author_name'],
array(($image['author_name'])));
}
foreach($result as $key => $data) {
$uniqueauthors = array_unique($result[$key]['author_name']);
$result[$key]['author_name'] = implode('; ', $uniqueauthors);
}
return $result;
}
$images = transform($images);?>
My html code to create the table:
<table>
<tr><th>Book(s)</th>
<th>Author(s)</th>
<th>Image Name</th>
</tr>
<?php foreach ($images as $image): ?>
<tr>
<td><?php htmlout($image['book_name']); ?></td>
<td><?php htmlout($image['author_name']); ?></td>
<td><?php htmlout($image['image_name']); ?></td>
</tr>
<?php endforeach; ?>
</table>
Resulting table:
Book(s) | Author(s) | Image Name
BookA AuthorA;AuthorB ImageA
BookA AuthorA;AuthorB ImageB
Desired table:
Book(s) | Author(s) | Image Name
BookA AuthorA;AuthorB ImageA
BookA;BookB AuthorA;AuthorB ImageB
Problem:
This code correctly outputs (2.) list of all authors of all the books that contain the image in question and (3.) the image name. However, it does not output (3.) as desired: It only outputs the first book_name (BookA) that contains the image in question, not a list of ALL books containing the image (BookA;BookB).
Question: How can I obtain an HTML table that contains 3 columns of which (1.) lists all books containing the image in question, (2.) lists all authors of the books containing the image in question, (3.) contains the name of the image in question.
Thanks a lot in advance!
A few tweaks to your transform function, seems you handled the authors but forgot the books 🙂
Output:
Which should hopefully work in your htmlout function