使用Codeigniter生成页码链接时出错

时间:2015-10-22 02:53:23

标签: php codeigniter

我正在使用Codeigniter创建分页,我的问题是我在显示网址中的页码链接时出错。

示例我转到第2页,我的网址将是这样的:

http://localhost/my_project/inbound/listing/1

如果第3页

http://localhost/my_project/inbound/listing/2

这是我的控制器

    $config = array();
    $config['base_url'] = base_url('inbound/listing');
    $config['total_rows'] = $this->mod->countList();
    $config['per_page'] = 1;
    $config['uri_segment'] = 3;

    //fd($config);

    $this->pagination->initialize($config);

    $page   =   ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

    fp($page, 'pink'); //print out result

    $order  =   ($this->input->get('order'))? $this->input->get('order'): '';
    $sort   =   ($this->input->get('sort'))? $this->input->get('sort'): '';

    // Query data
    $data['data_list'] = $this->mod->listing($config['per_page'], $page);  //where, limit, page, field, sort     

    fP($data['data_list']); //print out result

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

然后我的模型用于生成链接:

function listing($limit, $start)
    {

        //DATE_FORMAT(`post_date_added`, "%m/%d/%Y %H:%i") as `proper_post_date_added`,
        $this->db->select('*');
        $this->db->from('inventory');
        $this->db->limit($limit, $start);


        $query = $this->db->get();

        //$rows = $query->result_array();

        if($query->num_rows() > 0) {
            foreach($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;

    }

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

如果您想根据自己的网址转到页码,请修改您的商家信息功能,如下所示:

function listing($limit, $start)
{
    $offset_1 = $start - $limit;
    $offset_2 = $offset_1 < 0 ? 0 : $offset_1;

    //DATE_FORMAT(`post_date_added`, "%m/%d/%Y %H:%i") as `proper_post_date_added`,
    $this->db->select('*');
    $this->db->from('inventory');
    $this->db->limit($limit, $offset_2);


    $query = $this->db->get();

    //$rows = $query->result_array();

    if($query->num_rows() > 0) {
        foreach($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;

}