问题在分页上计数表上的行?

时间:2011-08-14 17:55:29

标签: php codeigniter count pagination

我使用codeigniter

我在以下代码中遇到rownum的问题。就是这样,在表each page of pagination中的be counted from the beginning行中。对于示例,请清除:

P1:如果我们在分页的第一页,则行数是:

1 Columns1
2 Columns2
3 Columns3
4 Columns4

P2:如果我们在分页的第二页,则行是计数:

1 Columns5
2 Columns6
3 Columns7
4 Columns8


P2:我希望计入表中的行,(在第二页的分页和...):

5 Columns5
6 Columns6
7 Columns7
8 Columns8

P3: ......等等

此代码:

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

完整代码:

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

怎么回事?

尊重

编辑以及有关此内容的更多信息:

我不希望在每个分页页面中,行从头开始计算 每个上一页都将计算下一页。

看到这张图片: enter image description here enter image description here enter image description here

我的观点:

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

1 个答案:

答案 0 :(得分:2)

你有两个问题:

1)你没有传递抵消。

2)你没有告诉Pagination类使用哪个URI段。

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

修改

现在您的代码运行良好,我们可以解决您的问题(我假设您正在使用MySQL):

您尝试使用@rownum获取行号,但MySQL不支持@rownum

但是,有一种解决方法(来自here的想法)。将您的查询更改为:

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