I am trying to create a function in model that retrieve the prev and next links of certain blog post page. Blog posts are saved in database in a table where are different types of pages, so the ID's are not in order. What I achieve so far is getting an array with all the pages that are "marked" as blog posts. To make it clear, here is the array:
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
)
)
I can get the current page ID and I want to get the prev and next ID's, for example when I am on page with ID 2133 I want to get the ID 2127 and 2137.
I already searched and tried some solutions but they didn't worked. Please help!
Assuming your array of StdObjects is called $myArray, you can get an array of the id's with this.
$idArray = array();
foreach($myArray as $m=>$o) {
$idArray[]= $o->id;
}
print_r($idArray);
which gives you
Array (
[0] => 2127
[1] => 2133
[2] => 2137
[3] => 2138
[4] => 2139
)
and you could pull which id's you need from the $idArray.
@ourmandave: I used your suggestion and finally come up with the entire solution. I'll write it down here in case someone needs 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]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.