到目前为止,这是我的 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>