试图获取论坛以显示所有类别的子类别

问题描述 投票:0回答:2

这是我目前的论坛的样子: http://prntscr.com/73oicl

但是,如您所见,这些类别都说“社区”,这不应该发生。 “测试”子类别应在“公告和更新”子类别下。

我知道问题是什么,但我不知道如何解决问题。

function getForums($id) { $currentHost = "http://$_SERVER[HTTP_HOST]"; $query = "SELECT * FROM forums, categories"; try { global $db; $stmt = $db->prepare($query); $stmt->execute(); $result = $stmt->fetchAll(); foreach($result as $row) { $category_title = $row['category_title']; $forum_id = $row['forum_id']; $forum_title = $row['forum_title']; $forum_topic_count = $row['forum_topic_count']; $forum_post_count = $row['forum_post_count']; $forum_last_topic_id = $row['forum_last_topic_id']; $forum_last_topic = $row['forum_last_topic']; $forum_last_date = $row['forum_last_date']; $forum_last_user = $row['forum_last_user']; $fixed_last_topic = substr($forum_last_topic,0,25).'...'; echo '<div class="forum pleft"> <div class="forum-header"> <span class="header-text">'.$category_title.'</span> </div> <table> <tr> <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td> <td class="topics">'.$forum_topic_count.'</td> <td class="posts">'.$forum_post_count.'</td> <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td> </tr> </table> </div>'; } } catch(PDOException $ex) { die("error"); } }

您可以看到,每个结果都将成为一个全新的论坛div,这意味着该类别给出的所有子类别都不会在一起,并且每个子类别都将建立一个新的论坛,我不想要。
有什么方法我想爆炸以进行回声,因此,如果两个或多个子类别在1个类别中,则不会推出新的div?
    

我在没有测试的情况下写了这篇文章,因此您可能需要检查错误。这可能不是最有效的方法,但应该起作用。
php html css explode forums
2个回答
0
投票
function getForums($id) { $currentHost = "http://$_SERVER[HTTP_HOST]"; $query = "SELECT * FROM forums, categories ORDER BY categories ASC"; /* Order by categories to arrange same categories together */ try { global $db; $stmt = $db->prepare($query); $stmt->execute(); $result = $stmt->fetchAll(); $numrows = $stmt->fetch(PDO::FETCH_NUM); /* Check for total rows of records */ $count = 0; $currentCategory = ""; // Declare container foreach($result as $row) { $category_title = $row['category_title']; $forum_id = $row['forum_id']; $forum_title = $row['forum_title']; $forum_topic_count = $row['forum_topic_count']; $forum_post_count = $row['forum_post_count']; $forum_last_topic_id = $row['forum_last_topic_id']; $forum_last_topic = $row['forum_last_topic']; $forum_last_date = $row['forum_last_date']; $forum_last_user = $row['forum_last_user']; $fixed_last_topic = substr($forum_last_topic,0,25).'...'; /* If currentCat is empty OR is not similar to previous */ if($currentCategory == "" || $currentCategory != $category_title){ /* If currentcat is not empty AND not similar to previous, close */ if($currentCategory != "" && $currentCategory != $category_title){ echo '</table></div>'; } echo '<div class="forum pleft"> <div class="forum-header"> <span class="header-text">'.$category_title.'</span> </div> <table> <tr> <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td> <td class="topics">'.$forum_topic_count.'</td> <td class="posts">'.$forum_post_count.'</td> <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td> </tr>'; $count++; /* Check if its last row, If yes, end */ if($count == $numrows){ echo '</table></div>'; } $currentCategory = $category_title; /*Set category title */ } /* If next row belong to the same category, If yes, continue */ else if($currentCategory == $category_title){ echo '<tr> <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td> <td class="topics">'.$forum_topic_count.'</td> <td class="posts">'.$forum_post_count.'</td> <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td> </tr>'; $count++; /* Check if its last row, If yes, end */ if($count == $numrows){ echo '</table></div>'; } } } } catch(PDOException $ex) { die("error"); } }

我在没有测试的情况下写了这篇文章,因此您可能需要检查错误。这可能不是最有效的方法,但应该起作用。

0
投票
/ 尝试 { 全球$ db; $ stmt = $ db->准备($ query); $ stmt-> execute(); $ result = $ stmt-> fetchall(); $ numrows = $ stmt-> fetch(pdo :: fetch_num); /

检查总记录的总行 */

$count = 0; $currentCategory = ""; // Declare container foreach($result as $row) { $category_title = $row['category_title']; $forum_id = $row['forum_id']; $forum_title = $row['forum_title']; $forum_topic_count = $row['forum_topic_count']; $forum_post_count = $row['forum_post_count']; $forum_last_topic_id = $row['forum_last_topic_id']; $forum_last_topic = $row['forum_last_topic']; $forum_last_date = $row['forum_last_date']; $forum_last_user = $row['forum_last_user']; $fixed_last_topic = substr($forum_last_topic,0,25).'...'; /* If currentCat is empty OR is not similar to previous */ if($currentCategory == "" || $currentCategory != $category_title){ /* If currentcat is not empty AND not similar to previous, close */ if($currentCategory != "" && $currentCategory != $category_title){ echo '</table></div>'; } echo '<div class="forum pleft"> <div class="forum-header"> <span class="header-text">'.$category_title.'</span> </div> <table> <tr> <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td> <td class="topics">'.$forum_topic_count.'</td> <td class="posts">'.$forum_post_count.'</td> <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td> </tr>'; $count++; /* Check if its last row, If yes, end */ if($count == $numrows){ echo '</table></div>'; } $currentCategory = $category_title; /*Set category title */ } /* If next row belong to the same category, If yes, continue */ else if($currentCategory == $category_title){ echo '<tr> <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td> <td class="topics">'.$forum_topic_count.'</td> <td class="posts">'.$forum_post_count.'</td> <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td> </tr>'; $count++; /* Check if its last row, If yes, end */ if($count == $numrows){ echo '</table></div>'; } } } } catch(PDOException $ex) { die("error"); } }

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.