17 April 2021

How to get next and preview post links in PHP (CodeIgniter).

 Get next and preview post links in PHP (CodeIgniter).


Paste in Controller :-

$id = 56;     // Current Post id 
$blog_pages = $this->Post_Model->all_posts();

$idArray = array();
foreach($blog_pages as $m=>$o) {
  $idArray[]= $o->id;
}
// Find the index of the current item
$current_index = array_search($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
if(!empty($idArray[$prev])){
if(in_array($idArray[$prev],$idArray)){
$data['prev'] = $this->Post_Model->getpostbyid($idArray[$prev]);
}
}
if(!empty($idArray[$next])){
if(in_array($idArray[$next],$idArray)){
$data['next'] = $this->Post_Model->getpostbyid($idArray[$next]); 
}
}



Paste in Model:-


public function all_posts()
{
            $query = $this->db->get('posts');  
            return $query->result();  
}



Paste in View:-

<div class="blog-details single-post-item format-standard">
<div class="post-details">
<div class="post-featured-img">
<img src="<?php echo base_url();?>assets/new-template/posts/<?php echo $blogs['post_image'];?>"/>
</div>
<div class="post-top-meta">
<ul class="meta-comment-tag">
<li><a href="#"><span class="icons"><i class="ti-user"></i></span>by <?php echo $blogs['uname'];?></a></li>
<li><a href="#Comments"><span class="icons"><i class="ti-comment-alt"></i></span><?php echo count($totalcomeent);?> Comments</a></li>
</ul>
</div> 
<h2 class="post-title"><?php echo $blogs['title'];?></h2>
<p><?php echo $blogs['body'];?></p>
<div class="single-post-pagination">
<?php
if(!empty($prev)){
//print_r($prev);
$prev_link = $prev['id'];
?>
<div class="prev-post">
<a href="<?php echo base_url().'blogdetail/'.$prev_link; ?>">
<div class="title-with-link">
<span class="intro">Prev Post</span>
</div>
</a>
</div>
<?php
}
?>
<div class="post-pagination-center-grid">
<a href="#"><i class="ti-layout-grid3"></i></a>
</div>
<?php
if(!empty($next)){
$next_link = $next['id'];
?>
<div class="next-post">
<a href="<?php echo base_url().'blogdetail/'.$next_link; ?>">
<div class="title-with-link">
<span class="intro">Next Post</span>
</div>
</a>
</div>
<?php
}
?>
</div>
 
</div>
</div>




No comments:

Post a Comment

How to create youtube videos slider with play and pause option in wordpress?

Create youtube videos slider with play and pause option in wordpress youtube videos slider Use this shortcode:- [punjab_today] function my_...