Php 在索引控制器codeigniter中显示类别的帖子
我在ci中是一个全新的人,我构建了一个项目,这个项目有索引页,并在这个页面中显示带有分页的帖子,我有包含类别的菜单侧栏,我想当我单击每个类别时显示该类别的帖子。如何将我的代码更改为该代码 主控器Php 在索引控制器codeigniter中显示类别的帖子,php,codeigniter,Php,Codeigniter,我在ci中是一个全新的人,我构建了一个项目,这个项目有索引页,并在这个页面中显示带有分页的帖子,我有包含类别的菜单侧栏,我想当我单击每个类别时显示该类别的帖子。如何将我的代码更改为该代码 主控器 class Home extends Ci_controller { function __construct() { parent:: __construct(); $this->load->model('home_model');
class Home extends Ci_controller {
function __construct() {
parent:: __construct();
$this->load->model('home_model');
$this->load->model('category_model');
$this->load->library('pagination');
}
function index() {
$category = $this->category_model->allCategory();
$config['base_url'] = base_url() . 'index.php/home/index/page/';
$config['total_rows'] = $this->home_model->countall();
$config['per_page'] = 5;
$config['uri_segment'] = 4;
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$this->pagination->initialize($config);
$posts = $this->home_model->all($config['per_page'], $page);
$setting = $this->home_model->setting('about');
$pagination = $this->pagination->create_links();
$this->template->load('home', array('posts' => $posts, 'pagination' => $pagination, 'category' => $category, 'setting' => unserialize($setting['value'])));
}
}
家用\u型号
class Home_model extends CI_Model {
function __construct() {
parent::__construct();
}
function allPost() {
$all = $this->db->get('posts')->result_array();
$data_send = array();
foreach ($all as $key => $value) {
$data = array();
$category = $this->db->get_where('catpost', array('post_id' => $value['post_id']))->result_array();
$data['id'] = $value['post_id'];
$data['title'] = $value['title'];
$data['create_time'] = $value['create_time'];
$data['content'] = $value['content'];
$data['username'] = $value['username'];
$data['category'] = $category;
$data_send[] = $data;
}
return $data_send;
}
function all($limit, $start) {
$lastpost = $this->db->select('post_id')->order_by('create_time DESC')->limit($limit, $start)->get('post')->result_array();
$idarr = array();
foreach ($lastpost as $key => $value) {
$idarr[] = "'{$value['post_id']}'";
}
$ids = implode(',', $idarr);
$sql = "select tbl_post.post_id, tbl_category.name, tbl_post.title, tbl_post.content, tbl_post.create_time, tbl_post.author_id, tbl_user.username from `tbl_category` join `tbl_post_category` on `tbl_post_category`.`category_id`=`tbl_category`.`category_id` join `tbl_post` on `tbl_post`.`post_id`=`tbl_post_category`.`post_id` join tbl_user on tbl_user.user_id=tbl_post.author_id where tbl_post.post_id in($ids) ORDER BY tbl_post.post_id DESC";
$allpost = $this->db->query($sql)->result_array();
$posts = array();
foreach ($allpost as $key => $value) {
$postid = $value['post_id'];
if (array_key_exists($postid, $posts)) {
$posts[$postid]['category'] .= ",{$value['name']}";
} else {
$posts[$postid] = array(
'post_id' => $postid,
'title' => $value['title'],
'content' => $value['content'],
'create_time' => $value['create_time'],
'username' => $value['username'],
'category' => $value['name']);
}
}
return $posts;
}
function setting($name = NULL) {
$ret = NULL;
if ($name != NULL) {
$this->db->where('name', $name);
$ret = $this->db->get('setting')->result_array()[0];
} else {
$ret = $this->db->get('setting')->result_array();
}
return $ret;
}
function countall() {
Return $this->db->count_all('posts');
}
}
查看类别的一部分
<?php
foreach($category as $key => $value){
$name='category['.$value['name'].']';
echo '<li><a href='.$value['name'].'>'.$value['name'].'</a></li>';
}
?>
查看列表的HTML输出源时,是否显示任何错误?我也不确定$name=的用途是什么。当您查看列表的HTML输出源时,是否显示任何错误?我也不确定$name=的用途是什么。