CI分页返回帖子链接

时间:2015-12-01 13:24:00

标签: php codeigniter pagination

我正在学习CI,而我最近的任务就是分页。所以我遵循了这个教程,因为它看起来相当不错,并且解释得很好。 Link

分页效果很好。现在我想在每个帖子下阅读更多内容,使每个帖子单独打开并附上完整描述。这也有效,但当我点击链接返回分页索引时,无论我点击哪个帖子,列表都从头开始。我不确定如何添加我希望返回链接带回来的页面,所以我只是在这里发帖,希望有人告诉我这太难了。

如果有人对我之后的事情感到困惑,请查看最后一个视图readmore_paginate。在其中,最后一个链接应该包含一个数字 到页面,但idk如何放或放在那里。

控制器

public function paginate()
{
    $config = array();
    $config['base_url'] = base_url()."welcome/paginate";
    $config['total_rows'] = $this->blog_model->countPosts();
    $config['per_page'] = 2;
    $config['uri_segment'] = 3;

    $this->pagination->initialize($config);

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; //TERNARY OPERATOR (? = TRUE) (: = FALSE)
    $data['results'] = $this->blog_model->fetchPosts($config['per_page'], $page);
    $data['links'] = $this->pagination->create_links();

    $this->load->view('header');
    $this->load->view("p_content", $data);
    $this->load->view('footer');
}
public function readMore_Paginate()
{
    $id = $this->input->get('postid');
    $data['post'] = $this->blog_model->getSpecificPost($id);
    $this->load->view('header');
    $this->load->view('readmore_paginate', $data);
    $this->load->view('footer');
}

模型

public function countPosts()
{
    return $this->db->count_all("Blogposts");
}

public function fetchPosts($limit, $start)
{
    $this->db->select('*');
    $this->db->from('Blogposts');
    $this->db->join('Blogcategories', 'Blogposts.postcatid=Blogcategories.id');
    $this->db->limit($limit, $start);
    $query = $this->db->get();
    if($query->num_rows() > 0)
    {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;
}

查看p_content

    <div class="col-md-8">      
            <table class="table">
                <tbody>
<?php 

foreach ($results as $key) {

                        echo "<tr><th><h2><kbd><font color='yellow'>".$key->postname."</font></kbd></h2><kbd><font color='lime'>".date("d M Y",strtotime($key->postdate))."</font></kbd> <kbd><font color='cyan'>".$key->catname."</font></kbd></th></tr>";
                        echo "<tr><td><blockquote>".mb_substr($key->postdesc, 0,80,'UTF-8')."...";?>
                        <a href="<?php echo base_url()."welcome/readmore_paginate?postid=".$key->postid; ?>">read more</a></blockquote> 
                                            <?php } ?>

    </tbody>
            </table>
            <p><?php echo $links; ?></p>
        </div>

查看readmore_paginate

    <div class="col-md-8">      
            <table class="table">
                <tbody>
<?php 

foreach ($post as $key) {

                            echo "<tr><th><h2><kbd><font color='yellow'>".$key['postname'].
                                "</font></kbd></h2><kbd><font color='lime'>".
                                date("d M Y",strtotime($key['postdate']))."
                                </font></kbd> <kbd><font color='cyan'>".
                                $key['catname']."</font></kbd></th></tr>";
                            echo "<tr><td><blockquote>".$key['postdesc']."</blockquote>"; ?>
                            <a href="<?php echo base_url().'welcome/paginate'; ?>">Back to Posts</a>
                        <?php }

                     ?>

</tbody>
        </table>
    </div>

1 个答案:

答案 0 :(得分:1)

控制器

$page = $this->input->get('page') ? 
             $this->input->get('page') : 
             $this->uri->segment(3) ? 
                 $this->uri->segment(3) : 
                 0;
$data['results'] = $this->blog_model->fetchPosts($config['per_page'], $page);
$data['links'] = $this->pagination->create_links();
$data['page'] = $page; // add this

查看p_content

<a href="<?php echo base_url()."welcome/readmore_paginate?postid=".$key->postid; ?>&page=<?=$page?>">read more</a></blockquote> 

查看readmore_paginate

<a href="<?php echo base_url().'welcome/paginate?page='.$this->input->get('page'); ?>">Back to Posts</a>