Codeigniter Pagination issue - php

I am trying to get the pagination to work with codeigniter. But am getting the following errors
Severity: Notice
Message: Undefined offset: 0
Filename: controllers/group.php
And
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/group_list_view.php
Following is the code of my Model
public function get_list($userid)
{
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/portal/index.php/group/index';
$config['total_rows'] =$this->db->where('UserId',$userid)->get('group')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$query['groups'] = $this->db->get('group', $config['per_page'], $this->uri->segment(3));
return $query;
}
Here is the code for my controller
class Group extends CI_Controller {
public function __constructor()
{
session_start();
parent::__constructor();
}
public function index()
{
if(!$this->session->userdata('userid'))
redirect('login');
$this->load->model('group_model');
$result = $this->group_model->get_list($this->session->userdata('$userid'));
//neither this works
data['groups'] = $result;
//nor this
$data['groups'] = $result[0];
$this->load->view('group_list_view', $data);
}
}
And this the code in view
foreach($groups as $group){
echo $group->GroupId;
}
Can anyone please guide me where am i doing it worng. Apparently no one else seems to have encountered any such problem. I followed the tutorial from net.tutsplus ditto but still facing the same issue. Please note i am new to PHP to be gentel :)

You need to write the code in controller not in model
public function index()
{
if(!$this->session->userdata('userid'))
redirect('login');
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/portal/index.php/group/index';
$config['total_rows'] = // Use the same function to get the number of rows
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$this->load->model('group_model');
$result = $this->group_model->get_list($this->session->userdata('$userid'));
data['groups'] = $result;
$this->load->view('group_list_view', $data);
}

Here:
$query['groups'] = $this->db->get('group', $config['per_page'], $this->uri->segment(3));
You're assing the query resultset to $query['group']
but in the controller you're doing
$data['groups'] = $result;
and then you iterate:
foreach($groups as $group){
You should be doing instead:
foreach($groups['groups'] as $group){
Since your original assignment in the model is to $query['groups'].
So, either you assing to $query in the model, or you iterate the correct array in the view (like I showed you above)

Related

Search result pagination not working well...search is working but paging causing errors in codeigniter

My pagination of search results is not working. It's showing result but pagination making it random. Please help me. Search is working but can't apply pagination in it, it's getting complicated
search_products_model
<?php
class search_products_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_results($search_term,$limit, $offset)
{
// Use the Active Record class for safer queries.
$this->db->like('name',$search_term);
// Execute the query.
$query = $this->db->get('tbl_products',$limit, $offset);
if ( $query->num_rows() > 0 )
{
return $query->result_array();
}
else
{
return FALSE;
}
// Return the results.
}
function get_products_id($id)
{
$this->db->where('prod_id',$id);
$query = $this->db->get('tbl_products');
return $query->result();
}
}
?>
controller
<?php
class search_products extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email','pagination'));
$this->load->database();
$this->load->model('search_products_model');
}
function index()
{
$this->search_products();
$this->load->view('search_products_view');
}
function display_registration_form()
{
$this->load->view('search_products_view');
}
function execute_search($offset = 0)
{
$config['base_url'] = '/mehmaa/index.php/search_products/execute_search/';
$config['total_rows'] = $this->db->count_all_results('tbl_products');
$config['per_page'] = 6;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
// Retrieve the posted search term.
$id = $this->input->post('id');
$search_term = $this->input->post('term');
// Use a model to retrieve the results.
$data['results'] = $this->search_products_model->get_results($search_term,$config['per_page'], $offset);
if ( empty($data['results']) )
{
$this->load->view('search_products_view');
$this->session->set_flashdata('message', 'No results found');
}
else{
// Pass the results to`enter code here` the view.
$this->load->view('search_results',$data);
}
}
hi in this link you are not checking the search condition
$config['total_rows'] = $this->db->count_all_results('tbl_products');
change this line to
$config['total_rows'] = $this->db->count_all_results($search_term);
and write the proper query
$config['total_rows'] = $this->db->count_all_results('tbl_products');
Problem in the this files, u need write another query which will be collect result by search without offsets and limits.
For example
function search($text, $offset, $limits){
//this u do search
}
function count_search($text){
//This same logic with search but return Integer value for example
//return $this->db->query()->num_rows();
}

How to Create pagination in codeigniter

