1

我正在尝试学习 CodeIgniter,但我遇到了分页问题。我是一个完整的新手,所以如果我错过了什么,我深表歉意。

分页显示在我的视图上很好,生成了它应该生成的所有内容,但是页面给了我一个 404。我怀疑它可能与路由或 uri_segment 有关(尽管我已将其更改为一大堆数字而没有成功了),但我不确定。

每个页面生成的 URL 是 /music3/music/20、/music3/music/40 等。

如果您需要我未包括的任何内容,请告诉我。

控制器:

$config['base_url'] = 'http://moefoster.com/music3/music/';
$config['total_rows'] = $this->db->count_all('Music');
$config['per_page'] = '20';
$config['num_links'] = '5';
$config['uri_segment'] = '2';

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

$data['music'] = $this->music_model->get_music($config['per_page'], $config['uri_segment']);

模型:

public function get_music($num = 20, $offset = 0, $slug = FALSE) {
    if ($slug === FALSE) {
        return $this->db->select('*')->from('Music')->limit($num, $offset)->order_by('Release', 'desc')->get()->result_array();

    }

    $query = $this->db->get_where('Music', $num, $offset, array('Track' => $slug));
    return $query->row_array();

}

路线:

$route['music'] = 'music';
$route['default_controller'] = "music";
$route['(:any)'] = 'pages/view/$1';
4

2 回答 2

1

您使用标准分页:/没关系,分页生成如下:

类/函数/分页编号

网址基础:

$config['base_url'] = 'http://moefoster.com/music/';

路线:

$route['music'] = 'music';
$route['music/(:num)'] = 'music/$1';
$route['default_controller'] = "music";
$route['(:any)'] = 'pages/view/$1';

但我在后端使用,\libraries\My_Pagination.php

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

class MY_Pagination extends CI_Pagination
{
    var $offset = 0;
    var $pagination_selector = 'page';
    var $index_page;

    function MY_Pagination ()
    {
        parent::__construct();
        log_message('debug', "MY_Pagination Class Initialized");

        $this->index_page = config_item('index_page') != '' ? config_item('index_page') . '/' : '';
        $this->_set_pagination_offset();
    }

    function _set_pagination_offset ()
    {
        $CI = & get_instance();
        if (strstr($CI->uri->uri_string(), $this->pagination_selector)) {
            $segments = $CI->uri->segment_array();
            foreach ($segments as $key => $value) {
                if ($value == $this->pagination_selector) {
                    $this->offset = $CI->uri->segment($key + 1);
                    $this->uri_segment = $key + 1;
                    $uri = $CI->uri->uri_string();
                    $pos = strpos($uri, $this->pagination_selector);
                    $this->base_url = '/'.$this->index_page . substr($uri, 0, $pos + strlen($this->pagination_selector.'/'));
                }
            }        
        }
        else {
            $this->offset = 0;
            $this->uri_segment = 0;
            $this->base_url = '/'.$this->index_page . $CI->uri->uri_string() . '/' . $this->pagination_selector.'/';        
        }    
    }
}

示例控制器

 function somefunction($somevariable){
        $config['total_rows'] = Model_Admin::getCountClankyCategory($somevariable); 
        $config['per_page'] = 25;
        $config['cur_tag_open'] = '#START SOME HTML#';
        $config['cur_tag_close'] = '#END SOME HTML#';
        $config['full_tag_open'] = '#START SOME HTML PAGINATION#';
        $config['full_tag_close'] = '#END SOME HTML PAGINATION#';  
        $this->pagination->initialize($config);          
      $data['content'] =  Model_Admin::getAllClankyCategory($somevariable,$config['per_page'],$this->pagination->offset);
.....
}

一些示例模型

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

class Model_Admin extends CI_Model {
  private static $db;

  function __construct() 
  {
    parent::__construct();
    self::$db = &get_instance()->db;
  }
static function getAllClankyCategory($cat,$num,$offset)  {   
     self::$db->where('#MYTYPE#',$cat);
      return self::$db->get('#SOMETABLE#',$num,$offset)->result_array();   
    }

static function getCountClankyCategory($cat) {
         return self::$db->count_all_results('#SOMETABLE#');
     }

那么如果你使用“一些硬代码路由”,比如路由,其他你使用没有这些路由

$route['admin/articles/cat/(:any)'] = "admin/cat/$1"; 
$route['admin/articles/cat/(:any)/(:num)'] = "admin/cat/$1/$2";
于 2013-02-01T12:23:44.797 回答
-1

您可以查看我之前关于分页的答案,该答案很好地解释了如何进行分页以及如何完美地处理网址.....

于 2013-02-01T05:34:52.340 回答