簡體   English   中英

codeigniter獲取上一個和下一個博客頁面鏈接

[英]codeigniter get prev and next blog page link

我正在嘗試在模型中創建一個函數,以檢索某些博客文章頁面的上一個和下一個鏈接。 博客文章保存在數據庫中表的類型不同的表中,因此ID的順序不正確。 到目前為止,我所獲得的是將所有“標記為”博客文章的頁面整理成數組。 為了清楚起見,這是數組:

Array
(    

   [0] => stdClass Object
        (
            [id] => 2127
            [options] => news=on
        )


   [1] => stdClass Object
        (
            [id] => 2133
            [options] =>  news=on
        )

    [2] => stdClass Object
        (
            [id] => 2137
            [options] =>  news=on
        )

    [3] => stdClass Object
        (
            [id] => 2138
            [options] => news=on
        )

    [4] => stdClass Object
        (
            [id] => 2139
            [options] => news=on
        )

    [5] => stdClass Object
        (
            [id] => 2142
            [options] =>  news=on
        )

    [6] => stdClass Object
        (
            [id] => 2144
            [options] => news=on
        )

    [7] => stdClass Object
        (
            [id] => 2145
            [options] => news=on
        )

    [8] => stdClass Object
        (
            [id] => 2146
            [options] => news=on
        )

    [9] => stdClass Object
        (
            [id] => 2153
            [options] => news=on
        )

    [10] => stdClass Object
        (
            [id] => 2156
            [options] =>  news=on
        )

)

我可以獲取當前頁面ID,並且想要獲取上一個ID和下一個ID,例如,當我使用ID 2133的頁面時,我想要ID 2127和2137。

我已經搜索並嘗試了一些解決方案,但是它們沒有起作用。 請幫忙!

假設您的StdObjects數組稱為$ myArray,則可以使用此數組獲取id的數組。

$idArray = array();
foreach($myArray as $m=>$o) {
  $idArray[]= $o->id;
}

print_r($idArray); 

這給你

Array (
    [0] => 2127
    [1] => 2133
    [2] => 2137
    [3] => 2138
    [4] => 2139 
)

您可以從$ idArray中提取所需的ID。

@ourmandave:我使用了您的建議,最后提出了整個解決方案。 如果有人需要身份證,我會在這里寫下來。

    // get the all the blog pages 
    $blog_pages = $this->pages_model->get_links();

    $idArray = array();
    foreach($blog_pages as $m=>$o) {
      $idArray[]= $o->id;
    }
    // Find the index of the current item
    $current_index = array_search($current_page->id, $idArray);
    // Find the index of the next/prev items
    $next = $current_index + 1;
    $prev = $current_index - 1;

    // and now finally sent the data to view
    $data['prev'] = $this->pages_model->get($idArray[$prev]);
    $data['next'] = $this->pages_model->get($idArray[$next]);

暫無
暫無

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

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