简体   繁体   中英

Error in entries per page count

I'm working on a site which has paginated entries and want to output where you are currently, eg Showing 10-18 of 50 results but am having trouble getting the correct output if there are less entries than the per-page limit.

The site uses ExpressionEngine where the last segment of the URI indicates where you are in the page list, so if the limit is 9 per page, the URI would be /path/to/page/P9/ when you're on the second page, P18 page 3 etc.

This is what I have so far:

$output        = '';

// the variables below are passed from a template to a plugin which does the processing

$url_segment   = $this->EE->TMPL->fetch_param('url_segment'); // e.g. P9, P18 etc.
$per_page      = $this->EE->TMPL->fetch_param('page_num'); // e.g. 9
$total_entries = $this->EE->TMPL->fetch_param('total'); // e.g. 50

$current_page  = preg_match('/^P+[0-9]+$/', $url_segment) === 1 ? str_replace('P','', $url_segment) +1 : 1;

if ($total_entries == 1) {
    $output .= '1 tour';
} else {
    if ($total_entries <= $per_page){
        $output .= '1 to '.($per_page<=$total_entries ? $total_entries : $per_page);
    } else {
        $output .= $current_page.' to ';
        $output .= $current_page+$per_page-1;
    }
    $output .= ' of '.$total_entries.' tours';
}

return $this->return_data = $output;

if ($total_entries <= $per_page) doesn't ever seem to evaluate to true, eg if page limit is 9 but only three entries, it'll say 1 to 9 of 3 entries .

I thought it might be because I need to make the variables integers but doing that means the other side of the conditional always evaluates as true so when there are more entries that the per-page limit, the output is always same regardless of which page you're on, eg 1 to 14 of 14 entries on all pages.

Where am I going wrong?

if ($total_entries <= $per_page){
    $output .= '1 to '.($per_page<=$total_entries ? $total_entries : $per_page);

On this part, you first check if $total_entries is less or equal than $per_page , but just after, you check if it's more or equal. Bad logic here (and your second if statement is reversed).

Try this :

if ($total_entries <= $per_page){
    $output .= '1 to '.$total_entries;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM