Codeigniter 3博客bug:post id被替换为category id

时间:2018-05-03 21:53:38

标签: php codeigniter

我正在使用Codeigniter 3.1.8中的基本博客应用程序

帖子在主页上显示,并按类别过滤

每当我尝试从类别页面查看帖子时,其网址末尾都有类别ID 而不是帖子ID 。 ID为1的类别中的所有帖子都链接到http://localhost/ciblog/posts/post/1

Posts_model 模型中有:

public function get_posts_by_category($id, $limit, $offset) {
    $this->db->order_by('posts.id', 'DESC');
    $this->db->limit($limit, $offset);
    $this->db->join('categories', 'categories.id = posts.cat_id');
    $query = $this->db->get_where('posts', array('cat_id' => $id));
    return $query->result();
}

类别控制器中:

public function posts($id) {
    $this->load->library('pagination');
    $config = [
        'base_url' => base_url('/categories/posts/' . $id),
        'page_query_string' => TRUE,
        'query_string_segment' => 'page',
        'display_pages' => TRUE,
        'use_page_numbers' => TRUE,
        'per_page' => 12,
        'total_rows' => $this->Posts_model->get_num_rows_by_category($id),
        //More code
    ];
    if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
        $_GET[$config['query_string_segment']] = 1;
    }
    $limit = $config['per_page'];
    $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
    $this->pagination->initialize($config);

    $data = $this->Static_model->get_static_data();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['category_name'] = $this->Categories_model->get_category($id)->name;
    $data['posts'] = $this->Posts_model->get_posts_by_category($id, $limit, $offset);

    $this->load->view('partials/header', $data);
    $this->load->view('categories/posts');
    $this->load->view('partials/footer');
}

上述控制器中的帖子视图:

<div class="container">
    <div class="col-xs-12">
        <h1 class="display-4 category-name"><?php echo $category_name; ?></h1>
    </div>  
    <?php if ($posts): ?>
        <div class="posts-grid">
            <?php foreach ($posts as $post) :?>
                <div class="col-xs-12 col-sm-6 col-lg-4 col-xl-3">
                    <div class="post">
                        <div class="thumbnail">
                            <a href="<?php echo base_url('posts/post/') . $post->id; ?>">
                                <img src="<?php echo base_url('assets/img/posts/') . $post->post_image; ?>" />
                            </a>
                        </div>
                        <div class="text">
                            <h2 class="card-title">
                                <a href="<?php echo base_url('posts/post/') . $post->id; ?>">
                                    <?php echo $post->title; ?>
                                </a>
                            </h2>
                            <p class="text-muted"><?php echo $post->description; ?></p>
                        </div>
                        <div class="read-more">
                            <a class="btn btn-block btn-sm btn-success" href="<?php echo base_url('posts/post/') . $post->id; ?>">Read more</a>
                        </div>
                    </div>
                </div>
            <?php endforeach ?>
        </div>
    <?php else: ?>
        <div class="col-xs-12">
            <p class="text-muted" id="no_posts">There are no posts in the <?php echo $category_name; ?> category yet.</p>
        </div>
    <?php endif; ?>

    <?php $this->load->view("partials/pagination");?>

</div>

正确显示帖子图片,标题等。 指向单个帖子的链接......

我的错误在哪里?

2 个答案:

答案 0 :(得分:1)

使用$this->db->select();指定查询中的选择部分。

<强>替代:

public function get_posts_by_category($id, $limit, $offset) {
    $this->db->select('posts.*'); // added
    $this->db->order_by('posts.id', 'DESC');
    $this->db->limit($limit, $offset);
    $this->db->join('categories', 'categories.id = posts.cat_id');
    $query = $this->db->get_where('posts', array('cat_id' => $id));
    return $query->result();
}

答案 1 :(得分:0)

您需要选择要检索Post_module应该是的数据的表:

public function get_posts_by_category($id, $limit, $offset) {
    $this->db->select('t1.*');
    $this->db->order_by('t1.id', 'DESC');
    $this->db->limit($limit, $offset);
    $this->db->join('categories t2', 't2.id = t1.cat_id');
    $query = $this->db->get_where('posts t1', array('t1.cat_id' => $id));
    return $query->result();
}