Skip to Content
PHPCODE
Codeigniter Public Articles Listing
codeigniter code / August 5, 2018

public Articles Listing in Codeigniter

Step 1 :- created view in articles_list.php
<?php include (‘public_header.php’);?>
<div class=”container”>
<h1>All Articles</h1>
<table class=”table”>
<thead>
<tr>
<td>Sr No.</td>
<td>Article Title</td>
<td>Published On</td>
</tr>
</thead>
<tbody>
<tr>
<?php if(count($articles)):?>
<?php $count=$this->uri->segment(3,0);?>
<?php foreach($articles as $article ):?>
<td><?= ++$count?></td>
<td><?= $article->title?></td>
<td><?= “Date” ?></td>
</tr>
<?php endforeach;?>

<?php else: ?>
<tr>
<td colspan=”3″> No Records Found.</td>
</tr>
<?php endif;?>
</tbody>
</table>
<?= $this->pagination->create_links()?>
</div>

<?php include (‘public_footer.php’);?>s

step 2: created model two function here

public function all_articles_list($limit,$offset)
{
$query=$this->db
->select([‘title’,’id’])
->from(‘articles’)
->limit($limit,$offset)
->get();
return $query->result();
}

public function count_all_articles()
{
$query=$this->db
->select([‘title’,’id’])
->from(‘articles’)
->get();
return $query->num_rows();
}

step 3: created controller user.php

<?php
class User extends MY_Controller
{
public function index()
{
$this->load->model(‘articlesmodel’,’articles’);
$this->load->library(‘pagination’);
$config=[
‘base_url’ => base_url(‘user/index’),
‘per_page’  => 5,
‘total_row’ => $this->articles->count_all_articles(),
‘full_tag_open’ => “<ul class=’pagination’>”,
‘full_tag_close’=> “</ul>”,
‘first_tag_opne’ => “<li>”,
‘first_tag_close’=> “</li>”,
// ‘first_link’ => “<li>”
// ‘last_link’ => “</li>”
‘last_tag_opne’ => “<li>”,
‘last_tag_close’ => “</li>”,
‘next_tag_opne’ => “<li>”,
‘next_tag_close’ => “</li>”,
‘pre_tag_open’  => “<li>”,
‘pre_tag_close’ => “</li>”,
‘num_tag_open’  => ‘<li>’,
‘num_tag_close’ => ‘</li>’,
‘cur_tag_open’  => “<li class=’active’><a>”,
‘cur_tag_close’ => ‘</a></li>’,
];

$this->pagination->initialize($config);
$articles=$this->articles->all_articles_list($config[‘per_page’],$this->uri->segment(3));
$this->load->view(‘public/articles_list’,compact(‘articles’));
}
}
?>

PHPCODE © 2023