到目前为止,这是我的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"> </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 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>
答案 0 :(得分:0)
我会在表线程中创建一个额外的字段:last_post_time。每次创建新帖子时,都会更新此字段。这不是正常形式,但到目前为止最简单的论坛应用程序。
答案 1 :(得分:0)
为此我会像这样查询数据库:按DESC
订购id
并检索id
。由于您要插入新行,id
将auto_increment
,您需要做的只是DESC
订单。
<?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 NULL
并在您更新帖子或类别时使用time()
。然后根据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;
}
}