Well, here's a simple query that retrieves all moderators for a forum with a given ID:
It's not possible to integrate this into a single query getting all the forums without unpleasant duplication of the forum data. It's possible, however, to fetch the moderators for all forums in a single query:Code:SELECT users.* FROM users INNER JOIN forum_moderators ON users.id = forum_moderators.user_id WHERE forum_moderators.forum_id = ?
Then you can use PHP code like to create an array where you can look up moderators based on the forum ID (assuming the above is in $sql, and still approximately in PEAR DB syntax):Code:SELECT forum_moderators.forum_id AS forum, users.id as user_id, users.name as username FROM users INNER JOIN forum_moderators ON users.id = forum_moderators.user_id
Finally, if you use a query to fetch every forum in an array:PHP Code:$result =& $connection->query($sql);
$moderators = array();
while($row = $result->fetch_assoc()) {
$moderators[$row['forum_id'][] = array('id' => $row['user_id'], 'name' => $row['username']);
}
You can use this in the markup-emitting part like thus:PHP Code:$sql = 'SELECT * FROM forums';
$result = $connection->query($sql);
$forums = array();
while($row = $result->fetch_assoc()) {
$forums[] = $row;
}
(Of course, I would actually use Smarty for the last.)PHP Code:<?php
foreach($forums as $forum) {
?>
<div class="forum">
<span class="name"><?php echo $forum['name']; ?></span>
<span class="description"><?php echo $forum['description']; ?></span>
<span class="mods">Moderators:
<?php
foreach($moderators[$forum['id']] as $mod) {
?>
<a href="userinfo.php?id=<?php echo $mod['id']; ?>"><?php echo $mod['name']; ?></a>
<?php
}
?></span>
<?php
}




CornedBee
Reply With Quote