我正在获取分页和页面链接但链接无法正常工作

时间:2016-08-19 07:19:23

标签: php

我正在尝试使用codeigniter进行分页。但是在一个页面上,我从db获取所有记录。我希望所有页面都被分割但是没有发生。我想要一个解决方案。我搜索了所有论坛但没有得到解决方案。

我的控制器是这样的:

public function catalogs()
    {

        $this->load->library("pagination");
        $this->load->model('Cataloguemodel');

        $get_vars = $this->input->get();
        if(is_array($get_vars))
        $config['suffix'] = '?'.http_build_query($get_vars,'', "&");
        $config['base_url'] = base_url() . $this->router->class."/catalogs";
        $config['first_url'] = $config['base_url'] . (isset($config['suffix'])?$config['suffix']:'');

        $config['total_rows'] = $this->Cataloguemodel->record_count();
        $config['per_page'] = 2;
        $config['uri_segment'] = $this->uri->segment(3);


        $config["num_links"] = 4;

        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = false;
        $config['last_link'] = false;
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = '&laquo';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';



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

        $page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;


        $data['catalist'] = $this->Cataloguemodel->Cataloguelist($config['per_page'], $page);

        $data['links'] = $this->pagination->create_links();

        $this->load->view('header');
        $this->load->view('features',$data);
        $this->load->view('footer');
    }
}

我的模型就像这样

public function Cataloguelist($limit= null, $start= null)
    {
        $this->db->limit($limit, $start);

        $returncataarray = array();

        $sql = "select id, name, image, catalogue, description from tbl_catalogues";
        $query = $this->db->query($sql);

        if($query->num_rows())
        {
            $rows = $query->result();

            foreach($rows as $tempcata)
            {
                $smallcataarray = array();

                $smallcataarray['id'] = $tempcata->id;
                $smallcataarray['name'] = $tempcata->name;
                $smallcataarray['image'] = $tempcata->image;
                $smallcataarray['catalogue'] = $tempcata->catalogue;
                $smallcataarray['description'] = $tempcata->description;

                array_push($returncataarray, $smallcataarray);
            }
        }

        return $returncataarray;
    }

     public function record_count() 
    {
        return $this->db->count_all("tbl_catalogues");
    }

}

观点就是这样。

<section id="main-container">
        <div class="container">

            <!-- About us -->
            <div class="row">
                <div class="col-md-12">
                    <h2 class="article-title">Catalogues</h2>
                </div>
            </div><!-- Title row end -->

            <div class="feature-items">
            <?php
            foreach($catalist as $tempcat)
            { 
            ?>
                <h3 class="title-normal"><strong><?php echo $tempcat['name'];?></strong></h3>
                <div class="row">
                    <div class="col-sm-2 feature-item-img"><img class="img-responsive" src="admin/<?php echo $tempcat['image'];?>" alt="" width="200" height="200"></div>
                    <div class="col-sm-8 feature-item-content">
                        <p><?php echo $tempcat['description'];?></p>
                         <a href="admin/<?php echo $tempcat['catalogue'];?>">Download Catalogue</a>
                    </div>
                </div>



            <div class="ts-divider">&nbsp;</div>
            <?php
            }
            ?>

            <div class="paging text-center">
            <ul class="pagination">
                <li><?php echo $links;?></li>
            </ul>
            </div>


        </div>

2 个答案:

答案 0 :(得分:0)

评论

$this->db->limit($limit, $start);

然后在查询中添加限制

$sql = "select id, name, image, catalogue, description from tbl_catalogues $start,$limit";

答案 1 :(得分:0)

使用以下代码更新您的功能Cataloguelist

public function Cataloguelist($limit= null, $start= null)
    {
       $returncataarray = array();
       if($limit>0){
          $this->db->limit($limit, $start);
        }
        $this->db->select('id, name, image, catalogue, description', false);
        $query = $this->db->get('tbl_catalogues');

        if($query->num_rows())
        {
            $rows = $query->result();

            foreach($rows as $tempcata)
            {
                $smallcataarray = array();

                $smallcataarray['id'] = $tempcata->id;
                $smallcataarray['name'] = $tempcata->name;
                $smallcataarray['image'] = $tempcata->image;
                $smallcataarray['catalogue'] = $tempcata->catalogue;
                $smallcataarray['description'] = $tempcata->description;

                array_push($returncataarray, $smallcataarray);
            }
        }

        return $returncataarray;
    }

并更新控制器代码

$this->load->library("pagination");
        $this->load->model('Cataloguemodel');

        $get_vars = $this->input->get();
        if(is_array($get_vars))
        $config['suffix'] = '?'.http_build_query($get_vars,'', "&");
        $config['base_url'] = base_url() . $this->router->class."/catalogs";
        $config['first_url'] = $config['base_url'] . (isset($config['suffix'])?$config['suffix']:'');

        $config['total_rows'] = $this->Cataloguemodel->record_count();
        $config['per_page'] = 5;
        $config['uri_segment'] = $this->uri->segment(3);