试图让论坛显示所有带有类别的子类别
Posted
技术标签:
【中文标题】试图让论坛显示所有带有类别的子类别【英文标题】:Trying to get forums to display all subcategories with category 【发布时间】:2015-07-21 03:01:16 【问题描述】:这是我的论坛目前的样子: 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,这意味着为该类别提供的所有子类别不会在一起,并且每个子类别都会创建一个新论坛,这是我不想要的。 有没有一种方法可以让回声爆炸,这样如果 2 个或更多子类别属于 1 个类别,它就不会推出新的 div?
【问题讨论】:
forums
和 categories
这两个表之间有什么关系吗?
不,没有。 @Ofir Baruch
应该如此,否则 - 你怎么知道哪些论坛属于哪个类别?考虑将新字段 category_id
添加到您的 forums
表中,其中将包含 category parent
的 ID
。
哦,我没想到你会这样问。在那种情况下,是的,category_id 已经存在。尽管如此,我仍然需要帮助,因为它为每个子类别创建了一个新的 div。
【参考方案1】:
我在没有测试的情况下写了这个,所以你可能想要检查错误。这可能不是最有效的方法,但它应该有效。
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");
【讨论】:
以上是关于试图让论坛显示所有带有类别的子类别的主要内容,如果未能解决你的问题,请参考以下文章