当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Codeigniter URI segment return boolean

问题描述:

I am new to Codeigniter and I am trying use pagination. My problem is unexpected, as my pagination code works perfectly except the URI segment always return as a boolean. My code is:

/* 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 */

where SEARCHHOTELS constant is

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

In my code: $this->uri->segment(3) always returns boolean.

When I click on my pagination, the URL value changes from:

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

to:

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

The URI segment does not set offset the value in my $this->db-get query. #

Any help will be appreciated.

网友答案:

In case anyone fall into this kind of trouble i want to share my solution which i found in two days.

The flow of my page was:

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

The uri segment for pagination only work in controller i.e it will fetch segments before loading the view .

if you want to get the uri segment in a function that is called through the ajax.

It will not work and fetch only 2 segments that is controller and method after that all methods will be skipped .

hope this will help some one else

网友答案:

From docs:

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

// Executes: SELECT * FROM mytable LIMIT 20, 10

// (in MySQL. Other databases have slightly different syntax)

That said, third parameter of get() method is limit but you are putting page number there. Switch places of second and third arguments. But you need to do some math there

$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();

I think your base url of pagination should be

$config['base_url'] = SEARCHHOTELS;

without any segment after.

网友答案:

You should use $this->uri->segment(4) as your part of uri which hold page. You should set config for pagination correctly:

$config['uri_segment'] = 4;

$this->uri->segment(3) is 'hotels' i your code. Am I right?

分享给朋友:
您可能感兴趣的文章:
随机阅读: