hkstb 发表于 2009-7-5 02:08:31

分页功能无法实现,可是也不报错。

分页功能无法实现,可是也不报错。

1、单击第2页,地址会成为:http://localhost/codeigniter/index.php/blog/index/6
2、$config['per_page'] = 6;设置每页6条,可是23条数据还是全部显示在一页上。

版本1.7.1
blogmodel.php 模型中查询数据库的函数

function get_last_ten_entries()
    {
      $this->db->order_by("blog_id", "desc");
      $query = $this->db->get('blog');
      return $query->result();
    }


blog.php 控制器

class Blog extends Controller {
function __construct()
{
parent::Controller();
      $this->load->helper('url');
      $this->load->helper('form');
      //$this->load->scaffolding('blog');
}
function index()
{
      $this->load->database();
      $this->load->library('pagination');
      $config['base_url'] = base_url().'index.php/blog/index/';
      $config['total_rows'] = $this->db->count_all('blog');
      $config['per_page'] = 6;
      //$config['uri_segment'] = 1;
      $config['full_tag_open'] = '<p>';
      $config['full_tag_close'] = '</p>';
      $this->pagination->initialize($config);
      $datas['titles']="这是网站标题";
      $datas['h1']="这是h1标题";
      $this->load->model('blogmodel');
      $datas['query'] = $this->blogmodel->get_last_ten_entries();
$this->load->view('blog_view',$datas);
}
}



blog_view.php 视图

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $titles ?></title>
<style type="text/css">
<!--
.cass {
background-color: #F0F8FF;
    margin: 5px;
    border: 1px solid #8EC7FF;
}
-->
</style>
</head>
<body>
<h1><?php echo $h1 ?></h1>
<?php foreach($query as $row): ?>
<div class="cass">
<h3><?php echo $row->title ?></h3>
<p><?php echo $row->content ?></p>
</div>
<?php endforeach; ?>
<?php echo $this->pagination->create_links(); ?>
<p><?php echo anchor('blog/addblog','添加日志') ?></p>
</body>
</html>

visvoy 发表于 2009-7-5 08:18:01

MySQL还是SQL Server?

Hex 发表于 2009-7-5 08:42:55

你的模型写的有问题,仔细根据
http://codeigniter.org.cn/forums/thread-17-1-1.html
修改

hkstb 发表于 2009-7-5 11:30:28

将blogmodel.php 模型中
    function get_last_ten_entries($num, $offset)
    {
      $this->db->order_by("blog_id", "desc");
      $query = $this->db->get('blog', $num, $offset);

      return $query->result();
    }
和blog.php 控制器的参数保持一致,分页效果就出来了。

       $this->load->model('blogmodel');
      $datas['query'] = $this->blogmodel->get_last_ten_entries($config['per_page'],$this->uri->segment(3));

可是我还是不明白
1、单击第2页,地址为什么会成为:http://localhost/codeigniter/index.php/blog/index/6
2、$this->uri->segment(3)是什么意思?

Hex 发表于 2009-7-5 13:06:30

CI 里不是“页数”而是“偏移量”
$this->uri->segment(3) 意思是取 URL 第三段
index.php/第一段/第二段/第三段/...../第n段

hkstb 发表于 2009-7-5 15:17:39

我看了看“CodeIgniter 案例”上的网站,分页都是这样的。 我想问一下,当时CI设计时http://localhost/codeigniter/index.php/blog/index/这里不按页码显示,而使用“偏移量”有什么好处吗?

visvoy 发表于 2009-7-5 17:41:42

因为分页类就是用偏移量的,如果用页码只能自己扩展的说

Hex 发表于 2009-7-5 23:40:29

我想问一下楼主,使用“页码”和“偏移量”有什么本质区别吗?
页: [1]
查看完整版本: 分页功能无法实现,可是也不报错。