如何将不同的论坛分组到PHP类别中

时间:2016-05-05 20:24:12

标签: php mysql forums

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

enter image description here

我目前有一个名为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 ;

1 个答案:

答案 0 :(得分:1)

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

解决方案: 由于您已经拥有数据库结构,并且您已经并且应该知道为了将import { Component, OnInit} from 'angular2/core' import { ROUTER_DIRECTIVES } from 'angular2/router' import { Card } from './card' import { CardService } from './card.service' import { CardFilterPipe } from './card-filter.pipe' @Component({ templateUrl: 'app/card/card-list.component.html', directives: [ROUTER_DIRECTIVES], pipes: [CardFilterPipe] }) export class CardListComponent implements OnInit { pageTitle: string = 'Card List'; cards: Card[]; errorMessage: string; listFilter: string; constructor(private _cardService: CardService) { } ngOnInit(): void { this.getCards(); } getCards() { this._cardService.getCards() .subscribe( result => this.cards = result, error => this.errorMessage = <any>error); } } 表与您的categories表链接起来,您需要在两个类似的列中至少有一列是{{{ 1}}从forums表自动递增列,即category_id,因此,为了将论坛分类为特定类别,您需要将类别categories添加到额外列{在您的id表格中{1}},以便每个论坛都有ID值中提到的类别..!

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

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

id

工作代码示例:

category_id

输出:

forums