doufangzhang4454 2017-03-06 06:17
浏览 39

我的分页代码不起作用

This is my pagination code.It displaying 20 images in first page and its not moving link to second page.My pagination code is not working.Could you please go through my code and give me your suggestions,Thank you.

Controller

                public function onSectorClick() {
                        $id = $_GET["id"];

                        $this->session->set_userdata('sub1category_id', $_GET['id']);
                        $this->session->set_userdata('sub1category_name', $_GET['name']);
                        $this->onSectorClickCopy();
                    }

                    public function onSectorClickCopy(){                            
                        $this->load->helper('url');                          

                        $data['ListMenuLevel1'] = $this->Categories_model->listsector1();

                        $config = array();
                        $config["base_url"] = base_url() . "index.php/welcome/onSectorClickCopy?id=".$this->session->userdata('sub1category_id')."&name=".$this->session->userdata('sub1category_name');
                        $total_row = $this->productdisplay_model->record_count($this->session->userdata('sub1category_id'));
                        $config['total_rows'] = $total_row;
                        $config['per_page'] = 20;
                        //$config['uri_segment'] = 3;
                        $config['use_page_numbers'] = TRUE;
                        //$config['page_query_string'] = TRUE;
                        //$config['reuse_query_string'] = FALSE;
                        $config['num_links'] = 1;
                        $config['cur_tag_open'] = '&nbsp;<a class="current">';
                        $config['cur_tag_close'] = '</a>';
                        $config['next_link'] = 'Next';
                        $config['prev_link'] = 'Previous';
                        $this->load->library('pagination');
                        $this->pagination->initialize($config);
                        /* if($this->uri->segment(3)){
                          $page = ($this->uri->segment(3)) ;
                          }
                          else{
                          $page = 1;
                          } */
                        $page = ($this->uri->segment(3) != '' ? $this->uri->segment(3) : 1);
                        //$offset = $config['per_page']*$page;

                        $offset = (($config['per_page']) * ($page - 1)) + 1;
                        $limit = $config['per_page'] * $page;
                        $data['sub1products'] = $this->productdisplay_model->sub1Productsmenu($this->session->userdata('sub1category_id'),$limit, $offset);
                        $str_links = $this->pagination->create_links();
                        $data["links"] = explode('&nbsp;', $str_links);

                        $this->load->view('productlist', $data);                           
                    }
                }

Model

            <?php

            class productdisplay_model extends CI_Model {

                function __construct() {
                    parent::__construct();
                }
             public function record_count($id) {
                    $this->db->select('*');
                    $this->db->from('sub1_category');
                    $this->db->where('main_categoryid_fk', $id);
                    $this->db->order_by("sub1_category.main_categoryid_fk ");
                    $query = $this->db->get();
                    return  $query->num_rows();                       
                }              

                public function sub1Productsmenu($id,$limit, $offset){                        
                    $this->db->select('*');
                    $this->db->from('sub1_category');
                    $this->db->where('main_categoryid_fk', $id);
                    $this->db->order_by("sub1_category.main_categoryid_fk ");
                    $this->db->limit($limit, $offset);
                    return $this->db->get()->result();                        
                }

                public function sub1Productfetch($id) {                        
                    $query = $this->db->select('product_image,sub1_category_name,product_description');
                    $this->db->from('sub3_category');
                    $this->db->join('sub2_category', 'sub3_category.sub2_categoryid_fk=sub2_category.id', 'left');
                    $this->db->join('sub1_category', 'sub2_category.sub1_categoryid_fk=sub1_category.id', 'left');
                    //$this->db->join('maincategory', 'sub1_category.main_categoryid_fk=maincategory.id', 'left');
                    $this->db->where('sub1_category.id',$id);

                    //$this->db->order_by('rand()');
                    $this->db->limit(1);              

                    return $this->db->get()->result();                  
                }
            }                

View

     <div class="center">
                <ul class="pagination">
                     <?php
                    foreach ($links as $link) {
                        echo "<li>" . $link . "</li>";
                    }
                    ?>
                </ul>
            </div>   
  • 写回答

1条回答 默认 最新

  • douang1243 2017-03-06 07:47
    关注

    This provides you to retrieve information from your URI strings

    $this->uri->segment(n); // n=1 for controller, n=2 for method, etc
    

    consider this example http://www.domainname.com/index.php/blog/language/php/function

    it will return

    $this->uri->segment(1); // blog
    $this->uri->segment(2); // language
    $this->uri->segment(3); // php
    $this->uri->segment(4); // function
    

    According to your url : http://localhost/terasukhintrade/index.php/welcome/onSectorClickCopy?id=1&name=agriculture/2

    $this->uri->segment(3) gets? // try to debug yourself

    But in your case, who have to fetch the last uri segment.

    Alternate 1

    $last = end($this->uri->segments); 
    //Where, segments is a class variable.
    

    Alternate 2

    $last = end($this->uri->segment_array());
    

    Alternate 3

    $total = $this->uri->total_segments();
    $last = $this->uri->segment($total);
    

    Update $page = ($this->uri->segment(3) != '' ? $this->uri->segment(3) : 1); to ($this->uri->segment($total) != '' ? $this->uri->segment($total) : 1); // as per alternative 3

    评论

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?