CodeIgniter URI第4分段分页异常求助:高亮错误+链接失效
Hey there, let's work through these pagination glitches you're hitting—they're mostly from small configuration oversights and a syntax error, so we can get this sorted quickly.
First, Let's Address the Root Causes & Fixes
1. Initial Load Highlights the Last Page
The problem here is that when your page first loads, $this->uri->segment(4) returns NULL (since the segment doesn't exist yet). The pagination class gets confused without a valid offset, so it defaults to highlighting the last page instead of the first.
Fix:
Set a default value of 0 for the offset when the segment is missing:
// Replace this line $offset = $this->uri->segment(4); // With this $offset = $this->uri->segment(4, 0);
This tells CodeIgniter to use 0 as the offset when the 4th segment isn't present, so the pagination class correctly identifies the first page as active on initial load.
2. Broken First/Last Links (Initial Load)
You've got two issues here:
- A critical HTML syntax error in your pagination config that breaks the last link's rendering
- An incorrect
num_linkssetting that messes with pagination logic
Fixes:
First, fix the missing space in last_tag_open (this is why your last link was unclickable initially):
// Replace this broken line $config['last_tag_open'] = '<liclass="page-item">'; // With this (add space between li and class) $config['last_tag_open'] = '<li class="page-item">';
Next, fix the num_links value—you set it to $num_rows (total number of chapters), which tells the pagination class to show all page links at once, leading to weird behavior. Instead, set it to a reasonable number of surrounding links (e.g., 3 or 5):
// Replace this $config['num_links'] = $num_rows; // With something like this $config['num_links'] = 3;
3. URI Segment 4 Becomes # When Clicking Last Page
This happens because your cur_tag_open uses <a href="#"> for the active page link. When the pagination class incorrectly thinks the last page is active (from the initial offset issue), clicking it just adds # to the URL. Once we fix the offset and configs above, this will resolve itself—only the actual active page will have the # link, and clicking other pages (including the last one) will generate the correct segment value.
Final Checks to Ensure Future Compatibility
Since you plan to add another pagination set on segment 5 later, make sure:
- You always explicitly set default offsets for each pagination set (e.g.,
$offset2 = $this->uri->segment(5, 0)for the second set) - Keep your pagination configs separate (use distinct variable names if you're initializing multiple pagination instances)
- Double-check all HTML tag syntax in your configs—missing spaces or unclosed tags will break functionality silently
Full Corrected Pagination Code Snippet
Here's your code with all fixes applied:
$num_rows = $this->story_model->count_rows('chapters',$story_id, null); $config['base_url'] = base_url().'story/story_profile/'.$story_id.'/'; $config['total_rows'] = $num_rows; $config['per_page'] = 10; $config['num_links'] = 3; // Updated to reasonable value $config['full_tag_open'] = '<ul class="pagination">'; $config['full_tag_close'] = '</ul>'; $config['first_link'] = '0'; // You can change this to "First" for better UX if needed $config['last_link'] = 'Last'; $config['first_tag_open'] = '<li class="page-item">'; $config['first_tag_close'] = '</li>'; $config['prev_link'] = '«'; $config['prev_tag_open'] = '<li class="page-item prev">'; $config['prev_tag_close'] = '</li>'; $config['next_link'] = '»'; $config['next_tag_open'] = '<li class="page-item">'; $config['next_tag_close'] = '</li>'; $config['last_tag_open'] = '<li class="page-item">'; // Fixed missing space $config['last_tag_close'] = '</li>'; $config['cur_tag_open'] = '<li class="page-item active"><a href="#">'; $config['cur_tag_close'] = '</a></li>'; $config['num_tag_open'] = '<li class="page-item">'; $config['num_tag_close'] = '</li>'; $this->pagination->initialize($config); $offset = $this->uri->segment(4, 0); // Added default offset $data['chapters'] = $this->db->get_where('chapters', array('story_id' => $story_id), $config['per_page'],$offset);
内容的提问来源于stack exchange,提问作者Chief Makii

