is it possible to dynamically change the url in codeigniter? - php

i have a page where books are to be display either with "grid" view or with "list" view.
So my current url is
http://localhost/thebestbookfinder.com/viewallbooks/books/pgn/grid/8
and if i click on "list" link from my view page then my current page should load with only changing this part /grid/ to /list/ and other same.
i.e http://localhost/thebestbookfinder.com/viewallbooks/books/pgn/list/8
I tried this but this is not helpful. Please help me to solve this issue.
inMyViewPage.php
<td width="10%" style="font-size:12px;color: red;">
<?php
echo "".anchor('#', 'Grid', array('id' => ''))."";
echo "".anchor(base_url().'viewallbooks/books/pgn/'.$this->uri->segment(4),' List', array('id' => ''))."";
?>
</td>
Instead of link if form submition solve my problem then please help me with that
Controller.php
function books()
{
$config = array();
$config['base_url'] = base_url().'viewallbooks/books/pgn/'.$this->uri->segment(4);
$config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
$config['uri_segment'] = 5;
$config['per_page'] = 8;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$data["query"] = $this->Subjectmodel->get_subjects();
$data["query1"]= $this->Editionmodel->get_edition();
$data["query2"]=$this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
$viewType = ($this->uri->segment(4)) ? $this->uri->segment(4) : 'list';
if($viewType=='list')
{
$this->load->view('commonfiles/bookslistview',$data);
}
else {
$this->load->view('commonfiles/booksgridview',$data);
}
}

Need to add route in route.php like this
$route['viewallbooks/books/(:any)'] = "viewallbooks/books/$1";
Function books() shold contain 3 params with values by default
Below is more correct varian
function books($img_type = 'png', $view_type = 'list', $page = 0) {
$config = array();
$config['base_url'] = base_url().'viewallbooks/books/'.$img_type.'/'.$view_type.'/'.$page;
$config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
$config['uri_segment'] = 5;
$config['per_page'] = 8;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data["query"] = $this->Subjectmodel->get_subjects();
$data["query1"]= $this->Editionmodel->get_edition();
$data["query2"]= $this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
if($viewType=='list')
$this->load->view('commonfiles/bookslistview',$data);
else
$this->load->view('commonfiles/booksgridview',$data);
}

Do this one:
<?php echo "".anchor(base_url().'viewallbooks/books/pgn/grid/'.$this->uri->segment(5),' Grid', array('id' => ''))."";
echo "".anchor(base_url().'viewallbooks/books/pgn/list/'.$this->uri->segment(5),' List', array('id' => '')).""; ?>
And no need to change anything in above controller.

You will have to do either:
Reload the page with the new url (grid) to render that template
OR:
Render both the grid and list view and then dynamically (with some css/javascript) hide content based on what you'll want to see.
OR:
Load the grid-view with AJAX and replacing the list with it.

Related

Codeigniter pagination giving 404 error

