我想在codeigniter框架中将分页放在我的页面上

时间:2014-07-30 08:42:46

标签: php codeigniter pagination

我想在我的婚姻项目视图页面上加以分页。 在这个项目中,变量流以这种方式传递,即从View-page(搜索表单)到 - controller -to-model-to-view page ...我做的是我从view-controller-model-view传递值。 在结果页面(即在视图页面中),我能够获得记录总数和总页数,并且能够获得每个记录的单个值。

现在开始我的问题我想在结果页面上添加分页(即查看页面) 我发布了控制器,模型和视图页面。

CONTROLLER PAGE

$per_page=$this->input->post('per_page');
$look = $this->input->post('look');
$age = $this->input->post('age'); 
$age_from = $this->input->post('age_from');
$age_to = $this->input->post('age_to');
$se_ct = $this->input->post('sect');
$subsect = $this->input->post('subsect');
$coun_try = $this->input->post('country');
$sta_te = $this->input->post('state');
$ci_ty = $this->input->post('city');
$qualification = $this->input->post('qualification');

$data['showdata']   =   $this->searchresultss->login($per_page,$look,$age, $age_to,$age_from,$se_ct,$subsect,$coun_try,$sta_te, $ci_ty,$qualification);

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

模型页面

 function login($per_page=3,$look,$age,$age_to,$age_from,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification)
 {
$query="SELECT *
FROM users
WHERE  

if('$se_ct'!='',sect =  '$se_ct' AND if('$subsect' !='',subsect =  '$subsect',subsect like  '%%'),sect like  '%%' AND subsect like  '%%')
AND
IF( '$coun_try' !='', country =  '$coun_try'
AND 
if('$sta_te' !='', state =  '$sta_te'
AND  
if('$ci_ty' !='',city =  '$ci_ty',city like  '%%'),state LIKE  '%%'
AND city LIKE  '%%'), country LIKE  '%%'
AND state LIKE  '%%'
AND city LIKE  '%%' ) 
AND age >=  '$age_from'
AND age <=  '$age_to'
AND 
IF('$qualification' !='',qualification =  '$qualification',  qualification LIKE  '%%' ) 
And gender = '$look'
And status='1'";
$data=array();
$query=$this->db->query($query);
$data['results']=$query->result_array();
$data['count']=$query->num_rows();

$data['pages']=ceil($data['count']/3);

return $data;
 }
}

MY VIEW PAGE(I.E.RESULT PAGE)

<?php
//echo "<pre>";
//print_r($showdata);
if (isset($showdata)){
            foreach ($showdata['results'] as $k => $v) {
                ?>
                <table width="200" border="1">
  <tr>
    <td>  <?php echo $v['gender'];?></td>
    <td> <?php echo $v['email'];?></td>
  </tr>

</table>



   <?php
            }
        }
        echo $showdata['count'];  echo $showdata['pages'];
        ?>

`我在上面的脚本中尝试做的是获取查询结果以及我认为可以进行分页工作的记录数和页数,但我错了。所以请ci开发人员帮我编写分页脚本。我是Ci框架的新手。

2 个答案:

答案 0 :(得分:0)

使用ci分页库,例如

控制器

 $config = array();
            $config["base_url"] = url;
            $config["total_rows"] = query;
            $config["per_page"] = 5;
            $config["uri_segment"] = 4;
            $config["first_link"] = 'First';
            $config["last_link"] = 'Last';
            $config["prev_link"] = 'Previous';
            $config["next_link"] = 'Next';
            $this->pagination->initialize($config);

            $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

            $this->db->limit($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();

视图:

在视图上显示链接

答案 1 :(得分:0)

    If you want use CI pagination below code help for you

    public function paginationFunction($pg = 0)                     
    {

       $query="SELECT *
               FROM users
               WHERE  
              if('$se_ct'!='',sect =  '$se_ct' AND if('$subsect' !='',subsect =  '$subsect',subsect like  '%%'),sect like  '%%' AND subsect like  '%%')
    AND
    IF( '$coun_try' !='', country =  '$coun_try'
    AND 
    if('$sta_te' !='', state =  '$sta_te'
    AND  
    if('$ci_ty' !='',city =  '$ci_ty',city like  '%%'),state LIKE  '%%'
    AND city LIKE  '%%'), country LIKE  '%%'
    AND state LIKE  '%%'
    AND city LIKE  '%%' ) 
    AND age >=  '$age_from'
    AND age <=  '$age_to'
    AND 
    IF('$qualification' !='',qualification =  '$qualification',  qualification LIKE  '%%' ) 
    And gender = '$look'
    And status='1'";

    $data=array();   
    $query=$this->db->query($query)->result_array();
    $totalRecord=$query->num_rows();

             /* [Start:: Pagination code] */

            $config['base_url'] = base_url() . 'test'; //page url i.e.controller name 
            $data['count'] = $totalRecord;
            $config['total_rows'] = $totalRecord;
            $config['per_page'] = 2;  //per page records
            $config['cur_page'] = $pg;  //third parameter first time is 0 
            $config['num_links'] = 4;

            $config['cur_tag_open'] = '<li><strong><a class="active" href="javascript:void(0);">';
            $config['cur_tag_close'] = '</a></strong></li>';
            $config['prev_link'] = 'Previous';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['next_link'] = 'Next';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['full_tag_open'] = '<ul>';
            $config['full_tag_close'] = '</ul>';
            $this->pagination->initialize($config);
            $data["pageLinks"] = $this->pagination->create_links();
            $data['page'] = $pg;

            //run same query but add limit 
           $query="SELECT *
FROM users
WHERE  

if('$se_ct'!='',sect =  '$se_ct' AND if('$subsect' !='',subsect =  '$subsect',subsect like  '%%'),sect like  '%%' AND subsect like  '%%')
AND
IF( '$coun_try' !='', country =  '$coun_try'
AND 
if('$sta_te' !='', state =  '$sta_te'
AND  
if('$ci_ty' !='',city =  '$ci_ty',city like  '%%'),state LIKE  '%%'
AND city LIKE  '%%'), country LIKE  '%%'
AND state LIKE  '%%'
AND city LIKE  '%%' ) 
AND age >=  '$age_from'
AND age <=  '$age_to'
AND 
IF('$qualification' !='',qualification =  '$qualification',  qualification LIKE  '%%' ) 
And gender = '$look'
And status='1' limit $pg,$config['per_page']"; 

            $data['results']=$query->result_array();

}