【问题标题】:codeigniter get prev and next blog page linkcodeigniter 获取上一个和下一个博客页面链接
【发布时间】:2017-02-16 16:23:05
【问题描述】:

我正在尝试在模型中创建一个函数来检索某些博客文章页面的上一个和下一个链接。博客文章保存在数据库中的不同类型页面的表中,因此 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 为 2133 的页面上时,我想获取 ID 2127 和 2137。

我已经搜索并尝试了一些解决方案,但没有奏效。 请帮忙!

【问题讨论】:

  • 我想你可以说分页是对的
  • 没有。我不是在谈论分页。这是博客文章的单页。我希望能够放置上一篇和下一篇文章的链接(博客文章)

标签: php mysql codeigniter


【解决方案1】:

假设你的 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。

【讨论】:

    【解决方案2】:

    @ourmandave: 我使用了您的建议,最终提出了整个解决方案。我会在这里写下来以防有人需要id。

        // 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]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 2020-04-22
      • 2015-11-01
      • 2013-04-08
      相关资源
      最近更新 更多