Could you help me with my problem about pagination in Codeigniter? I have a view that lists books. I wrote some code and now I see the buttons but the content is not limited as I wished. Can't find what is wrong with the code.
That is the updated code:
Controller:
public function index($offset=0){
$config['base_url'] = 'http://localhost/myLibrary/books/index';
//$config['total_rows'] = 200;
$config['total_rows'] = $this->db->get('books')->num_rows();
$config['per_page'] = 1;
$config['uri_segment']= 3;
$config['attributes'] = array('class' => 'pagination-link');
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
$book_list = $this->Books_model->list_books();
$genre_list= $this->Books_model->list_genres();
$author_list= $this->Books_model->list_authors();
$view_data = array(
"book_list" => $book_list,
"genre_list" => $genre_list,
"author_list" => $author_list
);
$this->db->get('books', $config['per_page'], $this->uri->segment(3));
$start = isset($_GET['start']) ? $_GET['start'] : 0;
$book_list = $this->Books_model->list_books($start, $config['per_page']);
$this->load->view("book_list",$view_data);
$this->load->library('pagination');
$this->load->library('table');
}
Model:
public function list_books($limit = FALSE, $offset = FALSE){
if($limit)
{
$this->db->limit($offset, $limit);
}
$list=$this->db->get("books")->result();
return $list;
}
View:
<?php echo $this->pagination->create_links(); ?>
I'd be so happy if you could help and thanks in advance
You can't use base_url() function as a base_url for pagination unless this index function belongs to your default site controller.
Instead change your line to this:
$config['base_url'] = site_url('controller/method/');
Also the reason your data isn't limited is because you're sending start parameter as false to model
It should be:
$data['records'] = $this->Books_model->list_books($this->uri->segment(3), $config['per_page'], $offset);
By looking at your code, the list of books being sent to the view file is stored in the $book_list variable, so in order to limit the number of books returned alter your line to this:
$start = isset($_GET['start']) ? $_GET['start'] : 0;
$book_list = $this->Books_model->list_books($start, $config['per_page']);
And add this you your pagination config array:
$config['page_query_string'] = TRUE;
And in your model function change this line:
$this->db->limit($limit, $offset);
To:
$this->db->limit($offset, $limit);
as stated in the CI documentation
$this->db->limit(10, 20); // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
Check here for more reference CI Limiting
Also as a side note, by looking at your code the $query and $data['records'] variables are assigned values but are never used or passed to the view file, so they don't do anything
This is how an index method in your controller should look like now:
Controller:
public function index($offset=0){
$config['base_url'] = site_url('books/index');
//$config['total_rows'] = 200;
$config['total_rows'] = $this->db->get('books')->num_rows();
$config['per_page'] = 1;
$config['uri_segment']= 3;
$config['attributes'] = array('class' => 'pagination-link');
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
$start = isset($_GET['start']) ? $_GET['start'] : 0;
$book_list = $this->Books_model->list_books($start, $config['per_page']);
$genre_list= $this->Books_model->list_genres();
$author_list= $this->Books_model->list_authors();
$view_data = array(
"book_list" => $book_list,
"genre_list" => $genre_list,
"author_list" => $author_list
);
$this->load->view("book_list",$view_data);
$this->load->library('pagination');
$this->load->library('table');
}
Model:
public function list_books($limit = FALSE, $offset = FALSE){
if($limit !== FALSE)
{
$this->db->limit($offset, $limit);
}
$list=$this->db->get("books")->result();
return $list;
}
$config['base_url'] = 'http://example.com/controller/index/page/'
Try to add index as method name config['base_url]. so that whenever your pagination url created it contains method name.
this happens due to index method call automatically i.e. we don't need to mention method name but in pagination url we have to pass offset parameter we must have to use full url.

Codeigniter Pagination first page is always current page

I am pulling my hair out on this one. I am using CodeIgniters pagination library and right now it is always stuck on page 1 as the current page. I have checked a bunch of StackOverflow questions and I don't have the same problem as anyone else.
Here is my url structure
website.com/leaders/page/[page_number]
Here is the pagination code in my controller
$this->load->library('pagination');
$config['per_page'] = $query_config['limit'];
$config['base_url'] = base_url() . 'leaders/page/';
$config['total_rows'] = 2000; // I actually use a function for this number
$config['full_tag_open'] = '<div id="paginate">';
$config['full_tag_close'] = '</div>';
$config['first_link'] = '« First';
$config['last_link'] = 'Last »';
$config['use_page_numbers'] = true;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
When I echo the pagination in the view it looks like it works. The urls on each link is correct and everything looks fine. The last link shows the last page url and the current page is 1. However when I click on page 2 or any other page from the pagination, it still shows page 1 as the current page even though the url is as follows
website.com/leaders/page/2
I used the $this->uri->segment(3) to grab the page number for my database queries so the page number is in the right segment. Just to double check I set the $config['uri_segment'] values to 1,2,3,4,5,6 just to make sure.
I found out the problem while writing this but I am still confused
Then I thought maybe there is something going on with the url itself as I have a route directing it to the index method in the controller. Here is what my routes file looks like
routes.php
$route['leaders/page/(:num)'] = 'leaders/index';
$route['leaders/page'] = 'leaders/index';
Then I tried setting the base_url for the pagination config to send it to the index directly like so:
$config['base_url'] = base_url . 'leaders/index';
Now it seems to be working properly. But how do I make it so that it works with the url structure I had before? I just think it looks nicer and I don't really need a method in the controller for this. Is there something conflicting in my routes.php file?
Thanks
define cur_page and define controller like :
public function index($page=''){
//...
$page = ($page!='')? $page : 0;
$config["cur_page"] = $page;
//...
}
Use this in your code, hope this will work-
if ($this->uri->segment(3) > 0) {
$offset = $this->uri->segment(3) * $config['per_page'] - $config['per_page'];
} else {
$offset = $this->uri->segment(3);
}
I've been working for about 6 hours to make CI pagination work as I expected, and I don't know if is the order of the config elements or just my browser joking with me.
Below is my configuration array for pagination to work properly.
As you can see this is normal code, but my problem was, when I rendered for the first time, my pagination view, all seems to be ok, but if $config['per_page'] = 10; was set to 10, when I clicked on 11 link of pagination links, link number 2 showed Cseguimiento/buscar_reportes/# and looks like current page was 2 not 11.
I was very tired and I started to change the order of $config array, and suddenly it worked. SO I preesnt it here.
$config['base_url'] = base_url().'Cseguimiento/buscar_reportes/';
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$config['next_link'] = '>';
$config['prev_link'] = '<';
$config["full_tag_open"] = '<ul class="pagination">';
$config["full_tag_close"] = '</ul>';
$config["first_tag_open"] = '<li>';
$config["first_tag_close"] = '</li>';
$config["last_tag_open"] = '<li>';
$config["last_tag_close"] = '</li>';
$config["next_tag_open"] = '<li>';
$config["next_tag_close"] = '</li>';
$config["prev_tag_open"] = "<li>";
$config["prev_tag_close"] = "</li>";
$config["cur_tag_open"] = "<li>";
$config["cur_tag_close"] = "</li>";
$config["num_tag_open"] = "<li>";
$config["num_tag_close"] = "</li>";
$config['total_rows'] = $this->mseguimiento->filas($fecha_inicio,$fecha_fin);
$config['per_page'] = 10;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$page = $config['uri_segment'] * $config['per_page'];
$this->pagination->initialize($config);
$offset = ($this->uri->segment(3)-1)*$config['per_page'];
$output = array(
'pagination_link' => $this->pagination->create_links(),
'lista_reportes' => $this->mseguimiento->fetch_details($this->pagination->per_page, $offset,$fecha_inicio,$fecha_fin)
);

Pagination codeigniter not working

pagination is working but i dont know Why its showing only one data per page?? and i hv 20 data in my db and i can see only 6 data in 6pages :(
This is my controller
function view($page=0){
$config = array();
$config["base_url"] = base_url() . "index.php/view_expenses/view";
$config["total_rows"] = $this->emp_expenses_model->getTotalExpensesCount();
$config["per_page"] =1;
$this->pagination->initialize($config);
$this->data["results"] = $this->emp_expenses_model->getExpenses($config["per_page"], $page);
$this->data["links"] = $this->pagination->create_links();
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('view_expenses', $this->data);
}
This is my model
function getTotalExpensesCount() {
return $this->db->count_all("emp_expenses");
}
function getExpenses($limit, $start) {
$this->db->limit($limit, $start);
$qry= $this->db->get("emp_expenses");
return $qry->result();
}
Any help thanks in advance :D
Try this:
function view(){
$config = array();
$config["base_url"] = base_url() . "index.php/view_expenses/view/".$this->uri->segment(3);
$config["total_rows"] = $this->emp_expenses_model->getTotalExpensesCount();
$config["per_page"] = 5;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$this->data["results"] = $this->emp_expenses_model->getExpenses($config["per_page"], $this->uri->segment(3));
$this->data["links"] = $this->pagination->create_links();
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('view_expenses', $this->data);
}
function getExpenses($limit, $start = 0) {
$qry= $this->db->get("emp_expenses", $limit, $start);
return $qry->result();
}
$config["per_page"] = 5; means that you want to show 5 data per page, $config['uri_segment'] = 3; says what segment will hold the offset which you will be using in your query. $config["total_rows"] defines the total no. of rows in your table. $config["base_url"] defines the url the pagination will hold.
The data you are showing in your view is no way related to the pagination. Check your query and the offset you are getting. E.g. place echo $this->db->last_qeury();die; in function getExpenses() just before return statement.
You need to pass the uri segment like
$config["uri_segment"] = $last_seg_no;
Here $last_seg_no will be the last segment where you can find the page number.And need to pass the per page param also like
$config["per_page"] = 1;//As per your case
"dont know Why its showing only one data per page??" because of $config["per_page"] =1;.
Add the following,
$config['uri_segment'] = 3; // change based on ur url
Change the value from $config["per_page"] =1; to $config["per_page"] =3; to display 3 data per page.
Try this
$config['uri_segment'] = 3;
$config["per_page"] = 3;
Modifiy modal function like this
function getTotalExpensesCount() {
$this->db->select("count(*) as CNT");
$qry = $this->db->get("emp_expenses");
$result2 = $qry->row()->CNT;
return $result2;
}
i have faced this same problem. Now it solved by changing this
$config['uri_segment']

pagination link disappear on second page-codeigniter

Hey, guys. I have a problem with pagination in my Codeigniter project. I am using the pagination library, and I have a search form. When I search for something, the results display on the page. When the resulting rows are more than the limit, I show the pagination links. But when I click on link number 2 to go to second page, the pagination links disappear. My search code is given below...
Controller function:
function search_branch()
{
$this->load->library('pagination');
$search_this=$this->input->post('inputsearchbranch');
$limit = 20;
$data['fields'] = array(
'branch_name' => 'Branch Name',
'city'=>'City',
'district'=>'District',
'tahsil'=>'Tahsil',
'Branch_manager'=>'Branch Manager'
);
$this->load->model('mod_user');
$searchbranch=$this->mod_user->search_branch($search_this,$limit);
$data['searchbranch'] = $searchbranch;
$config = array();
$config['base_url'] = site_url("ctl_home/search_branch/");
$config['total_rows'] = count($searchbranch);
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('view_searchbranch',$data);
}
My view:
<center>
<form class="userinfo" action="" method="post">
//table in which search record display
</form>
</center>
<?php if (strlen($pagination)): ?>
<div class="pagination">
<?php echo $pagination; ?>
</div>
<?php endif; ?>
Try to changing the "$config['uri_segment'] = 3;" to $config['uri_segment'] = 5;
And you are count the total count with the limited record. The total count value will be counted with all records.
function search_branch()
{
$this->load->library('pagination');
$search_this=$this->input->post('inputsearchbranch');
$limit = 20;
$data['fields'] = array(
'branch_name' => 'Branch Name',
'city'=>'City',
'district'=>'District',
'tahsil'=>'Tahsil',
'Branch_manager'=>'Branch Manager'
);
$this->load->model('mod_user');
$searchbranch=$this->mod_user->search_branch($search_this,false);
$config['total_rows'] = count($searchbranch);
$searchbranch=$this->mod_user->search_branch($search_this,$limit);
$data['searchbranch'] = $searchbranch;
$config = array();
$config['base_url'] = site_url("ctl_home/search_branch/");
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('view_searchbranch',$data);
}
& also use
$offset = $this->uri->segment(4);
Add new parameter to search_branch function and pass that to db..
$searchbranch=$this->mod_user->search_branch($search_this,$limit,$offset);
example Function($where,$limit,$offset)
$this->db->where($where);
$query = $this->db->get('mytable', $limit, $offset);

CodeIgniter Paging class, active link not working

I am using the paging class of the codeigniter for the first time. Everything else is working fine, but whichever page I am going, the active link is always the [1] and it doesn't change. Also the next button is always linked to the first page. Culdn't figure out why ! Please help !
Controller
public function unverified_images()
{
$data['title'] = 'Choose from the below Images to verify them >>';
$data['total_rows'] = $this->admin_model->all_unverified_images();
$config['base_url'] = base_url().'index.php/admin/admin/unverified_images';
$config['total_rows'] = $data['total_rows']->num_rows();
$config['per_page'] = 1;
$config['num_links'] = 8;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['query'] = $this->db->where('image_status', 0)
->get('tbl_img', $config['per_page'], $this->uri->segment(4));
$data['links']=$this->pagination->create_links();
$this->load->view('admin/image_verify', $data);
}
public function verify_image()
{
$data['title'] = 'Choose from the below Images to verify them >>';
$data['msg'] = $this->admin_model->verify_image();
$data['query'] = $this->admin_model->all_unverified_images();
redirect('admin/admin/unverified_images');
}
Model
function all_unverified_images()
{
$this->db->where('image_status', 0);
$query = $this->db->get('tbl_img');
return $query;
}
View
<?php echo $links; ?>
Sound like that CI can't figure out what page you supposed to be on.
Try adding the uri_segment or the cur_page for the $config array.
The uri_segment should tell CI what part of the url holds the current page number, from your example code it seem to be 4 (from the query) while CI's default is 3.

Categories