loops in loops
Posted: Mon Sep 27, 2010 5:08 pm
today i was told that some code I have is bad becuase i am using a loop within a loop. But i dont know how to do it any other way, and they werent helpful in the matter. So how would i do it? Heres the code in question:
As you can see the first query ($parent_info_query) gets the parent name and parent id so i can use the parent id in the next query ($forum_info_query) so i can use WHERE parent_id = $parent_id. This is so it groups the results into the respective categories. Is there a way to do this without a loop in a loop? and why is this bad practice?
Thanks
Code: Select all
$parent_info_query = $db->query("SELECT
p.parent_id, p.parent_name
FROM ".DB_PREFIX."parents as p
ORDER BY p.parent_id")
or trigger_error("SQL", E_USER_ERROR);
while ($parent_info = mysql_fetch_object($parent_info_query)) {
// Add parent_id into variable for later query
$parent_id = $parent_info->parent_id;
echo '<table class="forum_table" onclick="expandCollapseTable(this)">
<tr id="tr1">
<th class="forum_left_corner"></th>
<th class="forum_parent_name">'.$parent_info->parent_name.'</th>
<th class="empty"></th>
<th class="empty"></th>
<th class="forum_last_post_header">'.LAST_POST.'</th>
</tr>';
$forum_info_query = $db->query("SELECT
f.forum_id,
f.forum_name,
f.forum_description,
f.forum_topics,
f.forum_posts,
f.forum_last_poster,
f.forum_last_post_time,
f.forum_last_post,
m.user_id,
m.user_username,
m.user_group,
t.topic_id,
t.topic_name,
po.post_id,
po.post_subject
FROM ".DB_PREFIX."forums as f
LEFT JOIN ".DB_PREFIX."members as m
ON f.forum_last_poster = m.user_id
LEFT JOIN ".DB_PREFIX."topics as t
ON t.forum_id = f.forum_id
LEFT JOIN ".DB_PREFIX."posts as po
ON po.post_id = f.forum_last_post
WHERE f.parent_id = '$parent_id'
ORDER BY f.forum_id")
or trigger_error("SQL", E_USER_ERROR);
while ($forum_info = mysql_fetch_object($forum_info_query)) {
// Get Forum information from DB to show all forums
// including who the last post was posted by
echo '<tr class="gradient">';
$forum_url_name = $forum_info->forum_name;
$forum_url_name = str_replace(' ', '_', $forum_url_name);
echo ' <td class="forum_icon">
<div class="thread_icon"></div>
</td>
<td class="forum_name">
<p class="forum_name"><a href="index.php?forum=
'. $forum_info->forum_id . '
&name=
'.$forum_url_name.'
">
' . $forum_info->forum_name .'
</a></p>
<p class="forum_description">
' . $forum_info->forum_description .'
</p></td>
<td class="forum_topics">
'. $forum_info->forum_topics .'
<span class="small_word"> '.TOPICS.'</span></td>
<td class="forum_posts">
'. $forum_info->forum_posts .'
<span class="small_word"> '.POSTS.'</span></td>
<td class="forum_last_post">';
if (!$forum_info->forum_last_poster) {
echo '<p class="noposts">'.NO_POSTS.'</p>
<p class="be_the_first">'.BE_FIRST.'</p>';
}
elseif ($forum_info->forum_last_post == 0) {
echo '<p class="noposts">'.NO_POSTS.'</p>
<p class="be_the_first">'.BE_FIRST.'</p>';
}
else {
$forum_last_post_clean = str_replace(' ', '_', $forum_info->user_username);
echo '<p class="last_post_name"><a href="index.php?topic='.$forum_info->topic_id.'
&name='.$forum_last_post_clean.'">'.$forum_info->post_subject.'</a></p>';
echo '<p class="posted_by">Posted By - ';
$today = date('F j, Y');
$last_post = date("F j, Y", strtotime($forum_info->forum_last_post_time));
if ($last_post == $today) { $last_post = 'Today at '.date("g:i a", strtotime($forum_info->forum_last_post_time)); }
else { $last_post = date("F j, Y - g:i a", strtotime($forum_info->forum_last_post_time)); }
if ($forum_info->user_group == 1) { echo
'<span class="admin">'; }
elseif ($forum_info->user_group == 2) { echo
'<span class="mod">'; }
elseif ($forum_info->user_group == 3) { echo
'<span class="user">'; }
elseif ($forum_info->user_group == 0) { echo
'<span class="user">'; }
echo $forum_info->user_username .'</span></p>
<p class="last_post_date">'
.$last_post;
}
echo '</p></td></tr>';
}
}Thanks