codeigniter pagination link is not working when i select the link - php

Request yo to help in pagination links .
In my database i have 3 records i want to display single record per page. When I select the next numeric of pagination link, data is not being fetched.Thing is that when I click on number 2 of pagination link, echo var_dump() shows Result is empty and I am not getting any values for echo $data->email.But for the first time when i search i am able to display single record, problem is only with next link of pagination So what might be the error? I'm not able to get an answer,and I'm not sure what happens, so I am posting my code below please go through it and and help me.
Request you to help me.
**HERE STARTS MY CONTROLLER**
public function users($limit=1,$offset = 0)
{
$this->load->helper('url');
$data = array();
$look = $this->input->post('look');
$age = $this->input->post('age');
$age_from = $this->input->post('age_from');
$age_to = $this->input->post('age_to');
$se_ct = $this->input->post('sect');
$subsect = $this->input->post('subsect');
$coun_try = $this->input->post('country');
$sta_te = $this->input->post('state');
$ci_ty = $this->input->post('city');
$qualification = $this->input->post('qualification');
$results = $this->searchresultss->login($look, $age, $age_to, $age_from, $se_ct, $subsect, $coun_try, $sta_te, $ci_ty, $qualification);
$this->load->helper('url');
$config = array();
$config['base_url'] = base_url().'searchresult/users';
$config['total_rows'] = count($results);
$config['per_page'] = $limit;
$this->load->library('pagination', $config);
$data['pagination_links'] = $this->pagination->create_links();
$data['results'] = array_slice($results, $offset, $limit);
$this->load->view('searchresult', $data);
$this->load->view('includes/khelp');
$this->load->view('includes/kfooter');
**HERE STARTS MY MODEL PAGE**
Class Searchresultss extends CI_Model
{
public function login($look, $age, $age_to, $age_from, $se_ct, $subsect, $coun_try, $sta_te, $ci_ty, $qualification)
{
return $this->db->query("SELECT *
FROM users
WHERE gender = '$look'
And status='1'")->result();
}
}
**HERE START MY VIEW PAGE**
echo var_dump($_POST);
if (empty($results)) {
echo 'Results set is empty';
} else
{
foreach ($results as $data) {
echo $data->email.'<br />';
}
}
echo $pagination_links;

The problem lies in the fact that the pagination links do not include the POST variables (as well as the fact that hyper links are requested via GET).
I recommend you do a var_dump() on $_GET and $_POST and the problem will become more obvious.
A possible solution would be to include the post variables as url parameters. So for example
$config['base_url'] = base_url().'searchresult/users/look_param/age_param/etcetc';
However you would need to add functionality to handle the above.

Related

Advanced search Using Pagination in codeigniator

I want to set all the advanced search parameter using session how to set all the parameter at time.
I am using following function but it only set one parameter at time how to set all the parameter at time
public function searchterm_handler($searchterm)
{
if($searchterm)
{
$this->session->set_userdata('searchterm', $searchterm);
return $searchterm;
}
elseif($this->session->userdata('searchterm'))
{
$searchterm = $this->session->userdata('searchterm');
return $searchterm;
}
else
{
$searchterm ="";
return $searchterm;
} }
Method one (recommended)
So for pagination in CodeIgniter, you have 3 main variables you must set and a configuration method to call. You also have a library you must load.
The library is $this->load->library('pagination');
The 3 variables and configuration look like this:
//This next line is used mainly so the page number links on your pagination work.
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = $NumberOfRecords;
$config['per_page'] = 20;
$this->pagination->initialize($config);
If you are using MVC then this is quite simple. You would use the above code in your controller, grab the data you want to display starting at the nth row, where n is the page number * $config['per_page'], and ending at ((page number * $config['per_page']) + $config['per_page'])-1.
After getting the necessary data you would return that and the link code to your view. The link code is $this->pagination->create_links();
So your return might look something like this:
$data["results"] = $this->MyModel->MySqlMethod($config["per_page"], $CurrentPage);
$data["links"] = $this->pagination->create_links();
Then in your view you would loop through the $data["results"] and after the loop you would display the $data["links"]
This would give you your data displayed then the pagination at the bottom would look something like
So your controller all together should look like:
$config['base_url'] = 'http://example.com/index.php/controllerName/ViewName/';
$config['total_rows'] = $NumberOfRecords;
$config['per_page'] = 20;
$this->pagination->initialize($config);
$data["results"] = $this->MyModel->MySqlMethod($config["per_page"], $CurrentPage);
$data["links"] = $this->pagination->create_links();
return $this->load->view("ViewName", $data);
Method Two (NOT recommended)
Now you mentioned something about storing that data in Session Variables. I mean if you want you can do this. If you are going to use that method, then that tell you are not using MVC. CodeIgniter is meant for MVC. If you are not using MVC then you probably do not need CodeIgniter. If you are comfortable using CodeIgniter and do not want to try and implement the MVC, by all means go ahead.
To do the CodeIgniter Pagination in this method, you would change your public searchterm_handler($searchterm) function. The thing with session variables is that they are stored on the users browser so that way you, the programmer, can access them anywhere on your site without having to return and pass them from class to class or method to method. If you set a session variable then you return it, that is redundent and unnecessary.
You don't really need this method, it is unnecessary, but you could do something like this:
public function searchterm_handler($searchterm) {
$result = mysqli_query("SELECT count(*) FROM User_info");
$row = mysqli_fetch_row($result);
$TotalDataCount = $row[0];
$this->session->set_userdata("TotalDataCount", $TotalDataCount);
$this->session->set_userdata("RecordsPerPage", 20);
$this->session->set_userdata("BaseURL", www.example.com/link/to/your/page.php);
$this->pagination->initialize($config);
if($searchterm) {
$this->session->set_userdata('searchterm', $searchterm);
//Unnecessary
//return $searchterm;
} else {
$this->session->set_userdata('searchterm', "");
//return $searchterm;
}
}
Then in the code that called searchterm_handler($searchterm), you would do this:
searchterm_handler($input);
$searchterm = $this->session->userdata('searchterm');
$dataToReturn = array();
if($searchterm!="") {
$result = mysqli_query("SELECT * FROM table WHERE field LIKE '%$this->session->userdata('searchterm')%'");
if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
echo $this->pagination->create_links();
LET ME WORN YOU
This second method, is gross and ugly and yucky and very badly written. There is no real good way to write what you want to write. The purpose of using CodeIgniter is for MVC and built in CodeIgniter functionality, which you lose almost all of it when you get rid of MVC.
I know there is a chance I misunderstood what you are trying to do, but this was my best guess. My best advice for you is to use MVC in CodeIgniter.
Here are some sources that may help you if you use the first method:
https://www.sitepoint.com/pagination-with-codeigniter/
https://www.codeigniter.com/userguide3/libraries/pagination.html
I hope this helps, I spent a lot of time writing it...
Update - Method 3
I tried looking at your question again and maybe this will help
public function searchterm_handler($searchterm)
{
if($searchterm && $this->session->userdata('email'))
{ //user logged in
$this->session->set_userdata('searchterm', $searchterm);
$array = array(
"searchterm" => $searchterm,
"email" => $this->session->userdata('email'),
"username" => $this->session->userdata('username')
);
return $array;
}
else if($searchterm && !$this->session->userdata('searchterm'))
{ //user not logged in
$this->session->set_userdata('searchterm', $searchterm);
return $searchterm;
}
elseif($this->session->userdata('searchterm') && $this->session->userdata('searchterm'))
{ //user logged in
$searchterm = $this->session->userdata('searchterm');
$array = array(
"searchterm" => $searchterm,
"email" => $this->session->userdata('email'),
"username" => $this->session->userdata('username')
);
return $array;
}
elseif($this->session->userdata('searchterm') && !$this->session->userdata('searchterm'))
{ //user not logged in
$searchterm = $this->session->userdata('searchterm');
return $searchterm;
}
else
{
$searchterm ="";
return $searchterm;
} }
sorry if this is may, I did it on my phone

Creation of table with images CodeIgniter

The table displays only text, where images column should display actual image instead of just the name of image.
Everything else is fine and works perfectly but how to display the pictures?
controller code:
function getData()
{
//check if the user is already logged in
if($this->session->userdata('logged_in'))
{
//the user is already logged in -> display the secret content
redirect('logged');
//get the session data
$session_data = $this->session->userdata('logged_in');
//get the username from the session and put it in $data
$data['user'] = $session_data['username'];
$config['base_url'] = site_url('LittleController/getData/');
$config['total_rows'] = $this->littlemodel->record_count('products');
$config['per_page'] = 10;
$this->pagination->initialize($config);
$data['Products'] = $this->littlemodel->getData(10, $this->uri->segment(3));
foreach ($data['Products'] as &$row)
{
$img = $row['image'];
$row['image']= "<img src='resources/images/thumbs/.$img'>";
//img('resources/images/thumbs/'.$img);
}
//$data["links"] = $this->pagination->create_links();
$this->load->view('mainpage1', $data);
}
else
{
//user isn't logged in -> display login form
$data['user'] = "Guest";
$config['base_url'] = site_url('LittleController/getData/');
$config['total_rows'] = $this->littlemodel->record_count('products');
$config['per_page'] = 10;
$this->pagination->initialize($config);
$data['Products'] = $this->littlemodel->getData(10, $this->uri->segment(3));
$data["links"] = $this->pagination->create_links();
$this->load->view('mainpage1', $data);
}
}
model code:
public function getData($limit, $offset)
{
$this->db->limit($limit, $offset);
$this->db->select('productName');
$this->db->select('productLine');
$this->db->select('productScale');
$this->db->select('productDescription');
$this->db->select('buyPrice');
$this->db->select('image');
$resultset = $this->db->get('products');
return $resultset->result_array();
}
view code:
$tmpl = array ( 'table_open' => '<table z-index="1000" id="table">' );
$this->table->set_caption("List of Products");
$this->table->set_heading('Name','Product Line','Product Scale','Product Description','Price per unit(€)','Image');
$this->table->set_template($tmpl);
echo $this->table->generate($Products);
in the controller I have to change
$row['image']= "<img src='resources/images/thumbs/.$img'>";
to
$row['image']= img(array('src'=>'resources/images/thumbs/'.$img.'', 'alt'=>'','width'=>'100px', 'height'=>'100px'));
that's sets the issue.
First, you need to find which section is executed user login section.
If login section is not loaded, there is no option to display the image the product array.
so please ensure it
If login section is loaded, it can not find the product array() because before it redirects ('logged') so please correct it and works for you

codeigniter pagination, sending parameters between functions

I have a problem with pagination and codeigniter. I have a quick_searh view from witch I am submitting the information to a index controller function and there setting the pagination and calling the quick_search method to get the data I want. It just doesnt work . I've spent more then 5 hours rewriting those methods and even starting with quick_search and then passing to index function but nothing worked, please help.
public function index(){
// search parameters config
$lawyer_name = $this->input->post('lawyer_name');
$kanzlei = $this->input->post('kanzlei');
$area_of_expertise = $this->input->post('area_of_expertise');
$post_code = $this->input->post('post_code');
$city = $this->input->post('city');
$result = $this->quick_search(
$this->uri->segment(3),
$lawyer_name,
$kanzlei,
$area_of_expertise,
$post_code,
$city);
if(isset($result)){
// pagination config
$this->load->library('pagination');
$this->load->library('table');
$config['total_rows'] = count($result);
$config['base_url'] = 'http://localhost/anwalt/index.php/search/index';
$config['per_page'] = 5;
$config['num_links'] = 5;
$this->pagination->initialize($config);
$data['search_result_array'] = $result;
$data['main_content'] = 'pages/quick_search_results';
$this->load->view('templates/home_body_content', $data);
}
}
the quick_search function:
public function quick_search($offset, $lawyer_name, $kanzlei, $area_of_expertise, $post_code, $city){
// no input in the quick search
if( empty($lawyer_name) && empty($kanzlei) && empty($area_of_expertise)
&& empty($post_code) && empty($city))
{
$result = 'nothing';
} else {
$this->load->model('quick_search_model');
$result = $this->quick_search_model->get_search_results(
$offset,
$lawyer_name,
$kanzlei,
$area_of_expertise,
$post_code,
$city
);
}
return $result;
}
the sql is like this:
$sql = "SELECT users.user_id, users.canonical_name, first_name, last_name, city, phone_number, kanzlei
from users
inner join user_normal_aos
on users.user_id = user_normal_aos.user_id
inner join normal_areas_of_expertise
on user_normal_aos.normal_areas_of_expertise_id = normal_areas_of_expertise.normal_areas_of_expertise_id
where ".implode(" AND ", $where);
if(empty($offset)){
$offset = 0;
}
$sql = $sql." LIMIT ".$offset.", 4";
The data are displayed but I dont see the pagination in there .. and even when I want to change the url for segmenting it says I dont have any data.
The view is like:
<h1>Quick search results</h1>
<?php
if($search_result_array == "nothing"){
echo "<h3>You havent inputed anything</h3>";
} else {
echo $this->table->generate($search_result_array);
}
echo $this->pagination->create_links();
As per your search variables you can use this:
$lawyer_name = $this->input->post('lawyer_name');
$kanzlei = $this->input->post('kanzlei');
$area_of_expertise = $this->input->post('area_of_expertise');
$post_code = $this->input->post('post_code');
$city = $this->input->post('city');
/*pagination start*/
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/index/lawyer/'.$lawyer_name.'/kanzlei/'.$kanzlei.'/area_of_expertise/'.$area_of_expertise.'/post_code/'.$city.'/page/';
$config['total_rows'] = $this->model->count_all_results(); ###implement this function to count all the vodeos as per the search variables, just use the same function as "quick_search" but without the limit clause
$config['per_page'] = count($result);;
$config['uri_segment'] = 10;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['cur_tag_open'] = '<span class="active_page">';
$config['cur_tag_close'] = '</span>';
$this->pagination->initialize($config);
/*pagination end*/
You can not use $this->pagination->create_links(); method in view.
Use $data['pagination'] = $this->pagination->create_links(); in controller just before loading view
and echo $pagination in view
hope this will help you.

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.

In Codeigniter Pagination's generated page links, page 1 is always selected

I'm about to pull my hair over this!
On initial load of my page with pagination (by CI), all rows are displayed, even if I only want 3. On click of other pages, however, it works fine (the correct rows are displayed), but Page 1 is always "selected" (not clickable), even if I click on Page 2, 3, etc.
Any ideas?
My CONTROLLER:
function album($type, $album_id, $album_name) {
$this->load->library('pagination');
$config['base_url'] = base_url("photo_store/album/$type/$album_id/$album_name/");
$config['total_rows'] = $this->Media_model->get_photos($album_id, 'display_date DESC', NULL, NULL, TRUE);
$config['per_page'] = 3;
$this->pagination->initialize($config);
$album_photos = $this->Media_model->get_photos($album_id, 'display_date DESC', $config['per_page'], $this->uri->segment(6), FALSE);
$this->_load_view(array(
/* some other variables here */
'album_photos' => $album_photos
));
)
private function _load_view($more_data) {
$data = array_merge($more_data, array( /* some other variables here */ ));
$this->load->view('template', $data);
}
My MODEL:
public function get_photos($album_id=NULL, $order_by='display_date DESC', $limit=NULL, $offset=NULL, $count=FALSE) {
$result = array();
$query = $this->db->select('medium.*')->join('medium', "$this->item.medium_id = medium.id", 'inner')->order_by($order_by);
$limit = $limit ? $limit : '0';
$offset = $offset ? $offset : '0';
if ($limit!=='0' && $offset!=='0') {
$query->limit($limit, $offset);
}
if ($album_id) { $result = $query->get_where($this->item, array('album_id' => $album_id)); }
else { $result = $query->get($this->item); }
if ($count){ return $result->num_rows(); }
else { return $result->result(); }
}
My VIEW:
foreach ($album_photos as $photo) {
//display photos here
}
echo $this->pagination->create_links();
You can just add this to the config array so the pagination knows where to find the current page:
$config['uri_segment'] = 4;
I believe part of the problem is coming in here:
if ($limit!=='0' && $offset!=='0') {
$query->limit($limit, $offset);
}
Since you don't have an else part for your statement, the query is never limited for that first page.
I suggest you change that code to
if ($limit!=='0') {
$query->limit($limit, $offset);
}
or even just
$query->limit($limit, $offset);
since $limit should theoretically never be null or 0 because you've set it to 3. $offset, unless set, should be 0 so you could replace null with it in your model's function,
When there are more than 3 uri segments passed in the url, selected page of pagination will not be displayed correctly, it will highlight the first page all the time.
Pagination is working, but the selected page is not diplayed correctly.
To solve this, solution:
go to Pagination.php file which is located at system->libraries->Pagination.php
just simply set
var $uri_segment = 4;// or 5 or 6;
It will work.
You can just add this to the config array so the pagination knows where to find the current page:
$config['uri_segment'] = 4; // Your appropriate uri segment: 5 or 6
Try to code your controller like below:
public function index($page=''){
//...
$page = ($page!='')? $page : 0;
$config["cur_page"] = $page;
//...
}

Categories