代码点火器分页用于显示数据库中的特定数据?

时间:2013-03-29 10:11:27

标签: codeigniter

我想通过使用CI的分页来显示数据库中的一些特定数据。但是函数中的默认passess参数分页为我创造了问题。

这是我的控制器

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并且不显示数据。 我该怎么办?

2 个答案:

答案 0 :(得分: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();

    }

答案 1 :(得分: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();
}