0

到目前为止,这是我的 Categories_model:

<?php
class Categories_model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function get_categories($parent = NULL) {
        $this->db->from('categories')->where('parent', $parent)->order_by('position');
        $query = $this->db->get();
        return $query->result_array();  
    }

}

SQL结构:

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `parent` int(11) unsigned DEFAULT NULL,
  `title` varchar(100) NOT NULL,
  `description` text NOT NULL,
  `position` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;



CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `tid` int(11) unsigned NOT NULL,
  `title` varchar(100) NOT NULL,
  `body` text NOT NULL,
  `created` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;



CREATE TABLE IF NOT EXISTS `threads` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `cid` int(11) unsigned NOT NULL,
  `title` varchar(100) NOT NULL,
  `body` text NOT NULL,
  `created` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

在我加载显示所有类别/子类别的视图之前,我的控制器:

$data['categories'] = $this->categories_model->get_categories();
$forum = array();

foreach ($data['categories'] as $category) {
    $forum[$category['id']] = $this->categories_model->get_categories($category['id']);
}

$data['subcategories'] = $forum;

$this->template
        ->title('Forum')
        ->build('home', $data);

我想在主页上显示的每个“子类别”中获取最新的主题和对该主题的回复。但我不太确定如何做到这一点,方法或 SQL 看起来如何以及在我当前的代码中在哪里实现它?

基本上:

论坛子类别(最新帖子) 类别 2(线程 2) 链接 链接(转到最新回复)

是我试图实现的目标,但我不知道该怎么做。

(显示页面)

        <table class="tborder" width="100%" cellspacing="1" cellpadding="6" border="0" align="center">
            <thead>
                <tr align="center">
                    <td class="thead">&nbsp;</td>
                    <td class="thead" width="100%" align="left">Forum</td>
                    <td class="thead">Senaste inlägg</td>
                    <td class="thead">Ämnen</td>
                    <td class="thead">Inlägg</td>
                </tr>
            </thead>
<?php
foreach ($categories as $row) {
?>
            <tbody>
                <tr>
                    <td class="tcat" colspan="5">
                        <a href="forumdisplay.php?f=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a>
                    </td>
                </tr>
            </tbody>
<?php
    foreach ($subcategories[$row['id']] as $sub_row) {
?>
            <tbody>
                <tr align="center">
                    <td class="alt2">
                        Ikon
                    </td>
                    <td id="f<?php echo $sub_row['id']; ?>" class="alt1Active" align="left">
                        <div>
                            <a href="forumdisplay.php?f=<?php echo $sub_row['id']; ?>">
                                <strong><?php echo $sub_row['title']; ?></strong>
                            </a>
                            <span class="smallfont">(0&nbsp;besökare)</span>
                        </div>
                        <div class="smallfont"><?php echo $sub_row['description']; ?></div>
                    </td>
                    <td class="alt2">
                        <div class="smallfont" align="left">
                            <div>
                                <span style="white-space:nowrap">
                                    <img class="inlineimg" border="0" alt="" src="images/icons/icon1.gif">
                                    <a title="Gå till det första olästa inlägget i ämnet 'Ämne 1'" style="white-space:nowrap" href="#">
                                        <strong>Ämne 1</strong>
                                    </a>
                                </span>
                            </div>
                            <div style="white-space:nowrap">
                                av
                                <a rel="nofollow" href="#">Medlem</a>
                            </div>
                            <div align="right" style="white-space:nowrap">
                                0000-00-00
                                <span class="time">00:00</span>
                                <a href="#">
                                    <img class="inlineimg" border="0" alt="Gå till det senaste inlägget" src="#" title="Gå till det senaste inlägget">
                                </a>
                            </div>
                        </div>
                    </td>
                    <td class="alt1">0</td>
                    <td class="alt2">0</td>
                </tr>
            </tbody>
<?php
    }
}
?>
        </table>
4

2 回答 2

0

检索最新帖子

为此,我会像这样查询数据库: order DESCbyid和 retrieve id。由于您要插入一个新行,因此您需要做的就是整理好id遗嘱。auto_incrementDESC

<?php
// This is a model
public function getPosts(){
  $this->db->order_by('id', 'DESC');
  $q = $this->db->get('posts');

  //Make sure we returned something.
  if($q->num_rows() > 0){
    return $q->result();
  }else{
    return false;
  }
}

检索最新类别

为此,我将添加一个last_posted字段: last_posted int(11) unsigned NOT NULLtime()在您更新帖子或类别时使用。然后据此获取last_posted

<?php
// This is a model - get latest category
public function getLastPostedCategories(){
  $this->db->order_by('last_posted', 'DESC');
  $q = $this->db->get('categories');

  //Make sure we returned something.
  if($q->num_rows() > 0){
    return $q->result();
  }else{
    return false;
  }
}

更新类别

为此,我将运行两个查询,一个用于插入新帖子,一个用于更新您选择的类别:我只向您展示更新类别部分。

<?php
// this is a model - add the last_posted timestamp to a category
public function updatePostDate($cid){
  // i like to check to make sure, $cid is set.. you could do this in the controller too.
  if($cid){
    $this->db->where('id', $cid);
    $this->db->update('categories', array( 'last_posted' => time() ));
    // Make sure we updated the category
    if($this->db->affected_rows() > 0){
      return true;
    }else{
      return false;
    }
  }else{
    return false;
  }
}
于 2012-04-23T13:31:44.120 回答
0

我会在表线程中创建一个额外的字段:last_post_time。每次创建新帖子时,都会更新此字段。这不是普通的形式,但对于论坛应用程序来说是最简单的。

于 2012-04-23T12:27:30.707 回答