Welcome Guest, Not a member yet? Register   Sign In
Pagination error during link creation
#1

[eluser]zsela[/eluser]
Hi everybody!

I ran into a strange problem during paginating a page. This is the first time I do this with CI, so probably it is just a small inadvertence.

The links created by CI are not growing one at a time, but growing by 2, 3 or whatever the per_page value is. For example if $config[per_page] = 3, then the links are like these:
../list_users/3
../list_users/6

Here is the code, from the controller:

Code:
// pagination
$this->load->library('pagination');
$config['base_url'] = base_url() .'index.php/admin/list_users/';
$config['per_page'] = 3; // találatok száma oldalanként
$config['num_links'] = 2;
$config['first_link'] = '<<';
$config['last_link'] = '>>';
  
$page = $this->uri->segment(3); // oldal index
    
$count_user = $this->Admin_model->get_all_users_num();
$all_user = $this->Admin_model->get_users($page, $config['per_page']);
    
$config['total_rows'] = $count_user;
$this->pagination->initialize($config);
    
$this->load->view('admin/manage_users', array('all_user' => $all_user, 'count_user' => $count_user, 'config' => $config, 'page' => $page));

And from the view:

Code:
$lThr = $page * $config['per_page'] + 1;
if (($page + 1) * $config['per_page'] >= $config['total_rows']) $uThr = $config['total_rows'];
else $uThr = ($page + 1) * $config['per_page'];
  
echo '<p class="twelve">Oldalak: ';
echo $this->pagination->create_links();
echo '</p>';
    
echo '<p class="right">';
echo 'Találatok: ' . $lThr . ' - ' . $uThr . ' / ' . $config['total_rows'];
echo '</p>';

Thanks for your answers in advance!
#2

[eluser]zsela[/eluser]
Can anyone shed any light on this?
#3

[eluser]keevitaja[/eluser]
ci gives you the offset in uri, not the page number

in your case, /list_users/6 stands for limit 3, offset 6

in pagination links, the page number is shown!
#4

[eluser]InsiteFX[/eluser]
Code:
// -----------------------------------------------------------------------

    /**
     * get_all()
     *
     * Call:
     * $data['all_users'] = $this->Admin_model->get_users($this->config["per_page"], $offset);
     *
     * @access public
     * @param string
     * @param string
     * @return mixed
     */
    public function get_all($limit = 0, $offset = 0)
    {
        $query = $this->db->get('Your_Table_Name', $limit, $offset);
  
        if ($query->num_rows() > 0)
        {
           return $query->result();
        }
  
        return FALSE;
    }

    // -----------------------------------------------------------------------

    /**
     * get_count()
     *
     * Gets a count of all returned records.
     *
     * $config['total_rows'] = $this->Admin_model->get_count();
     *
     * @access public
     * @return boolean
     */
    public function get_count()
    {
     return $this->db->count_all('Your_Table_Name');
    }

Also your view is wrong, you should be using a foreach loop on the returned data.

View:
Code:
<table cellpadding='0' cellspacing='0' id='paged-table'>
  <thead>
   <tr>
    <th>ID</th>
    <th>Name</th>
   </tr>
  </thead>

  <tbody>
   &lt;?php $i = 0; ?&gt;
   &lt;?php foreach($all_users as $item): ?&gt;
    <tr class="&lt;?php echo ($i % 2 == 0) ? "even" : "odd"; ?&gt;">
     <td>&lt;?php echo $item->id; ?&gt;</td>
     <td>&lt;?php echo $item->name; ?&gt;</td>
     &lt;!--<td> any function like edit, delete, etc </td>--&gt;
    </tr>
    &lt;?php $i++; ?&gt;
   &lt;?php endforeach; ?&gt;
  </tbody>
</table>

<br />

&lt;?php echo $this->pagination->create_links(); ?&gt;

You will need to create a css class for the even and odd.
#5

[eluser]zsela[/eluser]
Thanks for your answers! It is working now!
My view contains the foreach loop for the results, I just didn't copy it here Smile

Thanks again for your help!




Theme © iAndrew 2016 - Forum software by © MyBB