【问题标题】:Codeigniter URI segment return booleanCodeigniter URI 段返回布尔值
【发布时间】:2016-10-27 08:24:37
【问题描述】:

我是 Codeigniter 的新手,我正在尝试使用 pagination。我的问题出乎意料,因为我的分页代码运行良好,除了 URI 段总是作为布尔值返回。我的代码是:

/* pagination */
$this->load->library('pagination');
$destination = $data['destination'];

$config = array();
$config["base_url"] = SEARCHHOTELS.$this->uri->segment(3);

$total_row = $this->db->get('hotel')->num_rows();
$config["total_rows"] = $total_row;
$config["per_page"] = 30;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = '<a class="active">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$str_links = $this->pagination->create_links();


$this->db->select('*');
if(!empty($data['destination'])){
  $destination = $data['destination'];
  $this->db->like('name',$destination);
  $this->db->or_like('regionName',$destination);
}
$hSearch['records'] = $this->db->get('hotel',$config['per_page'],$this->uri->segment(3))->result_array();
$hSearch["links"] = explode('&nbsp;',$str_links);
/* paginationEnd */

SEARCHHOTELS 常量在哪里

define('SEARCHHOTELS', SITE_URL.'search/hotels/');

在我的代码中:$this-&gt;uri-&gt;segment(3) 总是返回布尔值。

当我点击我的分页时,URL 值从:

http://localhost/holidays/search/hotels/ 

到:

http://localhost/holidays/search/hotels/1 (or 2,3,4) 

URI 段没有在我的$this-&gt;db-get 查询中设置偏移值。 #

任何帮助将不胜感激。

【问题讨论】:

    标签: php codeigniter pagination


    【解决方案1】:

    如果有人遇到这种麻烦,我想分享我在两天内找到的解决方案。

    我的页面流程是:

    search-controller->search-view->alot-of-ajax-request->find-all-hotels();
    

    用于分页的 uri 段仅在控制器中工作,即它将在加载视图之前获取段。

    如果您想在通过 ajax 调用的函数中获取 uri 段。

    它不会工作,只获取控制器和方法的 2 个段,之后所有方法都将被跳过。

    希望这对其他人有帮助

    【讨论】:

    • 如果您想使用 Ajax 请求,您需要调用您的数据和相应数据的分页:)。例如:如果您要求数据限制 10、30,您还应该要求相应的分页 html。 / @Tpojka:我认为他的 base_url 是正确的;)
    • @PawelN 他还是我的?
    • @PawelN 代码属于他自己。 :D 根据docs 基本网址是不包括分页更改部分的字符串。
    【解决方案2】:

    来自文档:

    $query = $this->db->get('mytable', 10, 20);

    // 执行:SELECT * FROM mytable LIMIT 20, 10

    //(在 MySQL 中。其他数据库的语法略有不同)

    也就是说,get() 方法的第三个参数是限制,但您将页码放在那里。交换第二个和第三个参数的位置。但是你需要在那里做一些数学运算

    $offset = (int)$this->uri->segment(3) > 0 ? ($this->uri->segment(3) - 1) * $config['per_page'] : 0;
    
    $hSearch['records'] = $this->db->get('hotel', $offset, $config['per_page'])->result_array();
    

    我认为你的分页基本网址应该是

    $config['base_url'] = SEARCHHOTELS;
    

    之后没有任何段。

    【讨论】:

    • 感谢您的回答,但这里的问题是 $this->uri->segment(3) 应该返回 url 最后一个段,它应该是 1 2 3 或一些 int 值,但它返回布尔值。当我为分页设置静态值时,我的查询完美运行,即 1 2 3 & 4
    • 在方法的第一行放置var_dump($this-&gt;uri-&gt;segment_array());exit;来调试它。
    • 我还尝试返回 2 个段,进一步调试首先是搜索,第二个是酒店,酒店位于第三段,进一步的值 1 2 等不会在段中弹出 .. 兄弟我调试每一行,但没有运气,我的代码没有错误
    • 你是否将参数传递给public function hotels($id = FALSE){/*code here*/}之类的方法?
    • 是的,我也尝试将其放在 url 中,例如 .'?value='.$this->uri->segment(3);但没有任何效果..
    【解决方案3】:

    你应该使用 $this->uri->segment(4) 作为你保存页面的 uri 的一部分。 您应该正确设置分页配置:

    $config['uri_segment'] = 4;
    

    $this->uri->segment(3) 在您的代码中是“酒店”。我说的对吗?

    【讨论】:

    • 是的,我也尝试过,但没有运气最后一个值,即 uri_segment 无法选择分页添加 1 2 3 或 4 ..
    猜你喜欢
    • 2013-08-18
    • 2012-08-26
    • 2011-12-14
    • 2020-07-29
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    相关资源
    最近更新 更多