I tried to use the codeigniter pagination library and it doesn't work for me. I'm trying to have 24 products on the all products page and when there's more products it should add a link to the next page. So right now I have more than 24 products on the site and no new page is added.
with this line I'm getting all the products from product table:
$data['products'] = $this->Product_model->selectProducts();
This is my controller function:
public function pagination() {
//load pagination library
$this->load->library('pagination');
//laad Products_model voor pagination
$this->load->model('Products_model');
$this->load->model('Product_model');
$config = array();
$config["base_url"] = base_url() . "AlleCadeausController/pagination";
$config["total_rows"] = $this->products->record_count();
$config["per_page"] = 24;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['products'] = $this->Product_model->selectProducts();
$data["links"] = $this->pagination->create_links();
$this->load->view("allecadeaus", $data);
}
My model (Products_model):
<?php
class Products extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("products");
}
public function fetch_products() {
$query = $this->db->get("products");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
?>
This is my view file (I don't know if I need to add anything in the view file for the pagination to work):
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="all-content">
<div class="row">
<div class="col-lg-12 bg-warning header-text" style="font-size:25px;font-weight: bold;">
<center>Alle cadeaus</center>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="row">
<div class="menu">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<div class="categorieen">
<?php foreach (get_categories_h() as $category) : ?>
<input id="box1" type="checkbox" />
<label for="box1"><a class="listcat" href="<?php echo base_url().'Product/category/' . $category->id; ?>"> <?php echo $category->name; ?></a></label>
<?php endforeach; ?>
</div>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="main">
<?php foreach($products as $product) : ?>
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product["product_id"]; ?>"> <div class="main-products">
<img id="cadeaufoto" src="<?php echo base_url(); ?>upload/<?php echo $product["product_foto_thumb"]; ?>">
<div class="product_naam"><h3><?php echo $product["product_naam"]; ?></h3></div>
<div class="ophaal_plaats">
<?php echo $product["ophaal_plaats"]; ?>
</div>
</a>
<div class="aangeboden_door">
<?php
//Here is the active record query which is getting the 'voorname' and other data
$userarray = $this->db->get_Where('users', array('user_id'=>$product["user_id"]))->row_array();
// you can print_r($userarray); for see the array you get
?>
<a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id'];?>">
<td><?php echo $userarray['voornaam'];?></td>
</tr>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php');?>
Below is the fine working pagination code, hope it will fix the issue you have.
Model
public function fetch_oldusers($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get_where('dc_user',array('Is_Hidden'=>0));
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
View
<div class="text-center"><nav aria-label="Page navigation">
<ul class="pagination">
<li><?php echo $links; ?></li>
</ul>
</nav></div>
Controller
public function Users()
{
$config = array();
$config["base_url"] = base_url() . "master/Users";
$config["total_rows"] = $this->bm->record_countolduser();
$config['use_page_numbers'] =True;
$config['cur_tag_open'] = '<a><b class="text-success">';
$config['cur_tag_close'] = '</b></a>';
$config["per_page"] =10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['title'] = "Users List";
$data["posts"] = $this->bm->fetch_oldusers($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('Admin/header',$data);
$this->load->view('Admin/nav');
$this->load->view('Admin/sidebar');
$this->load->view('Admin/old_user', $data);
$this->load->view('Admin/footer');
}
It is working fine for me.
Related
I have a problem with pagination.when I click on the next but it brings the above error, but when I click on the previous link it works normally. here is my code for the controller:
public function categoryfour()
{
$config = array();
$config["base_url"] = base_url() . "bulletins/categoryfour";
$total_row = $this->news_model->record_count_category_four();
$config["total_rows"] = $total_row;
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["posts"] =$this->news_model->get_news_cat_four($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["pages"] = explode(' ',$str_links );
$this->load->view('category_one',$data);
}
here is the code for the model:
function get_news_cat_four($limit, $start)
{
$this->db->select('news.id as newsId,news_category.id as catId,users.id as userId,news.*,news_category.*,users.*,comments.*, COUNT(comments.post_id) as num_comments');
$this->db->join('users' , 'users.id = news.user_id');
$this->db->join('news_category' , 'news_category.cat_id = news.news_id');
$this->db->join('comments' , 'comments.post_id = news.id');
$this->db->group_by('comments.post_id');
$this->db->order_by('news.date_added','desc');
$this->db->where('news.news_id',4);
$this->db->limit($limit, $start);
$query = $this->db->get("news");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;}
and finally the view:
<div id="page-content" class="single-page container">
<div class="gutter-7px">
<div id="main-content" class="col-md-8 fix-right">
<article class="single-post">
<?php if(count($posts)) {
foreach($posts as $n){ ?>
<div class="entry-header">
<h1 class="entry-title"><?php echo $n->title;?></h1>
<span> <i class="fa fa-calendar"></i> <?php echo date( 'F jS, Y g:i a' , strtotime($n->date_added))?>/
<i class="fa fa-comment-o"></i>
<?php echo $n->num_comments;?>/<i class="fa fa-user"></i>
<?php echo $n->username?></span>
</div>
<div class="post-thumbnail-wrap"><a href="<?php echo base_url();?>bulletins/view/<?php echo $n->newsId?>"><img src="<?php echo base_url('assets/images/'.$n->image)?>"
style="width:800px;height:500px" /></a></div>
<div class="entry-content">
<p><?php echo substr(strip_tags($n->content), 0, 200).'...';?></p>
Read More...
<?php }}?>
</div>
</article>
<div id="pagination">
<ul class="pagination">
<!-- Show pagination links -->
<?php foreach ($pages as $link) {
echo "<li>". $link."</li>";
}
?>
</div>
</div>
</div>
I'm trying to make pagination using Codeigniter builtin pagination library but do not get results from database in view.
I dont know where I'm wrong.
If someone can help me, I will post my code below. Thanks
Model
public function showAll($limit, $offset){
$this->db->limit($limit);
$this->db->offset($offset);
return $this->db->get('jobs')->result();
}
public function countAll($value='')
{
return $this->db->get('jobs')->num_rows();
}
Controller
$total = $this->JobsM->countAll();
$limit = 3;
$offset = $page;
$config['base_url'] = base_url('jobs/');
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 2;
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$data['myData'] = $this->MusiciansM->myProfile();
$data['jobList'] = $this->JobsM->showAll($offset, $limit);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('user/header', $data);
$this->load->view('public/job-list', $data);
$this->load->view('public/footer');
View
<div class="row">
<?php if(isset($jobList)):?>
<?php foreach($jobList as $job): ?>
<div class="col-md-12 job-item">
<a href="<?php echo base_url().'jobs/'.$job->id.'/'.url_title($job->title);?>" class="thumbnail">
<h3 class="job-title"><?php echo word_limiter($job->title, 5);?> <small> <?php echo $job->musicianType;?> </small></h3>
<p class="description"><?php echo character_limiter($job->description, 200);?></p>
<?php $date = date('d-m-Y', strtotime($job->date));?>
<p><small class="dateandplace"> <?php echo $date;?> </small></p>
</a>
</div>
<?php endforeach;?>
<?php endif;?>
</div>
<!-- Pagination -->
<div class="row pagination">
<div class="col-md-12">
<?php print_r($pagination) ;?>
</div>
</div>
<!--/ Pagination -->
Routes
$route['jobs/(:any)'] = 'jobs';
$route['jobs/(:num)/(:any)'] = 'jobs/preview/$1';
Thank you
$this->pagination->create_links(); returns pagination in form of string.
Printing the string in print_r is wrong.
<!-- Pagination -->
<div class="row pagination">
<div class="col-md-12">
<?php print_r($pagination) ;?>
</div>
</div>
<!--/ Pagination -->
The correct form is:
<!-- Pagination -->
<div class="row pagination">
<div class="col-md-12">
<?php echo $pagination; ?>
</div>
</div>
<!--/ Pagination -->
I'm getting this error when i try to execute 2 function in the same controller. the 1st one works fine, but get the error
'Fatal error. Cannot use object of type stdClass as array in
C:\xampp\htdocs\system\libraries\Table.php on line 256'.
Controller
<?phpif (!defined('BASEPATH'))
exit('No direct script access allowed'); class tables extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('session');
$this->load->library('table');
$this->load->library('pagination');
$this->load->database();
//load the search model
$this->load->model('mtables');
}
public function index() {
}
public function current_users() {
$config = array();
$config["base_url"] = base_url() . "tables/current_users";
$table = 'dept_officer_view';
$total_row = $this->mtables->record_count($table);
$config["total_rows"] = $total_row;
$config["per_page"] = 10;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 20;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$result_per_page = 10;
$data["links"] = explode(' ', $str_links);
$datatable1 = $this->mtables->fetch_data($result_per_page, $this->uri->segment(3), $table);
$this->load->view('header');
$this->load->view('dept_officer_table_view', array(
'datatable1' => $datatable1,
'result_per_page' => $result_per_page
));
$this->load->view('footer');
}
public function crpo_users() {
$config = array();
$config["base_url"] = base_url() . "tables/crpo_users";
$table = 'crpo_view';
$total_row = $this->mtables->record_count($table);
$config["total_rows"] = $total_row;
$config["per_page"] = 10;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 20;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$result_per_page = 10;
$datatable2 = $this->mtables->fetch_data($result_per_page, $this->uri->segment(3), $table);
$this->load->view('header');
$this->load->view('crpo_table_view', array(
'datatable2' => $datatable2,
'result_per_page' => $result_per_page));
$this->load->view('footer');
}
)
?>
Model
<?php
class mtables extends CI_Model {
function __construct() {
parent::__construct();
}
// Count all record of table "contact_info" in database.
public function record_count($table) {
return $this->db->count_all("$table");
}
// Fetch data according to per_page limit.
public function fetch_data($limit,$start,$table) {
$this->db->limit($limit, $start*$limit);
$query = $this->db->get("$table");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}?>
View 1
crpo_table_view.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<div id="wrapper">
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header"> Users</h1>
<ol class="breadcrumb">
<li>Dashboard</li>
<li>View Users</li>
<li class="active">Child Rights Promoting Officer</li>
</ol>
</div>
<div class="col-lg-3 col-sm-offset-1">
Department Officers
</div>
<div class="col-lg-4">
Child Rights Promoting Officers
</div>
<div class="col-lg-4">
Donors/ Foster Parents
</div>
</div>
<!-- pagination -->
<div>
<?php
// generate the table
$this->table->set_heading('Fisrt name', 'Last name', 'Contact no','Username', 'Email','Official Address','DS Division','District');
echo $this->table->generate($datatable2);
// generate the page navigation
echo $this->pagination->create_links();
?>
</div>
</div>
</div>
View 2
donor_table_view.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<div id="wrapper">
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header"> Users</h1>
<ol class="breadcrumb">
<li>Dashboard</li>
<li>View Users</li>
<li class="active">Donors/Foster Parents</li>
</ol>
</div>
<div class="col-lg-3 col-sm-offset-1">
Department Officers
</div>
<div class="col-lg-4">
Child Rights Promoting Officers
</div>
<div class="col-lg-4">
Donors/ Foster Parents
</div>
</div>
<!-- pagination -->
<div>
<?php
// generate the table
$this->table->set_heading('Fisrt name', 'Last name', 'Contact no','Address','Username', 'Email');
echo $this->table->generate($datatable3);
// generate the page navigation
echo $this->pagination->create_links();
?>
</div>
</div>
</div>
So, the problem is in:
function _prep_args($args) {
// If there is no $args[0], skip this and treat as an associative array
// This can happen if there is only a single key, for example this is passed to table->generate
// array(array('foo'=>'bar'))
if (isset($args[0]) && count($args) === 1 && is_array($args[0]) && ! isset($args[0]['data'])) {
$args = $args[0];
}
foreach ($args as $key => $val) {
is_array($val) OR $args[$key] = array('data' => $val);
}
return $args;
}
If $args is not an array, but an stdClass, then that would explain the error you get. To find out where it's called from with the wrong argument, you could either add
if ( is_object( $args ) ) throw new Exception("Bug!");
or
if ( is_object( $args ) ) { echo "<pre>"; debug_print_backtrace(); echo "</pre>"; }
This will do it:
$sql = "SELECT * FROM users";
$stmt = $pdo->query($sql);
while( $res = $stmt->fetch(PDO::FETCH_ASSOC)){
$res['row_name'];
};
I have a problem using codeigniter pagination. The problem is when I open the 2nd page during paging it is showing the error 404 page not found. I do not know why but for the 1st page it is working.
Model:
public function home($limit='',$segment=''){
$data = array();
$this->db->where(array('produk.status_produk'=>'1'))->limit($limit,$segment);
$i=0;
foreach ($this->get_all() as $item){
$data[$i] = $item;
$this->db->where(array('produk_id'=>$item->id_produk,'default'=>'1'));
if($this->db->get('foto_produk')->num_rows() > 0){
$this->db->where(array('produk_id'=>$item->id_produk,'default'=>'1'));
$foto = $this->db->get('foto_produk')->result();
foreach ($foto as $pic) {
$data[$i]->image = $pic->image;
$data[$i]->thumb = $pic->thumb;
}
} else {
$data[$i]->image = '';
$data[$i]->thumb = '';
}
$i++;
}
return $data;
}
Controller:
$cari = $this->input->post('search');
$this->db->like('deskripsi_produk', $cari);
$this->db->or_like('nama_produk', $cari);
$this->db->order_by('id_produk','desc');
$t = $this->produk_m->home();
$this->data->search = $this->input->post('search');
$this->load->library('pagination');
$perPage = 2;
$uri_segment = 2;
$total = count($t);
$config['base_url'] = site_url('search');
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = '2';
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$this->data->pagination = $this->pagination->create_links();
$this->db->like('deskripsi_produk', $cari);
$this->db->or_like('nama_produk', $cari);
$this->db->order_by('id_produk','desc');
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(2));
$this->template->set_judul('PT Bintang Sriwijaya')
->set_css('style')
->set_parsial('sidebar','sidebar_view',$this->data)
->set_parsial('topmenu','top_view',$this->data)
->render('search',$this->data);
}
View:
<h2>Hasil Pencarian</h2><br />
<?php if($produk):?>
<h3><center><?php echo $pagination;?></center></h3><br />
<?php foreach($produk as $item): ?>
<div class="produk-wrap">
<div class="image-wrap">
<div class="image-iner">
<a href="<?php echo base_url().'index.php/produk/'.$item->nama_kategori.'/'.$item->url_produk;?>">
<?php if($item->thumb == ''): ?>
<div class="no-image">
<span>Belum ada Gambar</span>
</div>
<?php else: ?>
<img src="<?php echo base_url().$item->thumb ?>" />
<?php endif; ?>
<?php if ($item->stok == 0): ?>
<div class="trans">
<span>Stok Habis</span>
</div>
<?php endif; ?>
</div>
</div>
<div class="produk-name">
<?php echo $item->nama_produk;?>
</div>
<?php if($item->harga_baru != 0): ?>
<div class="harga-lama">
Rp. <?php echo $this->cart->format_number($item->harga_jual) ?>
</div>
<div class="harga-jual">
Rp. <?php echo $this->cart->format_number($item->harga_baru) ?>
</div>
<?php else: ?>
<div class="harga-jual">
Rp. <?php echo $this->cart->format_number($item->harga_jual) ?>
</div>
<?php endif; ?>
</a>
</div>
<?php endforeach; ?>
<?php else:
echo "Data yang dicari belum ada";
endif;?>
please change these lines
$uri_segment = 2;
to
$uri_segment = 3;
and
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(2));
to
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(3));
and try beacause segment 1 is for controller, and 2 for methods in it, 3 may be the pagination element.
Basically Im using Bootstrap themes with Codeigniter
Here is my controller code. tutors is the table from database.
public function index() {
$this->load->library('pagination');
$config['base_url'] = 'http://localhost:8888/bootstrap/index.php/frontpage/index';
$config['total_rows'] = $this->db->get('tutors')->num_rows();
$config['per_page'] = 6;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$data['records'] = $this->db->get('tutors', $config['per_page'], $this->uri->segment(3));
$this->load->view('include/header');
$this->load->view('frontpage', $data);
$this->load->view('include/footer');
}
I want to customize the view as the followings
<div class="container" style="text-align: center;">
<?php
$i=0;
foreach($records as $row) :
if ($i%3==0) { ?>
<div class="row">
<div class="col-md-4">
<h1><?php echo $row->title; ?></h1>
<h2><?php echo $row->id; ?></h2>
</div>
<?php
} else {
?>
<div class="col-md-4">
<h1><?php echo $row->title; ?></h1>
<h2><?php echo $row->id; ?></h2>
</div>
<?php
if ($i%3==2) {
echo "</div>";
}
}
$i++;
endforeach;
?>
</div>
<div class="container">
<?php echo $this->pagination->create_links(); ?>
</div>
But it gets an error. How can I modify it?
Thanks guys.
Try to use
foreach ($query->result() as $row)
instead of foreach($records as $row)