Codeigniter : pagination with query string is not working properly - codeigniter

I am using query string pagination. I have integrated this successfully. There are some issue with this.
when i click on second page first time it reloads the page but doesn't append ?per_page to the URL, it does appends like this controller/method/?2
when i click on second page second time it does the correct behavior but URL becomes like this controller/method/parameter/?0=&per_page=2
but the URL should be if there are not other get parameter
controller/method/parameter/?per_page=2
how to get this issue fixed ? i have enabled query string like this
$config['page_query_string'] = true;
$config['reuse_query_string'] = true;
Can anyone suggest what is the wrong in this implementation ? My base url is
$config['base_url'] = base_url().'controller/method/parameter/';
Basically it is not adding per_page query string if there is no query string already there in URL. How can i fix that ?

$config["total_rows"] = 200;
$config["per_page"] = 10;
$config['num_links'] = 4;
$config['page_query_string']= TRUE;
$config['use_page_numbers'] = TRUE;
$config["base_url"] = base_url().'controller/method/';
//Change uri segment according your url
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$links = $this->pagination->create_links();

Related

How to redirect to another page after submitting a form

How to redirect to another page after submitting a form with my controller. the form is working fine but when i use the code like this i get a blank page and if i use
return view('leave.signUp');
it works but still remains in the page of the form and i want it to be redirected to another page.
public function index(Request $request)
{
if($request->isMethod('POST')){
$employee = new Employee;
$employee->lname = $request->input('lname');
$employee->fname = $request->input('fname');
$employee->oname = $request->input('oname');
$employee->employee_id = $request->input('staffId');
$employee->college = $request->input('college');
//$employee->salary_level = $request->input('salaryGradeLevel');
$employee->employee_type = $request->input('staffType');
$employee->employee_category= $request->input('staffCategory');
$employee->date_of_first_appointment= $request->input('dateOfFirstAppointment');
$employee->date_of_last_appointment= $request->input('dateOfLastAppointment');
$employee->country = $request->input('country');
$employee->gender = $request->input('gender');
$employee->title = $request->input('title');
$employee->marital_status = $request->input('maritalStatus');
$employee->email = $request->input('email');
$employee->phone_number = $request->input('phoneNumber');
$employee->date_of_birth = $request->input('dateOfBirth');
$employee->place_of_birth = $request->input('placeOfBirth');
$employee->religion = $request->input('religion');
$employee->origin = $request->input('stateOfOrigin');
$employee->local_government_area = $request->input('localGovernmentArea');
$employee->address = $request->input('address');
$employee->permanent_address = $request->input('permanentAddress');
$employee->extra_curicullar_activities = $request->input('extraCuricularActivities');
$employee->save();
$nextOfKin = new NextOfKin;
$nextOfKin->employee_id = $employee->employee_id;
$nextOfKin->title = $request->input('Ntitle');
$nextOfKin->surname = $request->input('Nsurname');
$nextOfKin->firstname = $request->input('Nfirstname');
$nextOfKin->othername = $request->input('Nothername');
$nextOfKin->relationship = $request->input('Nrelationship');
$nextOfKin->email = $request->input('Nemail');
$nextOfKin->phone_number = $request->input('NphoneNumber');
$nextOfKin->contact_address = $request->input('Naddress');
$nextOfKin->save();
return redirect(route('application'));
}
}
this is my web.php
Route::match(['post','get'],'application', 'LeavessController#index');
Route::match(['post','get'],'signUp', 'signUpsController#index');
Route::get('approved', 'LeavesController#getLeaveApproved');
Route::match(['post','get'], 'leaveType', 'LeavesController#getleaveType');
Route::match(['post','get'], 'leaveDepartment', 'LeavesController#getleaveDepartment');
Route::get('allLeave', 'LeavesController#getallLeave');
Route::get('leaveViewDetails', 'LeavesController#getleaveViewDetails');
Route::get('/get-leave-days/{leaveType}', 'LeavesController#getLeaveDays');
return redirect()->route('application');
The param for route() is what you defined in your routes with name(...)
https://laravel.com/docs/7.x/redirects#redirecting-named-routes
Besides that, I recommend you to read the laravel docs about Controllers: https://laravel.com/docs/7.x/controllers#resource-controllers
The index is only for returning the database entries. For inserting data into the database use store .
To redirect to a "simple" URL use:
return redirect('home/dashboard');
(replace 'home/dashboard' with the actual URL you want to redirect to)
To redirect to a named route use:
return redirect()->route('login');
(where login is the name of your route)
https://laravel.com/docs/7.x/responses#redirects

SESSION varibales are not displayed after redirection used with memcache

