簡體   English   中英

每頁計數條目錯誤

[英]Error in entries per page count

我正在一個有分頁條目並希望輸出當前位置的網站,例如, Showing 10-18 of 50 results但是如果條目數少於每頁限制,則很難獲得正確的輸出。

該站點使用ExpressionEngine,其中URI的最后一段指示您在頁面列表中的位置,因此,如果限制為每頁9,則當您位於第二頁時,URI將為/path/to/page/P9/P18第3頁等。

這是我到目前為止的內容:

$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)似乎從未評估為true,例如,如果頁面限制為9,但只有三個條目,它將說1 to 9 of 3 entries

我認為這可能是因為我需要使變量為整數,但這意味着條件的另一端始終求值為true,因此,當有更多條目超出每頁限制時,無論您在哪一頁上輸出,輸出始終是相同的例如在所有頁面上1 to 14 of 14 entries

我要去哪里錯了?

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

在這一部分,您首先要檢查$total_entries是否小於或等於$per_page ,但是$per_page ,您要檢查它是否大於或等於$per_page 這里的邏輯不好(第二個if語句相反)。

嘗試這個 :

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM