I am using Ajax_pagination library in CodeIgniter 3.0.The pagination is working fine.Now i want to pass more parameters with page itself.
For Example I have a page where User select Country and Category.Now i want to show data with pagination.Here is my code.
function ajaxPaginationData()
{
$page = $this->input->post('page');
if(!$page){
$offset = 0;
}else{
$offset = $page;
}
$totalRec = count($this->post->getRows());
//pagination configuration
$config['first_link'] = 'First';
$config['div'] = 'postList'; //parent div tag id
$config['base_url'] = base_url().'index.php/posts/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
$data['posts'] = $this->post->getRows(array('start'=>$offset,'limit'=>$this->perPage));
$this->load->view('posts/ajax-pagination-data', $data, false);
}
Here is my pagination html
<div class="pagination">Showing 1 to 5 of 8 | <b>1</b> <a onclick="$.post('http://localhost:8080/ajax/index.php/posts/ajaxPaginationData/5', {'page' : 5}, function(data){
$('#postList').html(data); }); return false;" href="javascript:void(0);">2</a> <a onclick="$.post('http://localhost:8080/ajax/index.php/posts/ajaxPaginationData/5', {'page' : 5}, function(data){
$('#postList').html(data); }); return false;" href="javascript:void(0);">></a> </div>
Now On Change of country dropdown I want to get list with that Id
<select id="country">
<option value="0">select</option>
<option value="1">USA</option>
<option value="2">UK</option>
</select>
Pass the value of your dropdown in function
function ajaxPaginationData($country)
{
$page = $this->input->post('page');
if(!$page){
$offset = 0;
}else{
$offset = $page;
}
$totalRec = count($this->post->getRows());
//pagination configuration
$config['first_link'] = 'First';
$config['div'] = 'postList'; //parent div tag id
$config['base_url'] = base_url().'index.php/posts/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
$data['posts'] = $this->post->getRows(array('start'=>$offset,'limit'=>$this->perPage));
$this->load->view('posts/ajax-pagination-data', $data, false);
}
And use $country to select the data from table with where condition.
for more on pagination try this http://w3code.in/2015/10/how-to-do-pagination-in-codeigniter/
Related
I am having the pagination kept in my codeigniter project. The links are all shown in the view page. But whichever link that I clicked is getting me to the same page...
The following is my controller code..
public function newsletter()
{
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url() . "index.php/welcome/newsletter";
$this->load->model('newsletter_model');
$total_row = $this->newsletter_model->record_count();
$config["total_rows"] = $total_row;
$config["per_page"] = 1;
$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);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
}
$this->load->model('newsletter_model');
$data["results"] = $this->newsletter_model->fetch_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$this->load->model('newsletter_model');
$data['rm_newsletter'] = $this->newsletter_model->get_rm_newsletter();
$this->load->view('newsletter/newsletter',$data);
}
The following is my view code:
foreach ($lt_newsletter as $letter) {
echo $newsletter['nl_newsletter']; }
<div id="pagination">
<ul class="pagination">
<?php foreach ($links as $link)
{
echo "<li>". $link."</li>";
} ?>
</ul>
</div>
And finally in my model:
public function fetch_data($limit, $id)
{
$this->db->limit($limit);
$this->db->where('nl_id', $id);
$query = $this->db->get("ins_newsletter");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
}
}
public function record_count()
{
return $this->db->count_all("ins_newsletter");
}
public function get_rm_newsletter()
{
$data = $this->db->query('SELECT nl.* FROM ins_newsletter nl ORDER BY nl.nl_id DESC');
return $data->result_array();
}
In my browser all the links(1,2,3,4) are shown clearly, but after I click on every link(1,2,3,4), it is showing the same page with all the records. For presently, I have 4 records in my database and it lists all. Kindly point me where am i going wrong...! Thanks in Advance.
Maybe the error is in your model, I see that you have a
$this->db->where('nl_id', $id);
but in the function in the controller you pass two parameters
$data["results"] = $this->newsletter_model->fetch_data($config["per_page"], $page);
this are the pagination values, one for quantity of record,(not variable), and the other for the page you are passing (1,2,3,etc.)
So, you have to add another parameter to make the where. See this link in pagination does not work and if you have some other problem can tell me.
have a form like this. This is an update form, i just need to update qty foreach product in cart.
But i have tried to explode each results and not work... it return object2array conversion error... its the first time that i get this error How i can save this in DB?
<form action="update">
#foreach($products as $product)
<input type="text" name="products[]">
<input type="text" name="qty[]">
#endforeach
<input type="submit" calss="btn btn-primary">
This is my controller:
Route::post('aggiorna', function() {
$quantita = Input::all();
$carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get();
$quantita = explode(',', $quantita);
$i = 0;
foreach($carrelli as $carrello) {
$carrello->quantita = $quantita[$i];
$i = $i++;
}
return Redirect::to('carrello');
});
Thanks in advance.
you do the iteration for each $carreli that exist on your database, however you never save the value.
Route::post('aggiorna', function() {
$quantita = Input::all();
$carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get();
$quantita = explode(',', $quantita);
$i = 0;
foreach($carrelli as $carrello) {
$carrello->quantita = $quantita[$i];
$i = $i++;
$carrelo->save();
}
return Redirect::to('carrello');
});
add the $carrelo->save(); after you update the value in order to save it on db.
Also careful when you use Input::all();. That means that your data array contains both products and quantities. I would suggest using the following code:
Route::post('aggiorna', function() {
$quantita = Input::get('qty');
$products = Input::get('products');
$carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get();
$quantita = explode(',', $quantita);
$products = explode(',', $products);
$i = 0;
foreach($carrelli as $carrello) {
$carrello->quantita = $quantita[$i];
$carrello->products = $products[$i];
$i = $i++;
$carrelo->save();
}
return Redirect::to('carrello');
});
However since i do not know what you are trying to achieve, I posted both solutions here.
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'm new to codeigniter and i'm using codeigniter pagination by getting query result from Category Id, but the page always starts at page 2 .
how do i fix the pagination to set the start page at number page 1?
public function kategori($kat=''){
$config['base_url'] = base_url().'index.php/lowongan/kategori/'.$kat;
$data['db']=$this->query->katPostingan($kat);
$config['total_rows'] = $data['db']->num_rows();
$config['per_page'] = 1;
$config['uri_segment'] = 4;
$this->pagination->initialize($config);
$data2['paging'] = $this->pagination->create_links();
$page = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$data2['records'] = $this->query->katPos($kat, $config['per_page'], $page);
$this->load->view( 'user/hal_kategori',$data2);
}
and my model
public function katPos($kat='',$limit,$start){
$this->db->where("id_kat", $kat);
$this->db->limit($limit,$start);
$query = $this->db->get("showKat");
return $query;
}
public function katPostingan($kat=''){
$this->db->where("id_kat", $kat);
$query = $this->db->get("showKat");
return $query;
}
try a different way of solution,
controller(index())
$this->load->library('pagination');
$config['base_url'] = site_url().'classname/index';
$config['per_page'] = $this->config->item('pagination_number');
$config['uri_segment'] = 4;
$data['count']=$this->example_model->count();
$config['total_rows'] =$data['count'];
$data['classname']=$this->example_model->getall($this->config->item('pagination_number'),$this->uri->segment(4));
$this->pagination->initialize($config);
$data['page_link']=$this->pagination->create_links();
$this->load->view('users/classname.php',$data);
model
function getall($num,$offset)
{
$this->db->from('table_name');
$this->db->limit($num,$offset);
$query = $this->db->get();
return $query->result_array();
}
function count()
{
$this->db->from('table_name');
$query = $this->db->get();
$rowcount = $query->num_rows();
return( $rowcount);
}
View
<div class="pagination"> <?php echo $page_link;?> </div>
I am trying to display data from database using Codeigniter's table and pagination library. In my model, apart from other column I want to fetch information of column "batchid" from my table "batch" ,but don't want to show it in the view file when I am displaying the other data.
But since I have included the "batchid" in this-(in the following)
$this->db->select('batchname, class, batchid, batchinstructor');
It is showing all the information of the column "batchid" in the view, which I don't want. I just want to retrieve the value of batchid to use it for anchoring "batchname".
I have tried a lot but it won't work. Would you please kindly help me?
Thanks in Advance
Here is my model
//Function To Create All Student List
function batch_list()
{
$config['per_page'] = 15;
$this->db->select('batchname, class,batchid, batchinstructor');
$this->db->order_by("batchid", "desc");
$rows = $this->db->get('batch',$config['per_page'],$this->uri->segment(3))->result_array();
$sl = $this->uri->segment(3) + 1; // so that it begins from 1 not 0
foreach ($rows as $count => $row)
{
array_unshift($rows[$count], $sl.'.');
$sl = $sl + 1;
$rows[$count]['batchname'] = anchor('batch_list/get/'.$row['batchid'],$row['batchname']);
$rows[$count]['Edit'] = anchor('update_student/update/'.$row['batchname'],img(base_url().'/support/images/icons/edit.png'));
$rows[$count]['Delete'] = anchor('report/'.$row['batchname'],img(base_url().'/support/images/icons/cross.png'));
}
return $rows;
}
//End of Function To Create All Student List
Here is my controller
function index(){
$this->load->helper('html');
$this->load->library('pagination');
$this->load->library('table');
$this->table->set_heading('Serial Number','Batch Name','Class','Batch Instructor','Edit','Delete');
$config['base_url'] = base_url().'batchlist/index';
$config['total_rows'] = $this->db->get('batch')->num_rows();
$config['per_page'] = 15;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['tab'] = "Batch List";
$this->load->model('mod_batchlist');
$data['records']= $this->mod_batchlist->batch_list();
$data['main_content']='view_batchlist';
$this->load->view('includes/template',$data);
}
I'm not sure what you're doing in your view, but you can add rows manually by looping through your dataset:
foreach ($records->result() as $r)
{
$this->table->add_row($r->serial,
$r->batchname,
$r->class,
$r->batchinstructor,
$r->edit,
$r->delete
);
}
echo $this->table->generate();
In this way, you control what data goes to your table.