Search code examples
phpcodeignitercountrownum

rownum and problem with pagination?


I use of codeigniter and class (library) pagination in it, why after each click on pagination in column #, be counted rows from the beginning.

I want like this( p = pagination ):

p1-> #-> 1 2 3

p2-> #-> 4 5 6

p3 -> #-> 7 8 9

p4 #-> 10

EXAMPLE: see example of my problem.
For see full code go to here: FULL CODE

$data['results'] = $this->db->query("SELECT @rownum:=@rownum+1 rownum, t.*
    FROM (
        SELECT *
        FROM hotel_submits
        ORDER BY id desc
        LIMIT $offset, 3    
    ) t,
    (SELECT @rownum:=0) r");

Rownum in top code is the associated with this problem.

You know how to solve this problem?


Solution

  • Can you retrieve the $offset for the page you are currently on?
    Like if I am on page 1, $offset = 1,
    if I am on page 2, $offset = 4 (1 + (page 2 - 1) * 3)
    if I am on page 3, $offset = 7 (1 + (page 3 - 1) * 3)
    etc.

    If you can't readily get that at the moment, you can pass it from your controller so your view is aware of the current $offset. Then in your code, instead of using $row->rownum, add the value of the current $offset minus 1 since rownum starts 1.

    foreach ($results->result() as $row)
    {
        echo    '<tr><td><input type="checkbox" name="remember_me" value="true" ></td>';
        // echo    '<td>'.$row->rownum.'</td>';
        echo    '<td>'.intval($row->rownum + $offset - 1).'</td>';
        echo    '<td>'.$row->type.'</td>';
        echo    '<td>'.$row->name.'</td>';
        echo    '<td>'.$row->star.' - '.$row->type_star.'</td>';                                
        echo    '<td><span id="'.$row->address.'" class="tooltip">'.$row->address.'</span></td>';
        echo    '<td><span id="'.$row->number_phone.'" class="tooltip">'.$row->number_phone.'</span></td>';
        echo    '<td>'.$row->fax.'</td>';
        echo    '<td>'.$row->site.'</td>';
        echo    '<td>'.$row->email.'</td>';
        echo    '<td>'.$row->date.'</td></tr>';
    }
    

    So on page 2, instead of 1, 2, 3 etc, it should be (1+4-1) 4, (2+4-1) 5, (3+4-1) 6.
    I hope that makes sense.