简体   繁体   English

每页计数条目错误

[英]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. 我正在一个有分页条目并希望输出当前位置的网站,例如, Showing 10-18 of 50 results但是如果条目数少于每页限制,则很难获得正确的输出。

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. 该站点使用ExpressionEngine,其中URI的最后一段指示您在页面列表中的位置,因此,如果限制为每页9,则当您位于第二页时,URI将为/path/to/page/P9/P18第3页等。

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 . if ($total_entries <= $per_page)似乎从未评估为true,例如,如果页面限制为9,但只有三个条目,它将说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. 我认为这可能是因为我需要使变量为整数,但这意味着条件的另一端始终求值为true,因此,当有更多条目超出每页限制时,无论您在哪一页上输出,输出始终是相同的例如在所有页面上1 to 14 of 14 entries

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. 在这一部分,您首先要检查$total_entries是否小于或等于$per_page ,但是$per_page ,您要检查它是否大于或等于$per_page Bad logic here (and your second if statement is reversed). 这里的逻辑不好(第二个if语句相反)。

Try this : 尝试这个 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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