-2

I use of codeigniter

I have an problem with rownum in following code. it is this that, in each page of pagination rows in table be counted from the beginning. For example, the clear:

P1: If we're on the first page of pagination, rows are the counts:

1 Columns1
2 Columns2
3 Columns3
4 Columns4

P2: If we're on the second page of pagination, rows are the counts:

1 Columns5
2 Columns6
3 Columns7
4 Columns8


P2: The I want to be counted row in table,(on the second page of pagination and ...):

5 Columns5
6 Columns6
7 Columns7
8 Columns8

P3: ... and more

This code:

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

Full code:

function show($offset = 0) 
    {
    $this->load->library('pagination');
        $config['base_url'] = base_url().'admin/accommodation/show';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->db->count_all('hotel_submits');
        $config['per_page'] = '4';          

        $this->pagination->initialize($config);    
            $data['pagination'] = $this->pagination->create_links();                                        

        $offset = (int) $offset; // just to make sure nothing funky gets in here
        $data['results'] = $this->db->query("SELECT @rownum:=@rownum+1 rownum, t.* ".
            "FROM (SELECT @rownum:=0) r, hotel_submits t ".
            "ORDER BY id desc LIMIT $offset, 4");

            ////////////////////////                                

            $this->load->view('admin/accommodation_submit_show', $data); 
}

How is it?

With respect

Editing and more about this:

I do not want that in each page of pagination, rows are counted from the beginning.
The next page will be counted on every prev page.

see this images: enter image description here enter image description here enter image description here

my view:

   <div class="table_show">
    <table>
        <tr>
                <th><input type="checkbox" name="remember_me" value="true" ></th>
                <th>#</th>
                <th>نوع</th>
                <th>نام</th>
                <th>ستاره - نوع</th>
                <th>آدرس</th>
                <th>شماره تماس</th>
                <th>نمابر</th>
                <th>وب سایت</th>
                <th>ایمیل</th>
                <th>زمان ثبت</th>
        </tr>
    <?php
    //echo $this->table->generate($results);    
        foreach ($results->result() as $row)
        {
                echo    '<tr><td><input type="checkbox" name="remember_me" value="true" ></td>';
                echo    '<td>'.$row->rownum.'</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>';
            }
    ?>

    </table>
    <?= $pagination;?>
</div>    
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 2
    I've been chasing you around with your questions: http://stackoverflow.com/questions/7056223/create-jquery-pagination/7057591#7057591, http://stackoverflow.com/questions/7057596/a-database-error-occurred-error-number-1064/7057643#7057643, http://stackoverflow.com/questions/7057829/problem-with-pagination/7057855#7057855, and more. Why do you keep on opening new threads? – Joseph Silber Aug 14 '11 at 17:59
  • @Joseph Silber - Thank you for your reply, The problems are different from each other.Your comments,long question seems that putting in comments? – Nayanthara Judila Aug 14 '11 at 18:16
  • If the other answers were resolved, then accept my answer by click on the check-mark next to the question. You're still not passing in the $offset. See my answer... – Joseph Silber Aug 14 '11 at 18:21
  • You accepted the answer to one question, but not to the others. Stack Overflow is specifically designed so that other people searching for the same question should easily find the answer. If the other answers answered your questions, you should accept them too... – Joseph Silber Aug 14 '11 at 18:26
  • Consider making one question per thread. If a problem depends on another, do it in order. – Alfonso Rubalcava Aug 14 '11 at 18:46
  • @Alfonso Rubalcava - Please see only to problem? – Nayanthara Judila Aug 14 '11 at 18:55

1 Answers1

2

You have 2 problems:

1) You're not passing in the offset.

2) You're not telling the Pagination class which URI segment to use.

function show($offset = 0) // You need the offset
{
    // load pagination class
    $this->load->library('pagination');
    $config['base_url'] = base_url().'admin/accommodation/show';
    $config['total_rows'] = $this->db->count_all('hotel_submits');
    $config['per_page'] = '2';
    $config['uri_segment'] = 4; // You have to tell CI which URI segment to use

    $this->pagination->initialize($config);
    $data['pagination'] = $this->pagination->create_links();

    $offset = (int) $offset; // just to make sure nothing funky gets in here
    $data['results'] = $this->db->query("SELECT @rownum:=@rownum+1 rownum, t.* ".
    "FROM (SELECT @rownum:=0) r, hotel_submits t ".
    "ORDER BY id desc LIMIT $offset, 2");

    $this->load->view('admin/accommodation_submit_show', $data);   
}

EDIT:

Now that your code works well, we can address your problem (I'm assuming you're using MySQL):

You're trying to use @rownum to get the row numbers, but @rownum is not supported in MySQL.

However, there's a workaround (idea taken from here). Change your query to:

$data['results'] = $this->db->query("SELECT @rownum:=@rownum+1 rownum, t.*
    FROM (
        SELECT *
        FROM hotel_submits
        ORDER BY id desc
        LIMIT $offset, 2    
    ) t,
    (SELECT @rownum:=0) r");
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292