I use CI3 and want to use pagination for my website. I used this tutorial for pagination https://www.codexworld.com/ajax-pagination-in-codeigniter-framework/ and it works somehow. I have 12 rows in the database and set the limit to 1, however, it shows that there are 12 pages (which is true) but fetches all 12 rows from the database as in the image (Look at scroll and number of pages):
Image
Here is my controller:
function __construct() {
parent::__construct();
$this->load->library('Ajax_pagination');
$this->perPage = 1;
}
public function index(){
//total rows count
$totalRec = count($this->collections_model->get_collections());
$limit = $this->perPage;
//pagination configuration
$config['target'] = '#ajpag';
$config['base_url'] = base_url().'collections/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
//get the posts data
$data['title'] = 'Collections';
$data['collections'] = $this->collections_model->get_collections(FALSE, $limit);
$this->load->view('templates/header', $data);
$this->load->view('pages/index', $data);
$this->load->view('templates/footer');
}
function ajaxPaginationData(){
$page = $this->input->post('page');
if(!$page){
$offset = 0;
}else{
$offset = $page;
}
//total rows count
$totalRec = count($this->collections_model->get_collections());
$limit = $this->perPage;
//pagination configuration
$config['target'] = '#ajpag';
$config['base_url'] = base_url().'collections/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
//get the posts data
$data['collections'] = $this->collections_model->get_collections(false, $limit, $offset);
//load the view
$this->load->view('collections/ajax-pagination-data', $data, false);
}
And here is my model:
public function get_collections($id = FALSE, $limit = FALSE, $offset = FALSE) {
if ($id === FALSE) {
$query = $this->db->get('collections');
$this->db->order_by('id','desc');
if($limit && $offset){
$this->db->limit($limit,$offset);
}elseif(!$offset && $limit){
$this->db->limit($limit);
}
return $query->result_array();
}
$query = $this->db->get_where('collections', array('id' => $id));
return $query->row_array();
}
How can I solve my problem?
You need to get after apply the limit, etc.
public function get_collections($id = FALSE, $limit = FALSE, $offset = FALSE) {
if ($id === FALSE) {
$this->db->order_by('id','desc');
if($limit && $offset){
$this->db->limit($limit,$offset);
}elseif(!$offset && $limit){
$this->db->limit($limit);
}
$query = $this->db->get('collections'); //call get here
return $query->result_array();
}
$query = $this->db->get_where('collections', array('id' => $id));
return $query->row_array();
}
Related
Codeigniter pagination with the passing parameter to where clause links not working
Here is my Model
public function record_count($category) {
return $this->db->where('sub_category_name',$category)->count_all_results("categories_item");
}
public function fetch_departments($category,$limit, $start) {
$this->db->limit($limit, $start);
$query=$this->db->where('sub_category_name',$category)->get("categories_item");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}
//Here is Controller
public function categories_item()
{
$this->load->database();
$this->load->library('pagination');
$this->load->model('Home_model');
$config = array();
$config["base_url"] = base_url() . "Home/categories_item";
$category=$this->uri->segment(3);
$this->db->where('sub_category_name',$category);
$config['total_rows'] = $this->db->get('categories_item')->num_rows();
$config['use_page_numbers'] = TRUE;
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? ($this->uri->segment(3)) : 0;
$data["link"] = $this->pagination->create_links();
if( !$data["results"] = $this->Home_model->fetch_departments($category,$config["per_page"], $page))
{
error_reporting(1);
}
else
{
$this->load->view("category_item_list",$data);
}
}
Hi I need hel I'm trying to get pagination with search results.
I don't know what I am doing wrong. Because my results don't show up.
Here is my Model
function search_posts($query, $pagina, $por_pagina)
{
$this->db->select();
$this->db->from('posts');
$this->db->like("post_title", $query, 'both');
$this->db->or_like("post", $query, 'both');
$this->db->order_by('post_id', 'desc');
$this->db->limit($pagina, $por_pagina);
$query = $this->db->get();
return $query->result_array();
}
public function contar_filas()
{
$this->db->select();
$this->db->from('posts');
$query = $this->db->count_all_results();
return $query;
}
Here is my Controller
public function buscar()
{
$this->load->model('M_Articulos');
$data['posts'] = '';
$data['sin_resultados'] = '';
$query = trim($this->input->get('query', TRUE));
if (sizeof($query) == 0)
{
show_404();
}
if ($query)
{
if ($this->uri->segment(3))
{
$pagina = $this->uri_segment(3);
}
else
{
$pagina = 0;
}
$por_pagina = 4;
$data['posts'] = $this->M_Articulos->search_posts($query, $pagina, $por_pagina);
if ($data['posts'] != FALSE)
{
$data['posts'] = $this->M_Articulos->search_posts($query, $pagina, $por_pagina);
$config['base_url'] = base_url() . 'admin/buscar?query=' . $query;
$config['per_page'] = $por_pagina;
$config['total_rows'] = $this->M_Articulos->contar_filas();
$config['uri_segment'] = 3;
$config['enable_query_strings'] = TRUE;
$config['query_string_segment'] = 'page';
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
}
else
{
$data['sin_resultados'] = 'nada';
}
}
else
{
$data['sin_resultados'] = 'nada';
}
$this->load->view('admin/layouts/header');
$this->load->view('admin/modules/buscar', $data);
$this->load->view('admin/layouts/footer');
}
does anyone know how to make it work?
I'm going to be more specific. What i want is that when i search somethin in search page there is a pagination. Thanks
I am Working pagination in CodeIgniter. I see tutorials and user manual also. but I can't find the way to do it properly they call the database in Controller for pagination. I want a proper solution.
This is The Controller
public function index() {
$this->load->library('pagination');
$config['base_url'] = 'index.php/Emp';
$config['total_rows'] = 200;
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data = array();
$data['showEmployeeTable']=$this->Em->selectEmployeeData(); //FOR SHOWING A EMPLOYEE DATABASE TABLE
if($query = $this->Em->getDesignationData()) //grabs all record
{
$data['records'] = $query;
}
if($query = $this->Em->getCityRecord()) //grabs all record
{
$data['records_city'] = $query;
}
//$this->load->view('emp_update', $data);
$this->load->view('erv', $data);
}
This are My models
public function getDesignationData() {
$query = $this->db->get('emp_desig'); //model created for get data
return $query->result();
}
public function getCityRecord() {
$query = $this->db->get('city'); //model created for get data
return $query->result();
}
// ***************** VEDDING PLAN MODELS **********************************************************
public function selectEmployeeData() {
$query = $this->db->get('emp_manage');
return $query;
}
So how can I show Proper Pagination on the View Page? Kindly answer me step by step. I am a newbie in Codeigniter.
And this in the view.
<?php echo $this->pagination->create_links(); ?>
Your controllers should be
public function index() {
$this->load->library('pagination');
$config = array();
$config['base_url'] = 'index.php/Emp';
$config['total_rows'] = 200;
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['showEmployeeTable']=$this->Em->selectEmployeeData($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
if($query = $this->Em->getDesignationData()) //grabs all record
{
$data['records'] = $query;
}
if($query = $this->Em->getCityRecord()) //grabs all record
{
$data['records_city'] = $query;
}
$this->load->view('erv', $data);
}
Your model should be
public function selectEmployeeData($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get('emp_manage');
return $query;
}
And in your views add following line
<?php echo $links; ?>
see this code and make some changes as per your requirement:
You controller should be:
function list($offset=0)
{
$num_rows=$this->db->count_all("table_name");
$config['base_url'] = site_url('list');
$config['total_rows'] = $num_rows;
$config['per_page'] = 100;
$config['num_links'] = round($config['total_rows']/$config['per_page']);
//$config['use_page_numbers'] = TRUE;
$config['page_query_string']=TRUE;
$this->pagination->initialize($config);
$data["results"] = $this->Em->selectEmployeeData($config["per_page"], $page);
$this->load->view('viewfile',$data);
}
Your Model :
function selectEmployeeData($limit,$offset)//fetch all data with pagination
{
$data = array();
$this->db->limit($limit, $offset);
$Q = $this->db->get('table');
if($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
In View :
<?php echo $this->pagination->create_links();?>
In model/rci_model.php
public function record_count() {
return $this->db->count_all("produk");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men' order by nama_produk ASC");
return $query->result();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
In controller/sandal_clarudo.php
function sandals_for_men(){
$data['seo'] = $this->rci_model->tampil_meta(22);
$this->load->view('head', $data);
$config = array();
$config["base_url"] = "http://localhost/clarudo/index.php/sandal_clarudo/sandals_for_men/";
$config["total_rows"] = $this->rci_model->record_count();
$config["per_page"] = 5;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->rci_model->
fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('men', $data);
$this->load->view('footer');
}
In view/men.php
<?php
foreach($results as $t){
echo "$t->nama_produk";
}
I want to show product that has category = 'Men' only, but using my code it shows all category products. Ideas?
Try This
in cotroller
$count = $this->rci_model->get_count(); //get number of rows
$config['base_url'] = base_url().'index.php/sandal_clarudo/sandals_for_men/';
$config['total_rows'] = $count;
$config['per_page'] = 5;
$config['uri_segment'] = 3;
$limit = $config['per_page'];
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['links'] = $this->pagination->create_links();
$data["results"] = $this->rci_model->fetch_countries($limit, $page);
in model
public function get_count()
{
//count
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men'");
$result = $query->result_array();
$count = count($result);
return $count;
}
//data from table
public function fetch_countries($limit, $page)
{
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men' LIMIT $page, $limit");
$result = $query->result_array();
return $result;
}
in view
//to show data
<?php
foreach ($results as $new_result)
{
echo $new_result['nama_podak'];
}
?>
//to show pagination
<?php
echo $link//Page segment tabs(you can config boostarp for this)
?>
Try this one. You wrong when using limit.
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men' order by nama_produk ASC LIMIT $limit, $start");
$this->db->limit($limit, $start);
$this->db->where('id_kategori', 'men');
$this->db->order_by('nama_produk');
$query = $this->db->get("produk");
Hope this work!
for count use :
public function record_count($limit, $start) {
$this->db->from('count(id_produk) AS count');
$this->db->where('id_kategori', 'men');
$this->db->order_by('nama_produk');
$this->db->limit($limit, $start);
$return = $query->row_array();
return $return['count'];
}
I am having some trouble passing data from 2 queries into my view.
I have a blog system on which people can comment, but i want to show the blog post at the top of my comments page.
To do this I figured I would just pass the results from a query that retrieves the data for the blog entry.
The problem is that I can't acces the array from the view it seems like.
My code so far.
Controller
public function blog($page){
$this->load->library('pagination');
$this->load->model("model_get");
$config['base_url'] = base_url('main/blog/');
$config['total_rows'] = $this->model_get->getBlogcount();
$config['per_page'] = 5;
$this->pagination->initialize($config);
$data["results"] = $this->model_get->getBlog($config['per_page'], $page, "desc");
$this->load->view("content_blog", $data);
}
function comments($page){
$this->load->library('pagination');
$this->load->model("model_get");
$config['base_url'] = base_url('main/comments/'.$this->uri->segment(3).'/');
$config['total_rows'] = $this->model_get->getCommentcount();
$config['per_page'] = 5;
$config['uri_segment'] = 4;
$this->pagination->initialize($config);
$data["blog"] = $this->model_get->getBlog(1,6, 'asc');
$data["results"] = $this->model_get->getComments($config['per_page'], $page);
$this->load->view('content_comment', $data);
}
Model
function getBlog($limit, $start, $sort){
$this->db->order_by("id", $sort);
$results = $this->db->limit($limit, $start)->get('blog');
if ($results1)
{
return $results->result();
}else{
return FALSE;
}
}
function getComments($limit, $start){
$this->db->from('comments');
$this->db->where('blog_id', $this->uri->segment(3));
$this->db->limit($limit, $this->uri->segment(4));
$this->db->order_by("id", "desc");
$results = $this->db->get();
if ($results)
{
return $results->result();
}else{
return FALSE;
}
}
View
foreach($results as $row){
$text = $row->text;
echo "<p>". $text ."</p>";
$name = $this->model_get->getUserById($row->author);
echo "<h4>". $name[0]->login ."</h4>";
}