分页不正常

时间:2017-02-27 07:03:48

标签: php html mysql

这是我的分页代码。在我的代码中,我将在每个页面中显示20个图像,但是我在第一页中获取所有图像,如果我再次单击page2,则显示相同的图像。请查看我的代码并给出我有价值的建议。谢谢。

控制器:

public function onSectorClick() {
    $id = $_GET["id"];
    $name = $_GET["name"];
    $this->session->set_userdata('sub1category_id', $_GET['id']);
    $this->session->set_userdata('sub1category_name', $_GET['name']);
    $this->onSectorClickCopy();
}

public function onSectorClickCopy(){
    $this->load->model('Categories_model');
    $this->load->model('productdisplay_model');

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

    $config = array();
    $config["base_url"] = base_url() . "index.php/welcome/onSectorClick?id=". $this->session->userdata('sub1category_id')."&name=".  $this->session->userdata('sub1category_name');
    $total_row = $this->productdisplay_model->record_count();
    $config['total_rows'] = $total_row;
    $config['per_page'] = 20;
    $config['use_page_numbers'] = TRUE;
    $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);
}

型号:

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


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

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();

}

查看:

<div class="container-main">
<?php
    foreach ($sub1products as $row) {
        foreach ($this->productdisplay_model->sub1Productfetch($row->id) as $menu1) {
?>
    <!--<h5 class="product-head">Agriculture Products</h5>-->
    <div class="col-xs-12 col-sm-4 col-md-3">
        <div class="prod-container">
            <a href="<?php echo base_url(); ?>index.php/welcome/onSectorClick1?id=<?php echo $row->id; ?>&name=<?php echo $row->sub1_category_name; ?>">
            <div class="prod_img">
                <img src="<?php echo 'data:image;base64,' . $menu1->product_image; ?>" width="100%"/>
            </div>
            <div class="prod_desc">
                <div class="prod-round-icon"></div>
                <h4 class="prod_title"><?php echo $row->sub1_category_name; ?></h4>
                <p class="prod_text"><?php echo $menu1->product_description; ?></p>
            </div>

            <div class="sub-products">
            <?php
                $count = 0;
                foreach ($this->productdisplay_model->sub2Productsmenu($row->id) as $menu2) {
            ?>

                <p><i class="fa fa-angle-right"></i> <?php echo $menu2->sub2_category_name; ?></p>

                <?php
                    $count++;
                    if ($count == 3) {
                        break;
                    }
                }
                ?>
            </div>
            <div class="view-more"> view more</div> 
            </a>  
        </div>
    </div>
<?php
        }
    }
?>  
</div>
<div class="center">
    <ul class="pagination">
    <?php
        foreach ($links as $link) {
            echo "<li>" . $link . "</li>";
        }
    ?>
    </ul>
</div>

2 个答案:

答案 0 :(得分:4)

在我的代码中,我刚刚更改了会话变量。然后一切正常。

<强>控制器

    public function onSectorClick() {

            $id = $_GET["id"];

            $this->session->set_userdata('sub1_id', $_GET['id']);

            $this->onSectorClickCopy();
        }

        public function onSectorClickCopy(){
            if ($this->session->userdata('log_in') == TRUE) {
                $id = $this->session->userdata('id');
                $this->load->model('cart_model');
                $data["count"] = $this->cart_model->count_rows($id);
            }



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

            $config = array();
            $config["base_url"] = base_url() . "index.php/welcome/onSectorClickCopy";
            $total_row = $this->productdisplay_model->record_count($this->session->userdata('sub1_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));
            $limit = $config['per_page']+1;
            $data['sub1products'] = $this->productdisplay_model->sub1Productsmenu($this->session->userdata('sub1_id'),$limit, $offset);
            $str_links = $this->pagination->create_links();
            $data["links"] = explode('&nbsp;', $str_links);

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

        }

<强>模型

   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();

    }

答案 1 :(得分:2)

正如我在您的代码中看到的那样:

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

你错了&#34 ;;&#34;在第二行。它总是返回所有的recods,因为WHERE条件不适用。