Codeigniter pagination do not display results - php

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 -->

Related

Codeigniter pagination not adding new page

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.

How to change data variable value in codeigniter?

I'm trying to create a search form in codeigniter. This is my controller and model,
Shop.php [ Controller ]
public function s(){
$q = $this->input->get('q');
if( $q == null ){
$this->index();
}
$data['products'] = $this->product_model->search_products( $q );
$this->load->view('shop', $data);
}
Product_model.php
public function search_products( $keyword ){
$this->db->select('product.id, product.product_name, product.product_description, product.product_image1,
brand.brand_name, category.category_name, type.type_name, stock.selling_price,
(SELECT COALESCE(ROUND(SUM(rate)/( COUNT(rate)*5 )*100), 0) FROM rating WHERE rating.product_id = product.id)AS rating,
(SELECT COALESCE(COUNT(rating.id), 0) FROM rating WHERE rating.product_id = product.id)AS rate_count');
$this->db->from('product');
$this->db->join('stock','stock.product_id = product.id');
$this->db->join('brand', 'brand.id = product.brand_id');
$this->db->join('category', 'category.id = product.category_id');
$this->db->join('type', 'type.id = product.type_id');
$this->db->join('color', 'color.id = product.color_id');
$this->db->where('MATCH(product.product_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(brand.brand_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(category.category_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(type.type_name) AGAINST("'.$keyword.'")');
$this->db->where('stock.qty >', 0);
$this->db->group_by('product.id');
$query = $this->db->get();
return $query->result_array();
}
But return value in null. So I tried calling search_product( $keyword ) function in my view and it worked. but when I print $data['product'] variable and its empty.
What is the problem here? Thank you.
Edit
The search result shows here.
<div class="row product-list-item">
<?php
// This is just used for check the query
$p = $this->product_model->search_products( 'mesh' );
echo '<br>';
print_r($products);
if( $products != null ){
foreach ( $products as $product ){
?>
<!-- item.2 -->
<div class="product-item-element col-sm-6 col-md-6 col-lg-4">
<!--Product Item-->
<div class="product-item">
<div class="product-item-inner">
<div class="product-img-wrap">
<img src="<?php echo base_url($product['product_image1']);?>" alt="">
</div>
<div class="product-button">
<i class="fa fa-eye"></i>
</div>
</div>
<div class="product-detail">
<p class="product-title"><a href="<?php echo site_url('shop/view/'.$product['id'])?>">
<?php echo $product['product_name']; ?>
</a></p>
<div class="product-rating">
<div class="star-rating" itemprop="reviewRating" itemscope="" itemtype="" title="Rated 4 out of 5">
<span style="width: <?php echo $product['rating'];?>%"></span>
</div>
<a href="#" class="product-rating-count">
<span class="count"><?php echo $product['rate_count'];?></span> Reviews
</a>
</div>
<p class="product-description">
<?php echo $product['product_description']; ?>
</p>
<h5 class="item-price"><?php echo $product['selling_price']; ?></h5>
</div>
</div>
<!-- End Product Item-->
</div>
<?php
}
}else{
?>
<div class="text-center no-items">
<h4>No items</h4>
</div>
<?php } ?>
</div>

Limiting number of items to display per page

I have files like (for an instance ) being shown as shown in
http://lawcommission.gov.np/site/NepalLawCommissionFinal/NepalLawCommissionFinal/document.php?cat=112&sub_cat=10008
The document.php has following html to display the files:
<section class="three-fourth">
<div class="sort-by">
<h3>Sort by</h3>
<ul class="sort">
<li>Date ascendingdescending</li>
<li>Name ascendingdescending</li>
<li>Category ascendingdescending</li>
</ul>
<ul class="view-type">
<li class="grid-view">grid view</li>
<li class="list-view">list view</li>
</ul>
</div>
<div class="deals clearfix">
<!--deal-->
<?php
$id = $_GET['cat'];
$base_obj = new Base();
$lst = $base_obj->list_doc($id);
$lst->execute();
foreach ($lst as $rec)
{
$sub_head = $rec['Head_Id'];
$get_sub = $base_obj->list_by_id('Head_Id', $sub_head, 'tbl_heading');
$get_sub->execute();
foreach ($get_sub as $h)
{
$heading = $h['Head_Name'];
}
?>
<article class="one-fourth">
<figure><span style="float:left"><img src="images/uploads/london1.jpg" alt="" style="width:100px" /></span><span style="float:left"></span></figure>
<div class="details">
<h1><?=$rec['Doc_Name'];?></h1>
<span class="address"><?=$heading;?></span>
<span class="price">Added On <em><?=substr($rec['Uploaded_Date_Time'],0,10);?></em> </span>
Download
</div>
</article>
<!--//deal-->
<?php
} ?>
<!--bottom navigation-->
<div class="bottom-nav">
<!--back up button-->
Back up
<!--//back up button-->
<!--pager-->
<div class="pager">
<span>First page</span>
<span><</span>
<span class="current">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>></span>
<span>Last page</span>
</div>
<!--//pager-->
</div>
<!--//bottom navigation-->
</div>
</section>
<!--//three-fourth content-->
</div>
<!--//main content-->
</div>
<div class="wrap clearfix">
<!--info boxes--><!--//info boxes-->
</div>
Can you guys please suggest a way to show only limited no. of pdf files in one page?
here is how you need to change your function:
public function list_doc($cat, $page=0) {
$itemsPerPage = 20;
$cat = intval($cat, 10);
$start = intval($page, 10) * $itemsPerPage;
if ($start < 0) $start = 0;
$qry = $this->dbobj->db1->prepare("SELECT SQL_CALC_FOUND_ROWS *
FROM tbl_documents, tbl_cat_head_doc
WHERE tbl_cat_head_doc.Cat_Id = " . $cat ." AND
tbl_cat_head_doc.Doc_Id = tbl_documents.Doc_Id AND
tbl_documents.Status = 'a'
limit " . $start . "," . $itemsPerPage);
return $qry;
}
and in main page call function like this:
$lst = $base_obj->list_doc($id, $_GET['page']);
to prevent next question: read about SQL_CALC_FOUND_ROWS and FOUND_ROWS()

Codeigniter: Paginate data without using table library

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)

Trying to get property of non-object in Codeigniter?

My controller code just generate ten copies of the same data. And I put the data in an array of array.Controller Code Below:
for($i=0;$i < $this->per_page;$i++)
{
$temp['title'] = "test";
$temp['link'] = "localhost/cib/index.php";
$temp['month'] = '4';
$temp['day'] = '21';
$temp['authorlink'] = "localhost/cib/index.php/author";
$temp['author'] = "Charles";
$temp['content'] = "tetestersafaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
$results[] = $temp;
}
$data['main_content'] = 'report_list';
$data['posts'] = $results;
$this->load->view('include/templates', $data);
And I show the data generated by controller in the view page report_list, the code of report_list is
<div id="bg_top">
</div>
<div class = "container" id="content">
<div class="row-fluid">
<div class="span9" id="content_left">
<h2>解题报告列表</h2>
<link href="<?php echo base_url('assets/css/style.css') ?>" rel="stylesheet">
<?php if(!empty($posts) && is_array($posts)):?>
<?php foreach($posts as $post):?>
<div class="entry">
<h2 class="entrytitle"><?php echo anchor($post->link, $post->title)?><span></span></h2>
<div class="entrymeta1">
<div class="meta-date">
<span class="meta-month"><?php echo $post->month?></span>
<span class="meta-day"><?php echo $post->day?></span>
</div>
<span class="meta-author"><?php echo anchor($post->authorlink, $post->author)?></span>
<span class="meta-category"></span>
<span class="meta-comment"></span>
<div class="clear"></div>
</div><!-- [entrymeta1] -->
<div class="entrybody">
<p><?php echo $post->content;?><?php echo anchor($post->link, '阅读全文')?></p>
</div>
<div class="entrymeta3">
</div>
</div><!-- [entry] -->
<?php endforeach;?>
<?php endif;?>
</div>
<div class="span3">
<?php echo "hello world2";?>
</div>
</div>
</div>
The question is where am wrong? why is there errors "Trying to get property of non-object"?
I tried to print the data in the controller, it works quite well.
foreach($results as $post)
{
foreach($post as $key=>$value)
{
echo $key.':'.$value;
echo '<br/>';
}
}
You need to print the variables in the view file in array style like this:
<?php echo anchor($post['link'], $post['title'])?>
Still you can always print the array to view the structure:
<?php echo "<pre>";print_r($posts);echo "</pre>";
My assumption is that
You are expecting $posts as array of objects. But in $posts in some loaction there is no object with property month, day etc. So just before the for loop you should look the type of $posts by
var_dump($posts)
This will give you the clear picture about your error
Change all the variables $post->link to array $post['link']
$post->month
$post->day
every fields that are using in template.

Categories