【问题标题】:Pagination Won't Work - Code Igniter分页不起作用 - Code Igniter
【发布时间】:2014-08-15 04:14:36
【问题描述】:

为什么我的分页不起作用? 仅仅为了这个功能就卡了 24 小时。我在数据库查询中错了吗?我不明白我的代码有什么问题。

这是控制器:

function search()
{   
    $category_type = $this->input->post('category_type');
    $data['result'] = $this->Grabber_model->get_data($category_type);

    $config = array();
    $config['base_url'] = base_url() . "grabber/search/index/";
    $config['total_rows'] = $this->Grabber_model->count();
    $config['per_page'] = 20;
    $config['uri_segment'] = 4;
    $config['num_links'] = 2;
    $config['use_page_numbers']  = TRUE;
    $config['full_tag_open'] = "<ul class='pagination'>";
    $config['full_tag_close'] ="</ul>";
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
    $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
    $config['next_tag_open'] = "<li>";
    $config['next_tagl_close'] = "</li>";
    $config['prev_tag_open'] = "<li>";
    $config['prev_tagl_close'] = "</li>";
    $config['first_tag_open'] = "<li>";
    $config['first_tagl_close'] = "</li>";
    $config['last_tag_open'] = "<li>";
    $config['last_tagl_close'] = "</li>";

    $this->pagination->initialize($config);
    $data["links"] = $this->pagination->create_links();

    $this->load->view('global_h');
    $this->load->view('grabber_view', $data);
    $this->load->view('global_f');
}

这是模型:

function get_data($category_type) {
    $this->load->database();
    $this->db->select('*');
    $this->db->from('products');
    $this->db->where('category_type', $category_type);
    $this->db->order_by('create_at','desc');
    $this->db->limit(50);

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

这是视图:

<div class="center">
  <?php foreach($result as $row){ ?>
  <div style="margin:0 10px 10px 0;float:left;width:250px" class="img-thumbnail" >
     <img data-original="http://static04-id.zalora.com<?php echo $row->image_full; ?>" width="240px" height="346px" class="lazy" /><br>
     Brand: <?php echo $row->brand; ?><br>
     SKU: <?php echo $row->sku; ?><br>
  </div>
  <?php } ?>
  <div style="clear:both;text-align:center;"><?php echo $links; ?></div>
</div>

希望你们能解决我的问题,谢谢!

【问题讨论】:

  • 检查您的基本网址。这可能是问题
  • 为什么要设置 $config['num_links'] = 2 ?

标签: php codeigniter pagination


【解决方案1】:

不知道分页不起作用意味着什么(如果可以,请解释更多)但根据我的观察,您没有像

那样在 $config 数组中添加偏移量

$config['offset'] = 'uri_segment or 0'

您必须在查询中使用限制方法,例如

limit(50, $offset);

【讨论】:

  • 不起作用。搜索功能仍然从数据库中获取所有数据,我的意思是分页 $config['per_page'] 不起作用。
  • 抱歉,您必须在查询中设置限制方法,如下所示: $this->db->limit($config['per_page'], $config['offset']);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
  • 2020-03-17
  • 2012-08-22
  • 2014-06-03
相关资源
最近更新 更多