2017-02-16 126 views
0

我试图在模型中创建一个检索某个博客文章页面的上一个和下一个链接的函数。博客帖子保存在数据库中的表格中,其中包含不同类型的页面,因此ID的顺序不正确。 到目前为止,我所做的是获得一个数组,其中包含所有标记为博客文章的页面。 要清楚,这里是数组:codeigniter获取上一页和下一个博客页面链接

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,我想要得到的prev和next ID的,例如,当我网页上的ID为2133我想ID 2127和2137.

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

+0

我想你可以说关于分页我是不是 –

+0

没有。我不是在谈论分页。这是博客文章的单个页面。我希望能够链接到上一篇和下一篇文章(博客文章) –

回答

0

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

0

@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]); 
相关问题