用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2629|回复: 7
收起左侧

[已解决] 分页问题

[复制链接]
发表于 2010-9-15 15:04:58 | 显示全部楼层 |阅读模式
控制器里面代码
PHP复制代码
//分页        
function customer_phone($offset=''){
            $this->load->library('pagination');
            $data['baseurl']=site_url();
            $limit=1;
            $condition="from tel_count as a join customer as b on a.cus_id=b.id";
                $totals=$this->admin_model->count_product($condition);
                $total=$totals[0]->count;
                $data['customer_phone']=$this->admin_model->customer_phone_info($limit,$offset);
                $config['base_url'] = base_url().'home/customer_phone/';
                $config['total_rows'] = $total;
                $config['per_page'] = $limit;
                $this->pagination->initialize($config);
                $data['pag_links']= $this->pagination->create_links();
        $this->load->view('customer_phone.html',$data);
}
复制代码

PHP复制代码
//模板里面方法
function  customer_phone_info($limit,$offset){
           $this->load->database();
           $this->db->limit($limit,$offset);
       $query_string="select a.*,b.* from tel_count as a join customer as b on a.cus_id=b.id";
       $result=$this->db->query($query_string);
       $resu=$result->result();
       return $resu;
}
function count_product($condition)  
{
    $query="SELECT COUNT(*) AS count {$condition}";
    //echo $query; exit;
    $result=$this->db->query($query);
    return $result->result();
}
复制代码

HTML复制代码
//视图里面代码啊!
<?php  if(!empty($customer_phone)){
foreach($customer_phone as $cus_ph){
echo "<tr>
    <td>{$cus_ph->id}</td>
    <td>{$cus_ph->cusName}</td>
    <td>{$cus_ph->cusMobile}</td>
    <td>{$cus_ph->cusEmail}</td>
    <td>{$cus_ph->cusUrl}</td>
    <td>{$cus_ph->cusCreateName}</td>
    <td>{$cus_ph->callin_tel}</td>
   <td>{$cus_ph->callin_status}</td>
    <td>{$cus_ph->callin_time}</td>
    <td>修改</td>
  </tr>";
   } }else{
          echo "<tr><td colspan='7'>你没有查到想要内容</td></tr>";
  }
?>
<tr class="page">
  <td colspan="7"><?php  echo $pag_links; ?></td>
</tr>
 
复制代码

我设置每页显示1条  但是我现在还是全部显示! 我不知道什么原因!
没有检查出错。如果知道告诉一声!谢谢啊!
 楼主| 发表于 2010-9-15 15:15:41 | 显示全部楼层
还行要改什么配置文件什么地方吗?
 楼主| 发表于 2010-9-15 15:21:20 | 显示全部楼层
333333333333.jpg 我查到数据库只有两条数据
 楼主| 发表于 2010-9-15 15:22:00 | 显示全部楼层
我设置每页显示1条信息!
发表于 2010-9-15 15:31:25 | 显示全部楼层
本帖最后由 ywqbestever 于 2010-9-15 15:37 编辑

模型里你怎么又用ar方法,又自己写sql语句啊,你要统一啊,你把limit和offset写你的$query_string 里面去

$this->db->limit($limit,$offset);这句删掉
$query_string="select a.*,b.* from tel_count as a join customer as b on a.cus_id=b.id ";
改成
$query_string="select a.*,b.* from tel_count as a join customer as b on a.cus_id=b.id limit $limit ,$offset ";

另外参数传递也有问题,你好好研究一下吧,理解CI的分页透彻点
发表于 2010-9-15 15:47:16 | 显示全部楼层
肚子有点疼 晚些时候来看
 楼主| 发表于 2010-9-15 16:13:56 | 显示全部楼层
function index() {
    $html = array();
    $this->load->library('pagination');
    $config['total_rows'] = $this->模型->fetch_count();
    $config['base_url'] = site_url(控制器名 . 'index');
    $this->pagination->initialize($config);
    $html['links'] = $this->pagination->create_links();
    $this->模型->set_limit($this->pagination->limit_string());
    //你要显示的数据
    $html['记录数组'] = $this->模型->fetch_result();
    $html['total_rows'] = $config['total_rows'];
    $this->load->view('view_path', $html);
}
这样写逻辑比较好
public function limit_string()
    {
        return $this->per_page . "," . $this->offset();
    }

    public function offset()
    {
        $total_page = ceil($this->total_rows / $this->per_page);
        if ($this->cur_page > $total_page)
            $this->cur_page = $total_page;
        if ($this->cur_page < 1 || ! is_numeric($this->cur_page))
            $this->cur_page = 1;
        return (int)($this->cur_page - 1) * $this->per_page;
    }
加在 Pagination
 楼主| 发表于 2010-9-15 16:14:53 | 显示全部楼层
$config['uri_segment'] = 4;
这个只有当你的方法有参数时 才增加。一个参数加 1
原因:
//$CI->uri->segment 修改为$CI->uri->rsegment kis
                        if ($CI->uri->rsegment($this->uri_segment) != 0)
                        {
                                $this->cur_page = $CI->uri->rsegment($this->uri_segment);

                                // Prep the current page - no funny business!
                                $this->cur_page = (int) $this->cur_page;
                        }

本版积分规则