0

I want to put pagination on my view page of matrimonial project . In this project flow of variables pass in this way i.e from View-page(search form)- to - controller -to- model -to- view page...What i did is i passed values from view-controller-model-view. In result page(i.e in view page) I'm able to get total number of records and total number of pages and also able to get individual values of each record.

Here starts my question now i want to add pagination on result page (i.e view page) I'm posting controller, model, and view page.

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

MODEL PAGE

 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'];
        ?>

`What i tried to do in above script is to get query result along with number of records and number of pages through which i thought i can do pagination work but i was wrong. so please ci developers help me out in writing pagination script. I am completely new to Ci framework.

5
  • 1
    Are you using Codeigniter's pagination library or are you wanting to build your own?
    – Rwd
    Jul 30, 2014 at 8:49
  • I'd highly recomend trying to use using CI's paginatin library before writing your own! =] Jul 30, 2014 at 8:51
  • what you want me to do
    – vicky
    Jul 30, 2014 at 8:59
  • I am completely new so thinking in what way to go
    – vicky
    Jul 30, 2014 at 9:01
  • what ever you suggest to follow i will follow
    – vicky
    Jul 30, 2014 at 9:02

2 Answers 2

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

}

1
  • patil need so clarity ru there
    – vicky
    Jul 30, 2014 at 11:58
0

use ci pagination library e.g.

controller

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

view:

display links on view

5
  • model is not necessary always, u can also do without model
    – mokNathal
    Jul 30, 2014 at 12:58
  • can you explain clearly what i changes i need to do in model controller and in my view
    – vicky
    Jul 30, 2014 at 13:09
  • include $this->load->library("pagination"); your r taking query result from model instead you can right query in controller itself
    – mokNathal
    Jul 31, 2014 at 6:06
  • so what you want me to do i am new
    – vicky
    Jul 31, 2014 at 6:07
  • just add code in controller where $config['total_rows']=query is count of result rows and show $links in view
    – mokNathal
    Aug 1, 2014 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.