I am using CI 2.1.0 with HMVC. I am going to implement pagination in view part. But I get 'Undefined table data' error. It displays paging number (1,2,3,..) but does not display records.
My code is :
included library for pagination :
$this->load->library('table');
controller
$data['title'] = "Test Events";
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/myproject/index.php/events/test/';
$config['total_rows'] = $this->eventModule->countRow();
$config['per_page'] = '3';
$this->pagination->initialize($config);
$config['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));
$data['main_content'] = 'test';
$this->load->view('event/template', $data);
view
<?php echo $this->table->generate('records'); ?>
<?php echo $this->pagination->create_links(); ?>
Thank you for support.
Finally I get the solution. It's silly mistake at
$config['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));
It should be $data instead of $config
$data['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));
Related
How I can change the color and width of table generated by table library in CodeIgniter
Controller
<?php
class Addbalance extends CI_Controller{
function index(){
if($this->session->userdata('logged_in')){
$this->load->library('pagination');
$this->load->library('table');
$config['base_url']='http://localhost/elvan/addbalance';
$config['total_rows'] = $this->db->get('products')->num_rows() - 1;
$config['per_page']=1;
$config['num_links']=10;
$config['use_page_numbers'] = TRUE;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$this->pagination->initialize($config);
$data['records']=$this->db->get('products',$config['per_page'],$this->uri->segment(2));
$this->load->model('Addbalance_m');
$this->load->view('addbalance_v',
array('order'=>$this->Addbalance_m->index(),
'provider'=>$this->Addbalance_m->get_provider_data()
,'mydata'=>$data
));
}
else {
redirect('login','refresh');
}
}
}
?>
View
<?php
$this->table->set_heading('id','product name','quanity','yoo','yoo');
echo $this->table->generate($mydata['records']);
echo '<div id="pagination">'.$this->pagination->create_links().'</div>';
?>
I want to use for each if that possible, but I don't know how to fetch the columns from this library in the view
It is done by modifying the Table class template. Documentation Here
To add a particular class to the <table> and another to all <tr>s for example.
$template = array(
'table_open' => "<table class='my-table-class' id='my-fancy-table'>",
'row_start' => "<tr class='my-row-class'>",
);
//apply the above to the table
$this->table->set_template($template);
I have to add pagination to my site and the link does not appear / shown
I Have an controller like this :
$data['a'] = 9;
$data['title'] = 'Articles';
$data['content'] = 'home/articles';
$this->load->library('pagination');
$q = $this->db->get_where('articles',array('article_category_id' => 18));
$config['uri_segment'] = 2;
$config['total_rows'] = $q->num_rows();
$config['per_page'] = 3;
$config['base_url'] = base_url() . 'article/';
$this->pagination->initialize($config);
$this->db->order_by('id','desc');
$this->db->limit($config['per_page'],$this->uri->segment(2));
$q = $this->db->get_where('articles',array('article_category_id' => 18));
$data['blogs'] = $q;
$data['pagination'] = $this->pagination->create_links();
$this->load->view('home/index', $data);
and view like this :
//Body of the site
Halaman : <?php echo $pagination; ?>
Site View
i have a blog where posts are shown in under different category id. In category home page i want to show 10 posts per page. But my pagination is not working. Can anybody help please?
//////////Controller///////
public function categoryDetails($category_id)
{
$data = array();
$this->load->library('pagination');
$config['base_url'] = base_url() . 'Welcome/categoryDetails/'.$category_id;
$data['total_rows'] = $this->WelcomeModel->select_number_of_published_blog_by_category_id($category_id);
$config['per_page'] = 2;
$this->pagination->initialize($config);
$data['allPost'] = $this->SuperEditorModel->select_all_blog_post_with_category($category_id,$config['per_page'], $this->uri->segment(3));
$this->load->view('frontend/master', $data);
You have to use create_links() function to load the pagination links. You can add it to your data array and pass it to the view
$data["pagination_links"] = $this->pagination->create_links();
$this->load->view('frontend/master', $data);
From the documentation:
create_links()
Returns: HTML-formatted pagination
Return type: string
Returns a “pagination” bar, containing the generated links or an empty
string if there’s just a single page.
add this in your controller to create pagination link
$data["links"] = $this->pagination->create_links();
And your view page where are you want to view pagination link
<?php echo $link; ?>
I am doing pagination in in my page, but I am getting error like this.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$login_model
Filename: controllers/home.php
Line Number: 41
Fatal error: Call to a member function getDet() on a non-object in
C:\xampp\htdocs\picasso\new_design\photo_frame\application\controllers\home.php
on line 41
This is my code
Model:
function getDet(){
$query_str = "SELECT * FROM frame";
$result = $this->db->query($query_str);
return $result;
}
Controller:
public function frame1()
{
$things = $this->login_model->getDet();
$total_rows = $things->num_rows();
$this->load->library('pagination');
$config['base_url'] = base_url() . 'home/';
$config['total_rows'] = '$total_rows';
$config['per_page'] = '1';
$this->pagination->initialize($config);
$this->view_data['pagination'] = $this->pagination->create_links();
$this->view_data['things'] = $things;
$this->load->model('login_model');
$data1['wood']=$this->login_model->getDetails('wood');
$this->load->view('frame1',$data1);
}
View:
<div>
<?php if($wood!=""||$wood!=null){foreach($wood as $row){ ?>
<div class="frm1" style="border:1px solid rgb(204, 204, 204); background-color: rgb(244, 244, 244);list-style-type:none; float:left; display:inline; padding:20px;">
<img src="<?php echo base_url()."uploads/frame/icon/".$row['iconimage'];?>" style="cursor:pointer" class="changemount" data-id="<?php echo base_url()."uploads/frame/".$row['frameimage'];?>"
data-price="<?php echo $row['price'];?>" data-thick="<?php echo $row['thickness'];?>" data-change="0" data-w="515" data-h="356" data-ch="385" data-cw="554" /></div>
<?php }} ?>
</div>
<?php echo $pagination; ?>
</div>
You have to load the model, before using it.
public function frame1() {
$this->load->model('login_model'); //You have to load this model, before use it
$things = $this->login_model->getDet(); $total_rows = $things->num_rows();
$this->load->library('pagination');
$config['base_url'] = base_url() . 'home/';
$config['total_rows'] = '$total_rows';
$config['per_page'] = '1';
$this->pagination->initialize($config);
$this->view_data['pagination'] = $this->pagination->create_links();
$this->view_data['things'] = $things;
$data1['wood']=$this->login_model->getDetails('wood');
$this->load->view('frame1',$data1);
}
undefined login_model, means you call the model without loading it, add this to your _construct in controller
$this->load->model("model_name");
and also in your view you miss something important, to execute your code in loop do the code below
//add the ->result() in your var $wood
foreach($wood->result() as $row)
this will fix your error
Okay, I am pretty new in CI and I am stuck on pagination. I am performing this pagination on a record set that is result of a query. Now everything seems to be working fine. But there's some problem probably with the link. I am displaying 10 results per page. Now if the results are less than 10 then it's fine. Or If I pull up the entire records in the table it works fine. But in case the result is more than 10 rows, then the first 10 is perfectly displayed, and when I click on the pagination link to get to the next page the next page displays the rest of the results from the query as well as, other records in the table. ??? I am confused.. Any help??
Here's the model code I am using ....
function getTeesLike($field,$param)
{
$this->db->like($field,$param);
$this->db->limit(10, $this->uri->segment(3));
$query=$this->db->get('shirt');
if($query->num_rows()>0){
return $query->result_array();
}
}
function getNumTeesfromQ($field,$param)
{
$this->db->like($field,$param);
$query=$this->db->get('shirt');
return $query->num_rows();
}
And here's the controller code ....
$KW=$this->input->post('searchstr');
$this->load->library('pagination');
$config['base_url']='http://localhost/cit/index.php/tees/show/';
$config['total_rows']=$this->T->getNumTeesfromQ('Title',$KW);
$config['per_page']='10';
$this->pagination->initialize($config);
$data['tees']=$this->T->getTeesLike('Title',$KW);
$data['title']='Displaying Tees data';
$data['header']='Tees List';
$data['links']=$this->pagination->create_links();
$this->load->view('tee_res', $data);
What am I doing wrong here ???? Pls help ...
I guess the problem is with the $KW=$this->input->post('searchstr'); ..
Because if I hard code a value for $KW it works fine. May be I should use POST differently ..but how do I pass the value from the form without POSTING it , its CI so not GET ... ??????
In Controller
<?php
$KW=$this->input->post('searchstr');
$this->load->library('pagination');
$count = $this->model_name->getNumTeesfromQ($KW);//Count
$config['base_url']= base_url().'index.php/tees/show/';
$config['total_rows']= $count;
$config['per_page']='10';
$config['uri_segment'] = 4;
$limit = $config['per_page'];
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data['links'] = $this->pagination->create_links();
$data['tees']=$this->model_name->getTeesLike($KW,$limit,$page);
$data['title']='Displaying Tees data';
$data['header']='Tees List';
$this->load->view('tee_res', $data);
In Model
public function getNumTeesfromQ($KW)
{
$query = $this->db->query("SELECT * FROM title WHERE table_field='$KW'");
$result = $query->result_array();
$count = count($result);
return $count;
}
public function getTeesLike($KW,$limit,$page)
{
$query = $this->db->query("SELECT * FROM title WHERE table_field='$KW' LIMIT $page, $limit");
$result = $query->result_array();
return $result;
}
in view
echo all data using foreach
then at bottom of page use <?php echo $links; ?>this will show your Pagination