【问题标题】:pagination not working properly in codeigniter 2.1.0 with url_suffix分页在带有 url_suffix 的 codeigniter 2.1.0 中无法正常工作
【发布时间】:2012-02-22 09:15:40
【问题描述】:

我正在使用 codeigniter 2.1.0 和 mysql 数据库。在我的管理面板中,我有一个页面,我想向所有用户显示分页视图。但它不工作。在尝试了很多事情之后
这是我的更新代码
控制器:

function accounts() // controller to view all users
{
    $this -> load -> library('pagination');
    $config['base_url'] = site_url('admin/home/accounts/');
    $config['total_rows'] = $this->db->get('user')->num_rows();
    $config['per_page'] = 2;
    $config['num_links'] = 2;
    $config['full_tag_open'] = '<p id="pagination">';
    $config['full_tag_close'] = '</p>';
    $offset = $this->uri->segment(4,0);
    $this->pagination->initialize($config);
    $this->load->model('admin_model');
    $data['user_data']=$this->admin_model->all_user($config['per_page'],$offset);
    $data['links']=$this -> pagination -> create_links();
    $data['main_content']='admin/accounts';
    $this->load->view('includes/admin/admin_template',$data);
}

查看:

<?php foreach ($user_data as $data) {?>
<?php echo 'Name:' . ' ' ?>
<?php echo ($data['name']).'<br/>'; ?>
<?php echo 'E-mail:' . ' ' ?>
<?php echo ($data['email']).'<br/>'; ?>
<br />
<?php } ?>
<?php echo $links;?>

型号:

function all_user($per_page,$offset) //shows all uer info
{
    $query = $this->db->get('user', $per_page, $offset);
    $row=$query->result_array();
    return $row;
}


这是路线:

$route['default_controller'] = "site";
$route['404_override'] = '';
$route['admin']='admin/admin';


我已使用 .htaccess mod_rewrite 删除我的 index.php 并使用 $config['url_suffix']='.html' 添加一个 .html 后缀,使 url 看起来像这样
'http://localhost/project/admin/home/accounts.html'
而不是
'http://localhost/project/index.php/admin/home/accounts'
但如果我删除 config.php 中的 url_suffix

*/

$config['url_suffix'] = '';

/*

分页工作正常。但我想使用 url_suffix

*/

$config['url_suffix'] = '.html';

/*

但如果我这样做,分页不起作用,并显示 404 page not found 错误。我该如何解决这个问题?

【问题讨论】:

  • 点击下一页时的网址是什么
  • 如果你拿localhost/project/admin/home/accounts.php/2会不会工作
  • @sabari,不,它不会工作。在 url localhost 是服务器项目是包含整个项目的文件夹,admin 是包含管理控制器的子域,home 是类和帐户是家庭类的功能。'.html'只是codeigniter提供的额外后缀,如果我将其配置为。后缀是'.html'/'.php'/'.asp'或无,功能保持不变。
  • 你能粘贴你的路由器配置吗

标签: php mysql pagination codeigniter-2


【解决方案1】:

我在您的代码中发现了很多问题。但是第一件事第一。

$this->uri->segment(4)

在您的查询中偏移。

更新:如果您还没有完成mod_rewrite,请将“index.php”添加到您的$config['base_url']

例如 $config['base_url']=base_url().'index.php/admin/....';

在上面的代码中添加你的路径。

【讨论】:

  • 我已经尝试过了,但它仍然没有改变任何东西:( @itachi
  • 我已使用 .htaccess mod_rewrite 删除我的 index.php 并使用 $config['url_suffix']='.html' 添加后缀 .html
  • 谢谢你。我没有先得到你的答案,但现在我想通了。我只需要更改 $config['base_url'] = base_url().'admin/home/accounts/'...非常感谢
  • 还有base_url()带参数,我觉得这个base_url('admin/home/accounts/')更好一点
【解决方案2】:

$config['base_url'] = site_url('admin/home/accounts');

在这一行中,您需要在末尾添加一个 backslas,例如

$config['base_url'] = site_url('admin/home/accounts/');

更新

以您的方式将其添加到您的控制器

$data['links']=$this->pagination->create_links();

更新 一个疯狂的想法,虽然我对 codeigniter 知之甚少。

在模型和控制器之间切换。你确定你所说的控制器是你代码中的控制器吗?模型的相同问题。

更新 抱歉,您的基本网址应该是

$config['base_url'] = site_url('admin/home/accounts/index/');

【讨论】:

  • 如果您已经尝试过这些,那么为什么不更新代码。在这个问题上,我支持 itachi。您的代码中有很多问题。我认为没有单一的解决方案可以解决这个问题。
【解决方案3】:

在系统中-> Pagination.php 文件(默认设置)

class CI_Pagination {

var $base_url           = ''; // The page we are linking to
var $prefix             = ''; // A custom prefix added to the path.
var $suffix             = ''; // A custom suffix added to the path.
.
.
}

$config['suffix'] ="example"

分页的Url_suffix ..........

【讨论】:

  • 尝试格式化您的代码(不太成功;-) - 请编辑和改进
猜你喜欢
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多