0

我想通过使用 CI 的分页显示数据库中的一些特定数据。但默认情况下,分页在函数中传递参数,这给我带来了问题。

这是我的控制器

function page($catagoryid){

 $this->load->library('pagination');
 $config['base_url'] = base_url().'index.php/pagination/page/'; 

$count = $this->db->query("select * from tbl_products where category_id='$categoryid'");
 $total=$count->num_rows();

 $per_page = 4;
 $config['total_rows'] = $total;
 $config['per_page'] = $per_page;
 $this->pagination->initialize($config);
 $data['pagination'] = $this->pagination->create_links();
 $this->load->model("mpagination");
 $data['list'] = $this->mpagination->get_s(,$categoryid,$config['per_page'],$this->uri->segment(3));
 if ($data['list']!== null){

 $this->load->view('pagination_view', $data);
 }
else {
$this->load->view('noresult');

} 
 }

这是我的模型

function get_s($categoryid,$num, $offset) {


$this->db->select ('*');
        ->where('category_id',$categoryid);  // field name

$sql = $this->db->get('tbl_products',$num, $offset); // table name
if ($sql->num_rows () >0) {

    return $sql->result();
 }
 else {
 return null;
}
}

这是我的看法

 <?php
 foreach($list as $row):?>




    <?php echo $row->_prduct_id."   ".$row->name."   ".$row->description;?>
    <br/>



  <?php endforeach;?>
  <br/>
     <?php echo $pagination; ?>

这段代码一开始可以工作,但是当我点击分页的第二个链接时它不会工作,因为默认情况下分页将其值作为参数发送到 url 并且不显示数据。我能做些什么?

4

2 回答 2

0

我没有正确理解您的代码,但这是您进行分页所需的方式。

public function page($category_id)
{
    $result = $this->model_file->model_function($category_id);

    $start_index = 0;

    $total_rows = count($result);

    $items_per_page = 10;

    $filtered = array_splice($result,$start_index,$items_per_page);

    $model['data'] = $filtered;

    $model['page_link'] = create_page_links (base_url()."/controller_file_name/page", $items_per_page, $total_rows);

    $this->load->view('view_file_name_path',$model);

    }

    function create_page_links($base_url, $per_page, $total_rows) {

    $CI = & get_instance();
    $CI->load->library('pagination');

    $config['base_url'] = $base_url;
    $config['total_rows'] = $total_rows;
    $config['per_page'] = $per_page; 

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

    return $CI->pagination->create_links();

    }
于 2013-03-29T10:23:28.677 回答
0

像这样试试

public function index($offset = 0) 
{
    $language_id = 1;
    $this->load->library('pagination');
    $limit = 10;

    $total = $this->legend_model->get_legend_count($language_id);

    $config['base_url'] = base_url().'legend/index/';
    $config['total_rows'] = $total;
    $config['per_page'] = $limit;
    $config['uri_segment'] = 3;

    $config['first_link'] = '<< First';
    $config['last_link'] = 'Last >>';
    $config['next_link'] = 'Next ' . '&gt;';
    $config['prev_link'] = '&lt;' . ' Previous';
    $config['num_tag_open'] = '<span class="number">';
    $config['num_tag_close'] = '</span>';

    $config['cur_tag_open'] = '<span class="current"><a href="#">';
    $config['cur_tag_close'] = '</a></span>';

    $this->pagination->initialize($config);
    $data['offset'] = $offset;
    $data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);

    $this->template->write('title', 'Legend : Manage Legend');
    $this->template->write_view('content', 'legend/index', $data);
    $this->template->render();
}
于 2013-03-29T10:36:17.943 回答