I’m trying to create a page which will display a list of blog entries from a table (blog) in an SQL database, along with each blog entry’s associated tags from another table (tags) in the same SQL database. Each tag has a foreign key that associates it with the proper blog entry. On the page that displays the list of entries, there is a column for these tags, in which all the tags for a particular entry are placed. I realize this might sound confusing, so I have included a screenshot:
The table itself is being generated by a do/while loop, while the tags are being generated by a nested do/while loop inside an if statement that compares the current article’s primary key (article_id) with all the foreign keys (object_id) in the “tags” table.
So what’s the problem? Well, all three of these bogus articles have tags stored in the MySQL database, but they only print for the first article in the loop. Any help would be appreciated. Here’s the code:
<?php $counter = 0; //initialize the hilite counter outside the loop ?>
<?php do { ?>
<tr <?php if ($counter++ % 2) {echo 'class="hilite"';} ?>>
<td><?php echo $row_getArticles['created']; ?></td>
<td><?php echo $row_getArticles['title']; ?></td>
<td><?php echo $row_getArticles['username']; ?></td>
<td><?php echo $row_getArticles['image']; ?></td>
<td><?php echo $row_getArticles['caption']; ?></td>
<td><?php
do {
if ($row_getArticles['article_id'] == $row_getTags['object_id']){
echo " *" , $row_getTags['tag'];
}//end if
} while ($row_getTags = mysql_fetch_assoc($getTags));
?></td>
<td><a href="blog_update.php?article_id=<?php echo $row_getArticles['article_id']; ?>">EDIT</a></td>
<td><a href="blog_delete.php?article_id=<?php echo $row_getArticles['article_id']; ?>">DELETE</a></td>
</tr>
<?php } while ($row_getArticles = mysql_fetch_assoc($getArticles)); ?>