dongtang3155 2016-08-03 01:22
浏览 17
已采纳

每页计数条目错误

I'm working on a site which has paginated entries and want to output where you are currently, e.g. 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, e.g. 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, e.g. 1 to 14 of 14 entries on all pages.

Where am I going wrong?

  • 写回答

1条回答 默认 最新

  • dsz90288 2016-08-03 08:18
    关注
    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多