I am new at codeigniter. I have a problem with codeigniter's pagination.
I tried to search google, youtube and codeigniter documentation but I didn't find anything.
I can implement a simple pagination but I need to create multi pagination with a database in one table.
Anyone that can help me please?
My database:
product:
id category title image
1 burger a a.jpg
2 burger b b.jpg
3 burger c c.jpg
4 pizza d d.jpg
5 pizza e e.jpg
6 pizza f f.jpg
The result that I want is pagination in one page
(pagination category burger by burger,pizza by pizza)
and my url is http://localhost/test-ci1/
Code in Model:
<?php
class Product_model extends CI_Model {
function get_burgers($category="", $limit, $start) {
$this->db->select('*');
$this->db->from('product');
if($category) {
$this->db->where('category',$category);
}
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result();
}
function get_total($category="") {
$this->db->select('count(*) AS num_row');
$this->db->from('product');
if($category) {
$this->db->where('category',$category);
}
$this->db->limit(1);
$query = $this->db->get();
return $query->row()->num_row;
}
}
in the Controller:
<?php
class Site extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('product_model','product');
}
public function index()
{
$this->load->model('Product_model');
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data = array();
$per_page =2;
$data["pizza"] = $this->product->get_burgers('pizza', $per_page, $page);
$total_pizza = $this->product->get_total('pizza');
$limit= 2;
$link_pizza = 'http://localhost/test-ci1/pizza';
$data['pagination_pizza'] = $this->pagination($total_pizza,$limit,$link_pizza);
$data["burgers"] = $this->product->get_burgers('burger', $per_page, $page);
$total_burger = $this->product->get_total('burger');
$limit = 2;
$link_burger = 'http://localhost/test-ci1/index/burger';
$data['pagination_burger'] = $this->pagination($total_burger,$limit,$link_burger);
$this->load->view('home_page_view',$data);
}
private function pagination($total ,$per_page ,$link) {
$config['base_url'] = $link;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
return $this->pagination->create_links();
}
}
and in my View:
<body>
<h1>Pizza</h1>
<ul>
<?php foreach($pizza as $val) { ?>
<li><?php echo $val->title; ?></li>
<?php } ?>
</ul>
<?php echo $pagination_pizza; ?>
<hr>
<h1>Burger</h1>
<ul>
<?php foreach($burgers as $val) { ?>
<li><?php echo $val->title; ?></li>
<?php } ?>
</ul>
<?php echo $pagination_burger; ?>
</body>
and my result:
Pizza
pizza1
burger2
12>
Burger
burger1
assasa
12>
As I can see you haven't developed your model pagination. You should add a function that does the pagination for you while extracting only the wanted rows from the database.
For your example something like this in your model will do:
function get_burgers($category="", $limit, $start) {
$this->db->select('*');
$this->db->from('product');
if($category) {
$this->db->where('category',$category);
}
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result();
}
And in your controller instead of fetching just one burger you should
$data["burgers"] = $this->burger_model->get_burgers($category, $config["per_page"], $page);
And change your foreach in your view to iterate through the burgers
Related
I'm learning CI and latest I've been tasked to do is pagination. So I followed this tutorial as it seemed relatively nicely made and explained. Link
The pagination works and its great. Now I wanted to make a read more under each post, making each post open separately with its full description. That also works, but when I click a link to go back to the pagination index, the list starts from the beginning, no matter which post I clicked. I'm not sure how to add the page I want the return link to take me back so I'll just post this here and hopefully it won't be too hard for someone to tell me.
If someone is confused what I'm after, just look at the last view readmore_paginate. In it, the last link should contain a number back
to the page, but idk how to put or what to put there.
Controller
public function paginate()
{
$config = array();
$config['base_url'] = base_url()."welcome/paginate";
$config['total_rows'] = $this->blog_model->countPosts();
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; //TERNARY OPERATOR (? = TRUE) (: = FALSE)
$data['results'] = $this->blog_model->fetchPosts($config['per_page'], $page);
$data['links'] = $this->pagination->create_links();
$this->load->view('header');
$this->load->view("p_content", $data);
$this->load->view('footer');
}
public function readMore_Paginate()
{
$id = $this->input->get('postid');
$data['post'] = $this->blog_model->getSpecificPost($id);
$this->load->view('header');
$this->load->view('readmore_paginate', $data);
$this->load->view('footer');
}
Model
public function countPosts()
{
return $this->db->count_all("Blogposts");
}
public function fetchPosts($limit, $start)
{
$this->db->select('*');
$this->db->from('Blogposts');
$this->db->join('Blogcategories', 'Blogposts.postcatid=Blogcategories.id');
$this->db->limit($limit, $start);
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
View p_content
<div class="col-md-8">
<table class="table">
<tbody>
<?php
foreach ($results as $key) {
echo "<tr><th><h2><kbd><font color='yellow'>".$key->postname."</font></kbd></h2><kbd><font color='lime'>".date("d M Y",strtotime($key->postdate))."</font></kbd> <kbd><font color='cyan'>".$key->catname."</font></kbd></th></tr>";
echo "<tr><td><blockquote>".mb_substr($key->postdesc, 0,80,'UTF-8')."...";?>
read more</blockquote>
<?php } ?>
</tbody>
</table>
<p><?php echo $links; ?></p>
</div>
View readmore_paginate
<div class="col-md-8">
<table class="table">
<tbody>
<?php
foreach ($post as $key) {
echo "<tr><th><h2><kbd><font color='yellow'>".$key['postname'].
"</font></kbd></h2><kbd><font color='lime'>".
date("d M Y",strtotime($key['postdate']))."
</font></kbd> <kbd><font color='cyan'>".
$key['catname']."</font></kbd></th></tr>";
echo "<tr><td><blockquote>".$key['postdesc']."</blockquote>"; ?>
Back to Posts
<?php }
?>
</tbody>
</table>
</div>
Controller
$page = $this->input->get('page') ?
$this->input->get('page') :
$this->uri->segment(3) ?
$this->uri->segment(3) :
0;
$data['results'] = $this->blog_model->fetchPosts($config['per_page'], $page);
$data['links'] = $this->pagination->create_links();
$data['page'] = $page; // add this
view p_content
read more</blockquote>
view readmore_paginate
Back to Posts
Simple issue for someone to solve I hope, (I have re written this), I have a table with pages each page record has a parent_id I need to output all of the pages nested correctly under their parent page. I have managed to get the output to show two levels but I am now stuck at this point.
The eventual aim is to use JQuery Nestable to allow my user to re-order the pages and set new parents.
EDIT
Controller:
public function navigation() {
if($this->session->userdata('logged_in')) {
$session_data = $this->session->userdata('logged_in');
$data['auid'] = $session_data['user_user_id'];
$data['userName'] = $session_data['user_fullname'];
$data['gravatar'] = gravatar($session_data['user_email'], 30, FALSE);
$pages = $this->admin_m->getAllTopPages();
foreach ($pages as $p) {
$pid = $p->page_id;
$children = $this->admin_m->getAllChildPages($pid);
if($children) {
$p->children = $children;
}
}
$data['pages'] = $pages;
$data['current'] = 'navigation';
$this->load->view('admin/partials/header_v', $data);
$this->load->view('admin/partials/top-bar_v');
$this->load->view('admin/partials/side-bar_v');
$this->load->view('admin/navigation_v');
$this->load->view('admin/partials/footer_v');
} else {
redirect('admin/login', 'refresh');
}
}
Model:
public function getAllTopPages() {
$query = $this->db->get_where('yell_page', array('page_parent_id'=>'-1'));
return $query->result();
}
public function getAllChildPages($pid) {
$this->db->select('page_id, page_name, page_parent_id');
$this->db->from('yell_page');
$this->db->where('page_parent_id =', $pid);
$this->db->where('page_parent_id !=', '-1');
$query = $this->db->get();
return $query->result();
}
View:
<div class="panel-body">
<div class="dd" id="nestable_list_2">
<ol class="dd-list">
<?php
foreach($pages as $p) {
echo '<li class="dd-item" data-id="'.$p->page_id.'">';
echo '<div class="dd-handle">'.$p->page_name.'</div>';
echo '<ol class="dd-list">';
if(isset($p->children)) {
foreach($p->children as $obj) {
echo '<li class="dd-item" data-id="'.$obj->page_id.'"><div class="dd-handle">'.$obj->page_name.'</div></li>';
}
}
echo '</ol>';
echo '</li>';
}
?>
</ol>
</div>
</div>
I am getting somewhat closer but now the issue is that I only get data on two levels this could potentially be many more.
Codeigniter pagination does not work for me. It displays page numbers and first 5 search results, but when I click on the page '2' it loads 'search_nok' view with message "Please select your options". Could you please check my code below and help me to find the mistake.
Here is my controller:
public function search($offset = 0) {
$limit = 5;
$this->load->library('form_validation');
$this->load->model('model_x');
$this->form_validation->set_rules('country', 'Country','required');
if($this->form_validation->run()) {
$country = $this->input->post('country');
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/abc/cont/search/'; //where 'http://localhost/abc' is my base url
$config['total_rows'] = 14;
$config['per_page'] = 5;
$data['pagination'] = $this->pagination->initialize($config);
if ($this->model_x->did_search($country, $limit, $offset)){
$data["results"] = $this->model_x->did_search($country, $limit, $offset);
$this->load->view("search_ok",$data);
}
}
else
{
$data['message'] = 'Please select your options.';
$this->load->view("search_nok",$data);
}
}
Here is my view:
<?php
echo $this->pagination->create_links();
foreach($results as $row){
$country = $row['country'];
$city = $row['city'];
?>
<table>
<tr>
<td >
<b><?php echo $country; ?></b>
</td>
<td>
<b><?php echo $city; ?></b>
</td>
</tr>
</table>
<?php }?>
I think you are confused about Pagination. First of all, technically all of the pagination stuff:
<?php
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/abc/cont/search/'; //where 'http://localhost/abc' is my base url
$config['total_rows'] = 14;
$config['per_page'] = 5;
should be in your controller, secondly, you should actually be limiting the results in your model as well, since you haven't shown your model I will come up with an example
$this->db->limit ($limit $offset //these will be defined in your controller)
$data = $this->db->get('whatever_table')->whatever_result_type.
return $data
you should define $limit in your controller and pass it over your your model function as a parameter that the function accepts
the controller should also have $offset and $limit defined
public function search($offset = 0) {
$limit = 5;
when you call your model make sure to pass these over
$this->model_x->did_search($country, $limit, $offset);
then instead of just $this->pagination->initialize($config);
do this(still in your controller remember):
$data['pagination'] = $this->pagination->initialize($config);
then echo pagination wherever you want it in your view
I have used join query to fetch the data, when some data is deleted from the table which is joined then it creating problem with pagination
controller code
function index($msg='',$offset = 0)
{
$data = array('title'=>'Towns','message'=>'', 'link_add'=>site_url('manage/town/add'), 'edit_link'=>site_url('manage/town/edit'), 'tbl'=>'towns' );
$uri_segment = 4;
$offset = $this->uri->segment($uri_segment);
// load data
$value=('towns.Id,towns.Name as TownName,city.Name as CityName,city.Status,towns.Status,towns.TaxAmount');
$data['list_records'] = $this->admin_model->get_joinlist($data['tbl'],$value,'city','city.Id = towns.cityId','left outer','towns.Id','asc',array('towns.Status !='=>'Delete','city.Status'=>'Enable'),$this->limit, $offset)->result();
if($msg=='m')$data['message'] = 'New Town has been added successfully!';
// generate pagination
$this->load->library('pagination');
$config['base_url'] = site_url('manage/town/index/');
$this->total = $this->admin_model->**count_all**($data['tbl'],array('Status !='=>'Delete'));
$config['total_rows'] = $this->total;
$config['per_page'] = $this->limit;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['j'] = 0 + $offset;
$data['total_rows']= $this->total;
// load view
$this->load->view('manage/includes/header', $data);
$this->load->view('manage/town', $data);
$this->load->view('manage/includes/footer');
}
<?php
class Admin_model extends CI_Model {
//listing with join
public function get_joinlist($table,$value,$table2,$condi,$join_type,$order_by,$order,$where,$limit, $offset)
{
$this->db->select($value);
$this->db->join($table2,$condi,$join_type);
$this->db->order_by($order_by,$order);
$this->db->where($where);
return $query= $this->db->get($table, $limit, $offset);
}
//For pagination
function count_all($table,$where)
{
return $this->db->where($where)
->count_all_results($table);
}
function num_rows($table)
{
return $this->db->affected_rows($table);
}
Do something like this:
public function get_joinlist($table,$value,$table2,$condi,$join_type,$order_by,$order,$where,$limit, $offset)
{
$this->db->start_cache();
$this->db->select($value);
$this->db->join($table2,$condi,$join_type);
$this->db->order_by($order_by,$order);
$this->db->where($where);
$this->db->stop_cache();
$query['num_rows']=$this->db->get($table)->num_rows();
$query['results']=$this->db->get($table, $limit, $offset);
$this->db->flush_cache();
return $query;
}
This way, your model will return an array with the the total rows of the join and the results limited by offset and limit rows
I've used codeigniter to make an easy search, no ajax or something, and I want to integrate the pagination class, I have read some examples and they use a library like this $this->table->generate($results); and I need in each result a button to edit that entry. I already did, but now with that way of showing the pagination I have a big problem, please, could anyone help me with this issue? I just need a clue or something to find in CI
I put here my model, controller and view, just in case
controller
public function searchdescription()
{
//$keyword = $this->input->post('keyword');
$keyword = $_POST['keyword'];
$this->load->model('searchdesc/searchdesc_model');
$data['retorno'] = $this->searchdesc_model->querydb($keyword);
$this->load->view('searchdesc/searchresults_view', $data);
}
model
function querydb($data,$num, $offset)
{
$queryresult = $this->db->query("select * from data where deleteflag=0 and title like '%$data%' or text like '%$data%' LIMIT $num, $offset ");
return $queryresult->result_array();
}
view
foreach ($retorno as $val)
{
echo $val['id'];
echo $val['title'];
echo $val['text'];
...here are some forms autocreated in each row that i need to keep making it
}
Here is an example (not using a model)
public function big()
{
$this->load->library('pagination');
$this->load->library('table');
$config['base_url'] = base_url().'/site/big/';
$where = "bot = '2' OR bot = '0'";
$this->db->where($where);
$config['total_rows'] = $this->db->count_all_results('visitors');
$config['per_page'] = 15;
//$config['num_links'] = 20;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$where = "bot = '2' OR bot = '0'";
$this->db->where($where);
$this->db->select('id, ip, date, page, host, agent, spammer, country, total, refer');
$this->db->order_by("date", "DESC");
$data['records'] = $this->db->get('visitors', $config['per_page'], $this->uri->segment(3));
$this->table->set_heading('Id', 'IP', 'Date', 'Page', 'Host', 'Agent', 'Spam', 'Country', 'Total', 'Referer');
$this->db->select_sum('total', 'trips');
$query = $this->db->get('visitors');
$data['trips'] = $query->result();
$this->load->view('site_view', $data);
}
In the view where I want the table:
<?php echo $this->table->generate($records); ?>
<?php echo $this->pagination->create_links(); ?>
to add buttons in the rows do something like this
foreach($records as $row){
$row->title = ucwords($row->title);
$this->table->add_row(
$row->date,
anchor("main/blog_view/$row->id", $row->title),
$row->status,
anchor("main/delete/$row->id", $row->id, array('onClick' => "return confirm('Are you sure you want to delete?')")),
anchor("main/fill_form/$row->id", $row->id)
);
}
$table = $this->table->generate();
echo $table;
Of course you will modify to fit your needs