【问题标题】:how can send variable value through pagination links?如何通过分页链接发送变量值?
【发布时间】:2013-08-23 06:47:07
【问题描述】:

已解决 我试图在主页上创建分页。

我的默认控制器:

$route['default_controller'] ="home";

问题 1: 如果我写了分页-

$config['base_url'] = base_url() . 'home/index'; // index function
$config['uri_segment'] = 3;
$config['per_page'] = 10;
$limit = array($config['per_page'] => $this->uri->segment(3));

然后它可以工作,但它在浏览器地址栏上显示“localhost/baseurl/home/index/10”。我只想显示“localhost/baseurl/10”(而“localhost/baseurl/”是我的主页,我想要在那里分页)。所以我写了

$config['base_url'] = base_url(); // index function
$config['uri_segment'] = 1;
$config['per_page'] = 10;
$limitt = array($config['per_page'] => $this->uri->segment(1));

但这不起作用。我该怎么做?

问题 2:

如何通过分页链接发送变量值,例如 localhost/baseurl/home/index?search=y/10

我写道:

$config['base_url'] = base_url() . 'home/index?search=y';

但这不起作用。它接收search=y/10。不仅y而且分页在$this->uri->segment(3))上找不到10

那么正确的做法是什么?

编辑:

我的 htaccess 代码是:-

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./mysite/index.php [L]

</IfModule>

此代码显示我的站点地址,例如“localhost/mysite/”。但是如果我像

这样设置分页的基本 url
$config['base_url'] = base_url();
$config['uri_segment'] = 1;
$config['per_page'] = 10;
$limitt = array($config['per_page'] => $this->uri->segment(1));

然后数字显示在分页链接上,如“localhost/mysite/10”,但脚本未收到值“10”。如果我写-

$config['base_url'] = base_url().'home/index';  // 'index' is the function in 'home' class
$config['uri_segment'] = 3;
$config['per_page'] = 10;
$limitt = array($config['per_page'] => $this->uri->segment(3));

然后它可以工作,但是浏览器地址行变成了我不喜欢的“localhost/mysite/home/index/10”。解决第一个问题的方法是什么?

【问题讨论】:

  • 对于第一个问题,您需要在 .htaccess 文件中定义重写规则,对于第二个问题,启用选项“page_query_string”,因此您不需要使用 uri->segements 它将其作为 _GET 变量发送。
  • 第二个问题已解决。将“page_query_string”声明为 TRUE。但第一个问题仍然存在。我的 htaccess 文件代码在我的编辑部分。我应该如何改变它来解决第一个问题?
  • hmm.. 首先在你的 routes.php 中尝试这样的事情: $route['(:num)'] = "home/index/$1";
  • 您能解释一下吗?在我的 routes.php 中有 '$route['default_controller'] ="home/";'我试过 '$route['default_controller'] ="home/index/";'但这不适用于 uri->segment(1)。服务器似乎找不到 uri->segment(1) [like 'localhost/mysite/10']。总是显示“找不到页面”。
  • 我知道您已经设置了默认控制器,但是由于您尝试直接使用分页而不指定“控制器/方法/偏移”,并且您只想在您的home url,然后你必须定义我给你的路由,它将匹配 url 开头的任何 num 并将其移动到适当的控制器(默认控制器)。

标签: codeigniter pagination


【解决方案1】:

对于第一个问题,您必须在第一条评论中定义 rewrite rule
对于第二个问题,这样做,

if($this->uri->segment(3)){
    $keyword = $this->uri->segment(3);
}
else if($this->input->get('search')){
    $keyword = $this->input->get('search');
}

$config['base_url']     = base_url().'home/index/'.$keyword.'/page/';
$config["uri_segment"]  = 5;

【讨论】:

  • 我的第二个问题解决了。我按照艾哈迈德的评论。但第一个问题仍然需要解决。在那里查看我的 htaccess 代码。从浏览器地址栏上的站点地址中删除“index.php”。那么我应该如何改变它来解决第一个问题呢?我应该在那里写什么新东西?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多