lihuan34 发表于 2012-11-18 10:47:04

求助:分页返回第一页时无法获取数据

查询数据时,点下一页没问题,但向前返回到第一页时,就无法获取数据,通过查看路径得知是无法获取数据查询起码位置
如:每页20条记录

第二页
xxx/xxx/20
第三页
xxx/xxx/40

当返回第一页时路径为
xxx/xxx/

因此无法获取第一页数据

控制器:

function customer_list()//查询客户资料列表页
{
$this->load->library('pagination');
$this->load->model('Madmin');
$data['baseurl'] = $this->base_url;
$data['get_data'] = FALSE;
$data['get_category'] = $this->Madmin->get_category();
//$this->load->library('form_validation');
//$this->form_validation->set_rules('SearchText', 'Username', 'required');
//$this->form_validation->set_rules('category_id', 'Username', 'required|');
$action = $this->input->post('act');
//if ($action == 'del' or $action == 'move'){
//   $ids = $_POST['ID'];
//   print_r($ids);
//}
$ic_number = $this->input->post('ic_number');
$phone = $this->input->post('phone');
$post_code = $this->input->post('post_code');
$sex = $this->input->post('sex');
$cureent_rows = $this->uri->segment(5,0);
if (strlen($ic_number) > 0 or strlen($phone) > 0 or strlen($post_code) > 0 or $this->uri->segment(5,0)>0){
   if (strlen(trim($post_code))==0){
    $post_code = $this->uri->segment(3,0);
   }
   if (strlen(trim($sex))==0){
    $sex = $this->uri->segment(4,0);
   }
   $data['total'] = $this->Madmin->search_customer_total($ic_number,$phone,$post_code,$sex);
   $config['total_rows'] = $data['total'];
   $config['per_page'] = 20;
   $config['uri_segment'] = 5;
   $config['num_links'] = 4;
   $config['base_url'] = $this->base_url.'index.php/admin/customer_list/'.$post_code.'/'.$sex.'/';
   $config['first_link'] = "<img src='../images/Main/first.gif' border='0'>";
   $config['last_link'] = "<img src='../images/Main/last.gif' border='0'>";
   $config['next_link'] = "<img src='../images/Main/next.gif' border='0'>";
   $config['prev_link'] = "<img src='../images/Main/back.gif' border='0'>";
   $this->pagination->initialize($config);
   $data['get_customer_list'] = $this->Madmin->search_customer_list_page($ic_number,$phone,$post_code,$sex,$cureent_rows,$config['per_page']);
   $data['get_data'] = TRUE;
}
$this->load->view('customer/customer_list',$data);
}


模型:
//返回查询数据的记录数
function search_customer_total($ic_number,$phone,$post_code,$sex)
{
$sql = "SELECT * FROM `customer`";
$i = 0;
if (strlen($ic_number)>0){
   $i = $i + 1;
   $sql = $sql . " WHERE ic_number='$ic_number'";
}
if (strlen($phone)>0){
   $i = $i + 1;
   if ($i==1) {
    $sql = $sql . " WHERE mobile_phone LIKE '%$phone%' OR other_phone LIKE '%$phone%'";
   }else{
    $sql = $sql . " and (mobile_phone LIKE '%$phone%' OR other_phone LIKE '%$phone%')";
   }
}
if (strlen($post_code)>0 and $post_code!=='0'){
   $i = $i + 1;
   if ($i==1){
    $sql = $sql . " WHERE postcode='$post_code'";
   }else{
    $sql = $sql . " and postcode='$post_code'";
   }
}
if ($sex > 0){
   $sql = $sql . " and sex=$sex";
}
$sql = $sql . " ORDER BY id DESC";
$query = $this->db->query($sql);
return $query->num_rows();
}

//查询客户数据并分页显示
function search_customer_list_page($ic_number,$phone,$post_code,$sex,$offset,$limit)
{
$sql = "SELECT * FROM `customer`";
$i = 0;
if (strlen($ic_number)>0){
   $i = $i + 1;
   $sql = $sql . " WHERE ic_number='$ic_number'";
}
if (strlen($phone)>0){
   $i = $i + 1;
   if ($i==1) {
    $sql = $sql . " WHERE mobile_phone LIKE '%$phone%' OR other_phone LIKE '%$phone%'";
   }else{
    $sql = $sql . " and (mobile_phone LIKE '%$phone%' OR other_phone LIKE '%$phone%')";
   }
}
if (strlen($post_code)>0 and $post_code!=='0'){
   $i = $i + 1;
   if ($i == 1){
    $sql = $sql . " WHERE postcode='$post_code'";
   }else{
    $sql = $sql . " and postcode='$post_code'";
   }
}
if ($sex > 0){
   $sql = $sql . " and sex=$sex";   
}
$sql = $sql . " ORDER BY id DESC LIMIT $offset,$limit";
$query = $this->db->query($sql);
return $query->result();
}

lihuan34 发表于 2012-11-19 12:35:25

自己顶下。。

ichou 发表于 2012-11-19 12:48:11

本帖最后由 ichou 于 2012-11-19 12:49 编辑

代码好长 我就不看了
{:soso_e113:}
纯帮顶咯哦

ichou 发表于 2012-11-19 12:50:48

建议你echo一下sql语句 看看哪里出问题

lihuan34 发表于 2012-11-20 09:51:42

ichou 发表于 2012-11-19 12:50 static/image/common/back.gif
建议你echo一下sql语句 看看哪里出问题

看过,但返回到第一页时,获取不到值“0”

但控制器里明明 $this->uri->segment(5,0); 这样写,没值时默认为0

奇怪在这里

lihuan34 发表于 2012-11-20 17:51:33

弄出来拉。。是逻辑错误。。

goyuquan 发表于 2014-4-11 16:07:18

我也显示不出来第一页,正在找呢{:soso_e130:}

小花鱼 发表于 2014-9-15 13:27:07

$this->load->view('customer/customer_list',$data);

这个数据在customer_list.php文件里怎么取?
页: [1]
查看完整版本: 求助:分页返回第一页时无法获取数据