I’m trying to do a bit of an experiment, however I am encountering an issue.
I am trying to accomplish 2 things from a query:
- For every row of data that is in the query, I want the label “Alternate” to appear as a table column header.
- For every row of data that is in the query, I want to show the the value of “image_mini_id” from my query.
//Display Table
if (isset($miniSelection)) {
$sql = "SELECT mini_id, mini_name, mini_number, mini_imag_id FROM tableMinis WHERE mini_id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$miniSelection]);
$mini = $stmt->fetch();
$sql = "SELECT imag_name, imag_mini_id FROM tableImages WHERE imag_mini_id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$miniSelection]);
echo '<table class="optionsMenuTable">';
echo '<tr>';
echo '<th>Main</th>';
foreach ($stmt as $row) {
echo '<th>Alternate</th>';
}
echo '</tr>';
echo '<tr>';
echo '<td>';
echo $mini["mini_image_id"];
echo '</td>';
foreach ($stmt as $row) {
echo '<td>';
echo $row['imag_name'];
echo '</td>';
}
echo '</tr>';
echo '</table>';
}
This table environment (and all the echo’s) is just to rough out what I am trying to achieve – I will be changing it all later once I have the functionality I am trying to achieve.
To help understand what I am trying to do, I’ll explain this a bit.
tableMinis has a column “mini_imag_id”. This is the id# of a corresponding image from the images table (tableImages). The image linked to this id# is considered the “main display image”.
tableImages contains all images, and consists of 3 columns, an id# column (imag_id), a name column (imag_name), and linking column (imag_mini_id) which links the id# of a record in tableMinis.
Eventually, I will want to show the the “main image” associated to the id of record in tableMinis, but then next to it, I want to also show the secondary (or alternate) images which are also linked to that id#.
Could someone please tell me why the results of the second foreach loop are null. When I remove the first foreach loop (which removes my table headers) then second foreach loop works fine (however I want the headers to populate in equal amount to the data returned).