Hi I have implemented Pagination in PHP Code But it is not Working while clicking on the pagination links.It is displaying the same data for all the pages.Here is the code.
Controller:
class Testimonial extends CI_Controller {
function __construct() {
parent::__construct();
//here we will autoload the pagination library
$this->load->library('pagination');
}
public function index()
{
$this->load->model('testimonial_model');
$config = array();
$config["base_url"] = base_url('testimonial/index');
$config['total_rows'] = $this->db->count_all("testimonials");//here we will count all the data from the table
$config['per_page'] = 6;//number of data to be shown on single page
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();//create the link for pagination
$data['mainpage'] = "testimonial";
$this->load->view('templates/template',$data);
}
Model:
class Testimonial_model extends CI_Model
{
function get_all_testimonials($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->select('T.*');
$this->db->from('testimonials AS T');
$this->db->where(array('T.status'=>1));
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
}
View:
<div class="pagination"><?php echo $links; ?></div>
Try following will may help you,
public function index()
{
$this->load->model('testimonial_model');
$config = array();
$config["base_url"] = base_url('testimonial/index');
$config['total_rows'] = $this->db->count_all("testimonials");//here we will count all the data from the table
$config['per_page'] = 6;//number of data to be shown on single page
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], (($page-1)*$config["per_page"]));
$data["links"] = $this->pagination->create_links();//create the link for pagination
$data['mainpage'] = "testimonial";
$this->load->view('templates/template',$data);
}
I cant comment so I just make this an answer,
Here
http://bootsnipp.com/snippets/featured/rounded-pagination
This is what I use in making my pagination! and there is alot more of it! I also use CI as my framework!

Unable to change the query in codeigniter pagination

I m using codeigniter pagination class, it works fine when a controller first load, on the next load it not change the data.
Here is my controller:
class test extends CI_Controller{
public function __construct() {
parent:: __construct();
$this->load->model("getdata");
$this->load->library("pagination");
}
public function read($page=0){
$this->load->view('header');
$config['base_url'] = base_url()."index.php/test/read";
$config["total_rows"] = $this->getdata->record_count();
$config["per_page"] = 5;
$this->pagination->initialize($config);
$data["results"] = $this->getdata->basic($config["per_page"],$page);
$data["links"] = $this->pagination->create_links();
$this->load->view('readarea',$data);
$this->load->view('footer');
}
};
?>
Here is my Model:
<?php
class getdata extends CI_Model{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("table1");
}
public function basic($limit,$start){
$this->db->limit($limit,$start);
$query = $this->db->query("SELECT table1.id,table1.post_id,table1.author,table1.quest,table1.first_name,table2.last_name,table2.pic,table2.userid FROM table1,table2 WHERE table1.post_id = table2.id ORDER BY table1.id DESC");
if($query->num_rows() > 0) {
return $query->result();
}
return FALSE;
}
};
?>
NOTE: Links are created, but not get the next data from database...
Try below query in model.
public function basic($limit,$start){
$query = $this->db->query("SELECT table1.id,table1.post_id,table1.author,table1.quest,table1.first_name,table2.last_name,table2.pic,table2.userid FROM table1,table2 WHERE table1.post_id = table2.id ORDER BY table1.id DESC LIMIT $start, $limit");
if($query->num_rows() > 0) {
return $query->result();
}
return FALSE;
}
change this function to this
public function read($page = null){
$this->load->view('header');
$config['base_url'] = base_url()."index.php/test/read";
$config["total_rows"] = $this->getdata->record_count();
$config["per_page"] = 5;
$this->pagination->cur_page = $page; // add this line into it i hope this work
$this->pagination->initialize($config);
$data["results"] = $this->getdata->basic($config["per_page"],$page);
$data["links"] = $this->pagination->create_links();
$this->load->view('readarea',$data);
$this->load->view('footer');
}
};

codeigniter error: I am trying to run my project and Select data from the datatbase but I get this error:

