goyuquan 发表于 2014-4-2 15:22:14

分布类异常

本帖最后由 goyuquan 于 2014-4-2 15:27 编辑

分页类连接部分不好使,只显示前4个连接,不管在哪页,都是1是当前页,但内容可以连接过去,

前第一页按不了,因为1页始终是当页页状态。
不知道怎么回事

控制器

      public function index($page=0)
      {
                $this->load->helper('url');
                $this->load->database();
                $this->load->library('pagination');
                $this->load->model('paging_model');
               
               
                $config['base_url'] = 'http://localhost/ci/index.php/content/index';
                $config['total_rows']=$this->paging_model->get_content();
                $config['per_page'] = '2';
                $config['first_link'] = '首页';
                $config['last_link'] = '尾页';
               
                $page_num=$this->uri->segment(3)?$this->uri->segment(3):1;
               
                if($page_num==1){$offset=0;}else{$offset=$config['per_page']*($page_num-1); }
               
                $data['content']=$this->paging_model->paging($config['per_page'],$offset);
               
                $this->pagination->initialize($config);
               
               
                $this->load->view('templates/header', $data);
                $this->load->view('content/index', $data);
                $this->load->view('templates/footer');
      }


Paging_model

<?php
class Paging_model extends CI_Model {

      public function __construct()
      {
                parent::__construct();
                $this->load->database();
      }
      
      public function get_content()
      {
                $query = $this->db->get('content');
                return $query->num_rows();
      }
      
      public function paging($num,$offset)
      {
                $query = $this->db->get('content',$num,$offset);
                return $query->result_array();
      }
      
      
}


view/content/index.php

<?php foreach ($content as $content_item): ?>

    <h2><?php echo $content_item['title'] ?></h2>
    <div id="main">
      <?php echo $content_item['text'] ?>
    </div>
    <p><a href="http://localhost/ci/index.php/content/view/<?php echo $content_item['id'] ?>">View article</a></p>

<?php endforeach;echo $this->pagination->create_links();?>

goyuquan 发表于 2014-4-2 17:36:34

已解决,重新梳理了一下程序重写了一遍

现在代码上
控制器
<?php
class Content extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                $this->load->model('content_model');
                $this->load->model('page_model');//分页模型
                $this->load->library('pagination');//分页
        }
       
        public function index($page = 0)
        {
                $config['base_url'] = 'http://localhost/ci/index.php/content/page/';//分页参数,路由配合
                $num = $this->page_model->get_pages('content');
                $config['total_rows'] = $num;
                $config['per_page'] = 3;
                $config['uri_segment'] = '3';
                $this->pagination->initialize($config);
               
                $pageNum=$this->uri->segment(3)?$this->uri->segment(3):1;
                if($pageNum==1){
                        $offset=0;
                }else{
                        $offset=$config['per_page']*($pageNum-1)+1;
                }//以上分页参数
               
                $data['title'] = 'Content archive';
                $data['content'] = $this->page_model->paging('content',$config['per_page'],$pageNum);
               
                $this->load->view('templates/header', $data);
                $this->load->view('content/index');
                $this->load->view('templates/footer');
        }
}

控制器

<?php
class Page_model extends CI_Model {

        public function __construct()
        {
                parent::__construct();
                $this->load->database();
        }
       
        public function get_pages($table)
        {
                $query = $this->db->get($table);
                return $query->num_rows();
        }
       
        public function paging($table,$num,$offset)
        {
                $query = $this->db->get($table,$num,$offset);
                return $query->result_array();
        }
       
}

视图

<?php foreach ($content as $content_item): ?>

    <h2><?php echo $content_item['title'] ?></h2>
    <div id="main">
      <?php echo $content_item['text'] ?>
    </div>
    <p><a href="http://localhost/ci/index.php/content/<?php echo $content_item['id'] ?>">View article</a></p>

<?php endforeach ;echo $this->pagination->create_links()?>

路由

<?phpif ( ! defined('BASEPATH')) exit('No direct script access allowed');

$route['default_controller'] = 'pages/view';
$route['404_override'] = '';

$route['content'] = 'content';
$route['content/add'] = 'content/add';
$route['content/update'] = 'content/update';
$route['content/update/(:any)'] = 'content/update/$1';
$route['content/page/(:any)'] = 'content/index/$1';
$route['content/page'] = 'content/index';
$route['content/update_content/(:any)'] = 'content/update_content/$1';
$route['content/del/(:any)'] = 'content/del/$1';
$route['content/(:any)'] = 'content/view/$1';

$route['upload'] = 'upload';

$route['login/login_array'] = 'login/login_array';

$route['login'] = 'login';

$route['upload/do_upload'] = 'upload/do_upload';

$route['(:any)'] = 'pages/view/$1';

再自忆建个表,加上视图出出现的几个字段就行了

goyuquan 发表于 2014-4-2 17:41:24

http://www.ning-an.com也贡献一篇关于分页的

曹东东 发表于 2014-4-4 13:49:18

我记得我做分页的时候,不管怎样都不行,然后换了一个新版本的ci就行了

ljc 发表于 2014-4-7 13:43:22

本帖最后由 ljc 于 2014-4-7 13:44 编辑

{:soso_e113:}
页: [1]
查看完整版本: 分布类异常