【问题标题】:How to group different forums into categories PHP如何将不同的论坛分组 PHP
【发布时间】:2016-05-05 20:24:11
【问题描述】:

我想将我的论坛分组,如下所示:

我目前有一个名为forum_categories 的数据库表,它采用标题并为创建的所有类别创建一个ID。我还在数据库表中有一个名为forum_forums 的列(我想要分类的所有不同论坛),它采用一个名为category_apart_of 的值。

我将如何在正确的类别 ID 中列出论坛?

非常感谢!

如果您想查看我的任何代码,或者希望我更深入地解释任何内容,请告诉我。

我当前列出论坛的代码(注意:获取所有论坛的 SQL 查询在上面):

<thead>
    <tr>
      <th style="width: 50%;">Forum</th>   
      <th style="width: 10%;">Threads</th> 
      <th style="width: 10%;">Posts</th>
      <th style="width: 30%;">Latest Posts</th>        
    </tr>
    </thead>
    <tbody> 
    <?php 
      while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $forumID = $row['forumID'];
        $forum_title[] = $row['forum_title'];
        $forum_description[] = $row['forum_description'];
        $forum_total_threads[] = $row['forum_total_threads'];
        $forum_total_posts[] = $row['forum_total_posts'];
        $forum_latest_thread[] = $row['forum_latest_thread'];

        $stmt2 = $db->prepare("SELECT * FROM forum_threads WHERE forum_thread_belongs_to = '$forumID'");
        $stmt2->execute();
        $count = $stmt2->rowCount();

        echo '
        <tr><td><h4 style="margin-bottom: 0px;"><a style="margin-bottom: 0px;" href="forum.php?id=' 
        . $row['forumID'] . ' ">'.$row['forum_title']
        . '</a></h4><br /><h6 style="margin-bottom: 0px; margin-top: 0px;">'
        .$row['forum_description'].'</h6></td><td style="text-align: center;><span">'.$count
        .'</span></td><td style="text-align: center;><span">'.$row['forum_total_posts']
        .'</span></td><td>'.$row['forum_latest_thread'].'</td></tr>
        ';
     }
    ?>
</tbody>

两个表的 DDL:

CREATE TABLE IF NOT EXISTS `forum_forums` (
`forumID` int(11) NOT NULL AUTO_INCREMENT,
`forum_title` varchar(255) NOT NULL,
`forum_description` varchar(255) NOT NULL DEFAULT 'This forum does not have      a description',
`forum_total_threads` int(255) NOT NULL DEFAULT '0',
`forum_total_posts` int(255) NOT NULL DEFAULT '0',
`forum_latest_thread` varchar(255) NOT NULL DEFAULT 'There are no new   threads',
`forum_apart_of` int(11) NOT NULL,
`category_apart_of` int(11) NOT NULL,
 PRIMARY KEY (`forumID`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `forum_categories` (
`catID` int(11) NOT NULL AUTO_INCREMENT,
`cat_title` varchar(255) NOT NULL,
PRIMARY KEY (`catID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

【问题讨论】:

  • 我对你的数据库结构有点困惑。您能否编辑您的问题以包含 ERD 或某些 DDL?
  • 您发布的 DDL 不包含您的代码中引用的表 forum_threads
  • 已修复,对此感到抱歉。
  • forum_thread_belongs_to 应该是FOREIGN KEYforum_forums 表?

标签: php mysql forums


【解决方案1】:

您的问题:我将如何在正确的类别 ID 中列出论坛?

解决方案: 由于您已经拥有数据库结构,并且您已经并且可能应该知道,为了将您的 categories 表与您的 forums 表链接,您需要在两个相似的列中至少有一个列,即来自您的 @ 的 category_id 987654324@ 表自动递增列即 id 因此,为了将您的论坛分类到特定类别,您需要将类别 id 添加到 category_id 在您的 category_id 表中的额外列中,这样每个论坛都会在 id 值中有提到它的类别..!

然后您可以按以下方式按类别列出您的论坛:

注意:此代码将检查每个论坛类别,并将列出每个类别下的所有论坛..!

<?php
//Assuming you have fetched whole data from forums table in $forums
//And whole data from categories in $categories
//So moving forward with the code

foreach ($categories  as $category) {
echo "<h1>".$category['category_title']."</h1>";
$category_id = $category['category_id'];
$query = mysqli_query($mysqli,"SELECT * FROM forums WHERE category_id='$category_id'");

$forums = array();
while ($rows = mysqli_fetch_array($query)) {
  $forums[] = $rows;
}

foreach ($forums as $forum) {
echo "Title :".$forum['forum_title']."</br>";
echo "Descripton :".$forum['forum_description']."</br></br></br>";
}
echo "</br></br></br></br>";  
} 
?>

工作代码示例:

<?php
$categories = array(
    array('id' => "04",'category_title' => "News & Support"),
    array('id' => "23",'category_title' => "Current Affairs"),
    array('id' => "12",'category_title' => "Politics"));

    $forums = array(
    array('forum_title' => "News 1",'category_id' => "04"),
    array('forum_title' => "News 2",'category_id' => "04"),
    array('forum_title' => "Current Afairs 1",'category_id' => "23"),
    array('forum_title' => "Current Afairs 2",'category_id' => "23"),
    array('forum_title' => "Politics 1",'category_id' => "12"),
    array('forum_title' => "Politics 2",'category_id' => "12")); 

foreach ($categories  as $category) {
echo "<h1>".$category['category_title']."</h1>";
$category_id = $category['id'];
$output = array();
for ($i=0;$i<=count($forums);$i++) {
if  ($category_id == $forums[$i]['category_id']) {
$add_forum = array('forum_title' => $forums[$i]['forum_title'],'category_id' => $forums[$i]['category_id']);
array_push($output, $add_forum);
}
}
for ($i=0;$i<=count($output);$i++) {
echo "Title :".$output[$i]['forum_title']."</br>";
}
echo "</br></br></br></br>";  
} 
?>

输出:

News & Support

Title :News 1
Title :News 2


Current Affairs

Title :Current Afairs 1
Title :Current Afairs 2


Politics

Title :Politics 1
Title :Politics 2

【讨论】:

  • 谢谢。现在正在研究它,我会在完成时告诉你它是否有效。
  • No..Problem..让我知道它是否解决了您的问题,所以我相信您会将其标记为已接受..!
  • @ashland3000 :更新了代码以获得更好的实际示例......!
猜你喜欢
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 2020-07-08
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
相关资源
最近更新 更多