codeigniter errors :I am trying to run my project and Select data from the datatbase but I get this error:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: controllers/NewsController.php
Line Number: 43
Controller:
class NewsController extends CI_Controller{
// num of records per page
private $limit = 10;
function News(){
parent::Controller();
// load library
$this->load->library(array('table','validation'));
// load helper
$this->load->helper('url');
// load model
$this->load->model('NewsModel','',TRUE);
}
function index($offset = 0){
$this->load->model('NewsModel','',TRUE);
// offset
$uri_segment = 3;
$offset = $this->uri->segment($uri_segment);
// load data
$News1 = $this->NewsModel->get_paged_list($this->limit, $offset)->result();
// generate pagination
$this->load->library('pagination');
$config['base_url'] = ('News/index/');
$config['total_rows'] = $this->NewsModel->count_all();
$config['per_page'] = $this->limit;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
// generate table data
$this->load->library('table');
$this->table->set_empty(" ");
$this->table->set_heading('id', 'title', 'image', 'discreption', 'Actions');
$i = 0 + $offset;
$newss=$this->load->model('NewsModel');
foreach($newss as $news){
$this->table->add_row(++$i, $news->title, $news->image,$news->discreption);
anchor('News/view/'.$news->id,'view',array('class'=>'view')).' '.
anchor('News/update/'.$news->id,'update',array('class'=>'update')).' '.
anchor('News/delete/'.$news->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this person?')") );
}
$data['table'] = $this->table->generate();
// load view
$this->load->view('NewsList', $data);
}
Model:
class NewsModel extends CI_Model{
// table name
private $tbl_News= 'news';
function News(){
parent::Model();
}
// get number of News in database
function count_all(){
return $this->db->count_all($this->tbl_News);
}
// get with paging
function get_paged_list($limit = 10, $offset = 0){
$this->db->order_by('id','asc');
return $this->db->get($this->tbl_News, $limit, $offset);
}
// get News by id
function get_by_id($id){
$this->db->where('id', $id);
return $this->db->get($this->tbl_News);
}
// add news
function save($news){
$this->db->insert($this->tbl_News, $news);
return $this->db->insert_id();
}
// update News by id
function update($id, $news){
$this->db->where('id', $id);
$this->db->update($this->tbl_News, $news);
}
// delete News by id
function delete($id){
$this->db->where('id', $id);
$this->db->delete($this->tbl_News);
}
}
here, you are wrong
$newss=$this->load->model('NewsModel');
do it like this
$this->load->model('NewsModel'); //this line just loads model
$newss=$this->NewsModel->your_function_in_model(); //here you call function from models that you ve loaded
Check this line:
$newss = $this->load->model('NewsModel');
You need to call a model function here and not load the model again. I think it should be:
$newss = $this->NewsModel->get_paged_list();
Or, change the foreach to:
foreach($News1 as $news){ #$News1 from $News1 = $this->NewsModel->get_paged_list($this->limit, $offset)->result();
I think you don't know how load->model() works.
$this->load->model('NewsModel'); // will load NewsModel on NewsController
// you can call loaded model by $this->modelName
$newss = $this->NewsModel->get_paged_list(10, $i); // will call get_paged_list() on NewsModel and result will save on newss
There is no such function call in your code. Now you can do this foreach($newss as $news)

pagination using codeigniter

I have a little problem with pagination class of codeigniter
Here is my controller
function index()
{
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/books/index/';
$config['total_rows'] = $this->db->count_all('books');
$config['per_page'] = '5';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$this->pagination->initialize($config);
echo "Here";
//load the model and get results
$this->load->model('books_model');
$data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3));
// load the HTML Table Class
$this->load->library('table');
$this->table->set_heading('Title');
// load the view
$this->load->view('books_view', $data);
}
and here is the view
<body>
<h1>Books</h1>
<?php echo $this->table->generate($results); ?>
<?php echo $this->pagination->create_links(); ?>
</body>
and the model
function get_books($num, $offset) {
$query = $this->db->get('books', $num, $offset);
return $query;
}
I setup my database, and routes (default->books) already but the page doesn't show anything. My database table is books.
I don't know if $config is a variable of your own class. If it is the global config, maybe your should set its parameters using $this->config->set_item function.
Furthermore, to avoid troubles like #Henesnarfel mentioned before, try to echo the number of results when you execute the query.
function index()
{
$this->load->library('pagination');
$this->config->set_item('base_url',base_url().'index.php/books/index/');
$this->config->set_item('total_rows',$this->db->count_all('books'));
// ECHO to make me sure that the query is succesfully done
echo("Found ".$this->db->count_all('books')." books");
$this->config->set_item('per_page','5');
$this->config->set_item('full_tag_open','<p>');
$this->config->set_item('full_tag_close','</p>');
$this->pagination->initialize($config);
echo "Here";
//load the model and get results
$this->load->model('books_model');
$data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3));
// load the HTML Table Class
$this->load->library('table');
$this->table->set_heading('Title');
// load the view
$this->load->view('books_view', $data);
}

Categories