controller :
function my_feeds()
{
$this->load->model('membership_model');
$query = $this->membership_model->get_my_feeds();
if($query->num_rows()>0) {
$res = $query->result();
$this->load->view('my_feed',array("query"=>$res));
}
else
{
echo "No feeds added";
}
Model:
function get_my_feeds()
{
$this->db->where('username',$this->session->userdata('username'));
$q = $this->db->get('feeds');
return $q;
}
View:
<ol><?php
foreach ($query as $row){
echo "<li><a href='".$row->url."'>".$row->title."</a></li><br/>";
};?>
With controller i retrieve data from my database and display thems in the view. I try to make a pagination. I put this code in the if statement on the controller function.
$this->load->library('pagination');
$this->load->library('table');
$config['base_url'] = 'http://localhost/Codeigniter/index.php/site/my_feeds';
$config['total_rows'] = $query->num_rows();
$config['per_page'] = 10;
$this->pagination->initialize($config);
I don't know how to connect the results with the pagination and how to show both in my view
A possible solution if you're willing to rewrite the db queries is this: use PDO and fetch the data as objects of a FeedEntry
class stored in a FeedEntryList
, which implements Iterator
interface. Code a function for FeedEntryList
that renders the actual page results, for example FeedEntryList::render(1,10)
should return an array of FeedEntry
objects (1st page, 10 feeds), which you can easily show using a foreach
.
Your Controller
function my_feeds()
{
$this->load->model('membership_model');
$this->load->helper('url');
$query = $this->membership_model->get_my_feeds();
$this->load->library('pagination');
$this->load->library('table');
$config['base_url']='http://localhost/Codeigniter/index.php/site/my_feeds';
$config['total_rows'] = $query->num_rows();
$config['per_page'] = 10;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
if($query->num_rows()>0) {
$res = $this->membership_model->get_my_feeds($config['per_page'],$this->uri->segment(4));
$this->load->view('my_feed',array("query"=>$res));
}
else
{
echo "No feeds added";
}
}
Model
function get_my_feeds($limit = 0,$offset = 0)
{
$this->db->where('username',$this->session->userdata('username'));
if ($limit != 0)
$this->db->limit($limit,$offset)
$q = $this->db->get('feeds');
return $q;
}
in view page just add this line where you want to show pagination links..
<?php echo $this->pagination->create_links(); ?>
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.