Welcome Guest, Not a member yet? Register   Sign In
Please help with Pagination - SOLVED
#1

[eluser]laytone[/eluser]
I'm trying ti implement pagination for my first time. Its working, but the numbers at the bottom of don't update to the current page. i.e. if you click '3' the third page shows up but the pagination navigation still shows page one. What am I doing wrong? Please help!

If I click on page three this is the uri I come up with and results 41 - 60 show up:
/salesmanager/searchsales/search/40

But pagination still says 1 2 3 > Last ›

sorry my controller is a little messy but you know how it is the first time you try to do something!

Controller Function:
Code:
public function search($start = '0', $page = '1'){
        if($this->input->post('searchsubmit')){
            $searchdata = array(
                'bdate' => date('Y-m-d', strtotime($this->input->post('bdate'))). ' 00:00:00',
                'edate' => date('Y-m-d', strtotime($this->input->post('edate'))). ' 23:59:59',
                'search_by' => $this->input->post('search_by'),
                'search_for' => $this->input->post('search_for'),
                'show_all' => $this->input->post('show_all'),
                'traffic_source' => $this->input->post('traffic_source'),
            );
            $cs = $searchdata;
            $this->session->set_userdata('cs', $cs);
        }
        $this->searchdata = $this->session->userdata('cs');
        if (!$this->searchdata){
            redirect ($this->myPreuri . '/index');
            return;
        }
        $this->searchdata['start'] = $start;
        $this->searchdata['limit'] = '20';
        $this->session->set_userdata('cs', $this->searchdata);
        
        $this->Mymodel->searchdata = $this->searchdata;
        $count = $this->Mymodel->get_count();
        $result = $this->Mymodel->dosearch();
        
        if(!$result){
            echo "No Results for this search use the back button and try again!";
            return;
        }    
        
        $pagedata['sr'] = $result;
        $pagedata['count'] = $count;
        $pagedata['start'] = $start;
        $pagedata['limit'] = '20';
        
        $this->load->library('pagination');
        
        $config['base_url'] = base_url().'/'.$this->myPreuri.'/search/';
        $config['total_rows'] = $count;
        $config['per_page'] = '20';
        $config['uri_segment'] = 5;
        $config['cur_page'] = $count / $start;
        $this->pagination->initialize($config);
        
        $this->_showPage('searchresults', $pagedata, "Search Results" );
        
    }

My Model:
Code:
class Managesalesmodel extends Model {
    
    var $searchdata = '';
    
    public function __construct(){
        parent::Model();
    }
    
    function setupquery(){
        $search = (object)$this->searchdata;
        $this->db->select('id,first_name, last_name, active, email, phone, zip');
        //first set the date range
        $this->db->where('register_date >=', $search->bdate);
        $this->db->where('register_date <=', $search->edate);
        //then what set the by what for what
        $this->db->like($search->search_by, $search->search_for);
        //then we filter the ocmpany
        if ($search->traffic_source != 'all')
            $this->db->where('traffic_source', $search->traffic_source);
        //Set the limit
        $this->db->from('people');    
    }
    
    public function get_count(){
        $this->setupquery();
        return $this->db->count_all_results();
    }
    
    public function dosearch(){
        $this->setupquery();
        $search = (object)$this->searchdata;
        $this->db->limit($search->limit, $search->start);
        //Get the result
        $query = $this->db->get();
        //Return the result!
        return $query->num_rows() >= 1 ? $query->result() : FALSE;
    }
}

My View:

Code:
<table border="0" align="center" cellpadding="3" cellspacing="1" style="border: 1px solid black;">
  <tr>
      <th scope="col">#</th>
    <th scope="col" nowrap="nowrap">Name</th>
    <th scope="col">Email</th>
    <th scope="col">Phone</th>
    <th scope="col">Zip</th>
  </tr>
  &lt;?php
      $rnum = $start;
    foreach($sr as $r):
        ++$rnum;
        $bgcolor = 'cccccc';
        if (($rnum % 2) == 0) $bgcolor = 'dddddd';
    ?&gt;
  <tr bgcolor="#&lt;?php echo $bgcolor; ?&gt;">
      <td>&lt;?php echo $rnum; ?&gt;
    <td nowrap="nowrap">&lt;?php echo anchor('salesmanager/manageperson/index/'.$r->id, $r->first_name. ' ' .$r->last_name); ?&gt;</td>
    <td nowrap="nowrap">&lt;?php echo $r->email; ?&gt;</td>
    <td nowrap="nowrap">&lt;?php echo $r->phone; ?&gt;</td>
    <td nowrap="nowrap">&lt;?php echo $r->zip; ?&gt;</td>
  </tr>
  &lt;?php endforeach; ?&gt;
</table>
&lt;?php echo $this->pagination->create_links();    ?&gt;
#2

[eluser]LuckyFella73[/eluser]
Hi Laytone,

after starring at your code for half an hour this line
came into my focus:
Code:
$config['cur_page'] = $count / $start;

The current page is calculated in the pagination class.
You set this value manually and "overwrite" the value
calculated by the class. $count is allway = 20
On page 2 $start = 20 by this you set act. page=1
because 20 / 20 = 1
sorry for my bit poor english, sometime it's hard to
express your thoughts in an other language ...

Try to comment out this line and see what happens.
#3

[eluser]laytone[/eluser]
[quote author="LuckyFella73" date="1273110706"]Hi Laytone,

after starring at your code for half an hour this line
came into my focus:
Code:
$config['cur_page'] = $count / $start;

The current page is calculated in the pagination class.
You set this value manually and "overwrite" the value
calculated by the class. $count is allway = 20
On page 2 $start = 20 by this you set act. page=1
because 20 / 20 = 1
sorry for my bit poor english, sometime it's hard to
express your thoughts in an other language ...

Try to comment out this line and see what happens.[/quote]

Thank you for your help. I actually added this line as a last resort to see if it might make it work. I removed it because I got a division by zero on page 1 and it made no difference. I should have removed it before I posted my code.
#4

[eluser]laytone[/eluser]
I solved my problem by setting the $config['uri_segment'] = 4 rather then 5.




Theme © iAndrew 2016 - Forum software by © MyBB