I have a page session_config.php in that page I have following code
session_name('session_sw');
session_start();
$session_ID = session_id();
I have another page index.php in which I am including session_config.php ,the code as follows.
$email = $_REQUEST['email'];
$pass = $_REQUEST['pass'];
$error = 0;
if(!empty($email) && !empty($pass)) {
$url = "$SERVER/sw/apis/3rdpartylogin.php?advertiser_login=1&pass=$pass&email=$email";
$resp = file_get_contents($url);// it sends json encoded string.
$resp = json_decode($resp);
if ( $resp->status == 'success' ) {
if ( !isset($resp->mesg->profile) || $resp->mesg->profile == 0 ){
$error = '1';
} else {
include "session_config.php";
$_SESSION['user_id'] = $resp->mesg->user_id;
$_SESSION['customer_id'] = $resp->mesg->customer_id;
$_SESSION['customer_name'] = $resp->mesg->customer_name;
$_SESSION['logged_in_user_email'] = $_REQUEST['email'];
$_SESSION['agency_name'] = $resp->mesg->agency_name;
$_SESSION['profile'] = $resp->mesg->profile;
$_SESSION['metadata'] = $resp->mesg->metadata;
$_SESSION['show_archival'] = $resp->mesg->show_archival;
$_SESSION['show_live'] = $resp->mesg->show_live;
$_SESSION['show_sov'] = $resp->mesg->show_sov;
$_SESSION[$session_ID]['session_name'] = 'surewaves_agency_view';
header("Location: reach.php");exit;
}
} else {
$error = '1';
}
}
?>
// Here I am able fetch the correct data in '$resp' variable and also it is redirecting to reach.php
In reach.php , i have following code
<?php
include "session_config.php";
echo "<pre>";
print_r($_SESSION);exit;
?>
But in reach.php when I am fetching the session variables the session array is showing completely empty like this Array (). WHY?
I am using memcache for it and we are using ubuntu.
In My php.ini i have included following code..
extension = memcache.so;
session.save_handler = memcache
session.save_path = "http://localhost:11211"
I have also tried with "tcp ://localhost:11211" but nothing worked.
Try to remove the http:// part from session.save_path and replace localhost with 127.0.0.1:
session.save_path = "127.0.0.1:11211"
Take a look at the second example at http://br1.php.net/manual/en/memcache.examples-overview.php.

codeigniter pagination total_rows dynamically

here is my code and my problem in comment line.
public function sonuc()
{
$config['total_rows'] = 5 //i want to set here count of result instead of digit. count($data['sonuc']) does not work.. what will i do?
$config['per_page'] = 2;
$data['sonuc'] = $this->emlak_model->arama(
$config['per_page'],
$this->uri->segment(3,0),
$this->input->post('semt'),
);
}
if i do it manually with digit, pagination will work, but i want to get total_row of query automatically, what will i do? thanks.
If you want a fresh count of your field on each page load,you have to count all your fields with a query count:
$total_rows = $this->db->count_all_results('mytable');
check the Codeigniter Doc
so according to your code:
public function sonuc()
{
$config['total_rows'] = $this->db->count_all_results('mytable'); //put your table name here
$config['per_page'] = 2;
$data['sonuc'] = $this->emlak_model->arama(
$config['per_page'],
$this->uri->segment(3,0),
$this->input->post('semt'),
);
}

ASP MVC Redirect using Ajax

I am having trouble with my redirects picking up the existing url. I have a page that has google charts on it where I have added a listener to the bars in a bar chart. When a user clicks on one of the bar charts a case statement determines what id to pass to the router.
google.visualization.events.addListener(wrapper, 'select', selectHandler);
function selectHandler() {
var selection = wrapper.getChart().getSelection()
var monthsTransform = 0;
switch(selection[0].column)
{
case 1:
monthsTransform = 0;
break;
case 2:
monthsTransform = 3;
break;
case 3:
monthsTransform = 6;
break;
case 4:
monthsTransform = 12;
break;
case 5:
monthsTransform = 18;
break;
//no need to default as already set to 0!
}
var url = "newController/newPage/" + monthsTransform;
window.location.href = url;
}
The problem I have is that the
window.location.href
is appending the newController/newPage to the existing url, so it ends up like:
localhost:1234/oldController/oldPage/newController/newPage/id
Use window.location.assign("/newController/newPage/" + monthsTransform), instead of window.location.href
It would even be nicer if you added the server name and protocol in front of the new URL, and used Html.Action html helper to generate the url.
Check my blog post on creating URL's on the server side:
http://ilovedevelopment.blogspot.co.uk/2012/04/aspnet-mvc-3-generating-actionlink-in.html
The crux of it I've pasted below for brevity - modify as required:
string absoluteAction = string.Format("{0}://{1}{2}",
Url.RequestContext.HttpContext.Request.Url.Scheme,
Url.RequestContext.HttpContext.Request.Url.Authority,
Url.Action("ActionName", "ControllerName", new {Id = "Data"}));
This can be done inside a controller method so you could make an ajax call and return the proper url "absoluteAction" as a Json result.

Why does a form submit only refresh the page when I turn on codeigniter caching?

probably a noob question but I am not sure what is going on. Here is the function from the controller...
function product ($product_id = NULL)
{
if ($this->input->post())
{
$pcs = array();
$pcs[] = $this->input->post('product_id');
$pcs[] = $this->input->post('styles');
$build = implode('-', $pcs);
redirect('seatcovers/configure/'.$build);
}
elseif ($this->_checkID('id','products',$product_id))
{
$data['product'] = $this->model_products->getProductRow($product_id);
$data['styles'] = $this->model_products->getStyles($product_id);
$data['images'] = $this->model_products->getImages($product_id);
$tags['title'] = 'title';
$this->load->view($this->session->userdata('language').'/includes/view_header',$tags);
$this->load->view($this->session->userdata('language').'/products/view_product',$data);
$this->load->view($this->session->userdata('language').'/includes/view_footer');
}
else {
redirect('seatcovers');
}
}
Not sure what I'm doing wrong? Does caching not work when their is a form?
Yes, caching is done on URI level means, when a URL is cached in Codeigniter then it will display the cached page and will not execute your controller.

Resources