Pass query string to same method in controller in codeigniter - php

I am Trying To Reload Contents Of Page. Through link button Filter In PHP
By Passing Some Value Through Query String To Controller Method.
The Controller Method Loads The Same Page From Where I am Passing Query String.
But The View Is Not Reloading.
But If I Call The Controller Method Some Other View It Works Fine.
here is my code
mainview.php
English
maincontroller.php
Here Is The Index Method Of Controller.
$movielanguage=$this->input->get('language');
if(!empty($movielanguage))
{
$moviedata['popularmovies']=$this->main_model-
>getmoviebylanguage($movielanguage);
}
$moviedata['allmovies']=$this->main_model->getAllMovies();
$this->load->view('home/mainview.php',$moviedata);
}
Here View Does Not Refresh Its Contents
How Can I Solve This

Hope this will help you :
First if you want a single view ,should put you code it in if else condition and second assign your data in same variable
Your code should be like this :
public function index()
{
$movielanguage=$this->input->get('language');
if(! empty($movielanguage))
{
$moviedata['movies']=$this->main_model->getmoviebylanguage($movielanguage);
}
else
{
$moviedata['movies']=$this->main_model->getAllMovies();
}
$this->load->view('home/mainview.php',$moviedata);
}
Second : and if you want to add different view for different category of movies
You can do like this :
public function index()
{
$movielanguage=$this->input->get('language');
if(! empty($movielanguage))
{
$moviedata['popularmovies']=$this->main_model->getmoviebylanguage($movielanguage);
/*popularview is just an example*/
$this->load->view('home/popularview.php',$moviedata);
}
else
{
$moviedata['allmovies']=$this->main_model->getAllMovies();
/*allview is just an example*/
$this->load->view('home/allview.php',$moviedata);
}
}

Related

Callback function in igniter datatables is not working

I am using igniter datatables in my code for datatables with server side processing.My code is like :
public function datatable()
{
$this->datatables->select("
insight_worksheet.id,
insight_worksheet.workingDate,
insight_worksheet.reason,
...
insight_worksheet.worksheet_status
")
->from("insight_worksheet")
->edit_column('Actions', '$1', 'callback_test(insight_worksheet.id,insight_worksheet.worksheet_status)')
...
->join("insight_status","status.id=worksheet.status","left");
echo $this->datatables->generate();
}
public function test($id,$worksheetStatus){
return "srimanta";
}
in my view page for Actions column direct string callback_test(insight_worksheet.id,insight_worksheet.worksheet_status)
is showing instead of actual work whereas for other columns, showing exact data.
For time being I put two functions in my controller class.
Could you please let me know the issue in my code?
Thanks in advance.
Try adding the callback function test in a helper file.
Your model file:
public function datatable()
{
$this->datatables->select("
insight_worksheet.id,
insight_worksheet.workingDate,
insight_worksheet.reason,
...
insight_worksheet.worksheet_status
")
->from("insight_worksheet")
->edit_column('Actions', '$1', 'test(insight_worksheet.id,insight_worksheet.worksheet_status)')
...
->join("insight_status","status.id=worksheet.status","left");
echo $this->datatables->generate();
}
your helper file:
public function test($id,$worksheetStatus){
return "srimanta";
}

cant get passed parameter by url in php

please help me, this is simple thing but I don't know why this still keep error for an hour,
on my view I've got :
<a href="admin/editProduct?idd=$id">
on my controller that directed from above :
public function editProduct(){
$data["id"] = $_GET['idd'];
$data["produk"] = $this->model_get->get_data_list(2);
//this below doesn't work either, i just want to past the parameter to my model
//$data["produk"] = $this->model_get->get_data_list($_GET['idd']);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}
I can not use the array id. It keeps telling me undefined variable idd.
I don't know what to do anymore, please anyone help!
I am using Codeigniter framework
Change your view as:
<a href="admin/editProduct/<?php echo $id;?>">
And in the controller, either get the id as parameter,
public function editProduct($id) {
}
or as uri segment
public function editProduct() {
$id = $this->uri->segment(3);
}
Change your link (in view) with this
<a href="admin/editProduct/$id">
And change your controller as
public function editProduct($id) {
}
Then user $id inside your controller
Make the href link as follows:
...
And edit the controller like this:
public function editProduct($id){
...
$this->model_get->get_data_list($id);
}
Where $id will be the passed $id.
make
try this this works fine
public function editProduct()
{
$id=$this->uri->segment(3);
$data["produk"] = $this->model_get->get_data_list($id);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}

How to send data from a button click to a controller in laravel 4

I am trying to send the id data from a button back to one of my controllers on button click. Depending on the id sent, I want the controller function to redirect the user to different views.
Button:
<a href="/oauth/facebook" id="{{$artist->id}}" class="sign">
/oauth/facebook route:
Route::get('oauth/{provider}', 'Oauth2Controller#action_session')->before('guest');
Function action_session in Oauth2Controller:
public function action_session($provider) {
$id=Input::get('id');
if($id > 5) {
return Redirect::to('/fans');
}
else {
return Redirect::to('/artists');
}
}
I tried using ajax but it seems that the oauth/facebook route is called on button click first, before the ajax request can go through (so the $id field is blank when the controller runs). Is there any way to do this? Thank you.
You won't find the id in the input using this approach, but you could extend the route with an optional (unless you want it for all providers) parameter like this:
Route
Route::get('oauth/{provider}/{id?}', 'Oauth2Controller#action_session')->before('guest');
Controller
public function action_session($provider, $id = null) {
if ($id > 5) {
return Redirect::to('fans');
} else {
return Redirect::to('artists');
}
}
Button
...

Page redirection in Codeigniter

I am a newbie to Codeigniter. As part of studying it, I created a form which inserts data to the database. After insertion it is not redirecting properly to the form view page (formvalidation.php).
Form Action
public function submitform()
{
$formdata['username']=$this->input->post('username');
$formdata['address']=$this->input->post('address');
$formdata['state']=$this->input->post('state');
$formdata['country']=$this->input->post('country');
$data['state']=$this->getstatearray();
$data['country']=$this->getcountries();
if($this->userprofile_model->setdata($formdata)=='success')
{
$this->load->view('formvalidation/formvalidation',$data);
}
else
{
$this->load->view('formvalidation/formvalidation',$data);
}
}
Form view page (formvalidation.php)
echo form_open('formvalidation/submitform');
echo form_input('username');
echo "<br>".form_textarea("address");
echo "<br>".form_dropdown('state',$state);
echo "<br>";
echo form_dropdown('country',$country);
echo "<br><br>".form_submit('submit','Submit');
echo form_close();
Model (userprofile_model.php)
class userprofile_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function setdata($data)
{
$this->db->insert('userprofile',$data);
if($this->db->affected_rows()>0)
{
echo "success";
}
else
{
echo "fail";
}
}
}
Now the value is getting inserted into the database. But after insertion I want to redirect it to the url http://www.example.com/codeigniter/index.php/formvalidation/. But instead, it is now redirecting to http://www.example.com/codeigniter/index.php/formvalidation/submitform where submitform is the form action method.
How can I solve this problem? I tried with putting exit; after the page redirect function. But it won't work. Please help me to fix this.
Thanks in advance.
yes it will, because there is no redirect code. When the form is submitted your submitform function is executed and in that function you are loading the view. So if you want to redirect
to something else you must use the redirect() function.
public function submitform()
{
$formdata['username']=$this->input->post('username');
$formdata['address']=$this->input->post('address');
$formdata['state']=$this->input->post('state');
$formdata['country']=$this->input->post('country');
$data['state']=$this->getstatearray();
$data['country']=$this->getcountries();
if($this->userprofile_model->setdata($formdata)=='success'){
redirect("Your URL");
}
else{
redirect("Your URL");
}
}
Also you must properly sanitize the user input. You must validate your form inputs before passing it to model. You can find more info here
First Load the view page in controller index function.
function index()
{
$this->load->view('v_bank_master');
}
Then in model page, after insertion done. give the below code.
redirect('give your controller page name here','refresh');
Your page will now redirect to the view page after insertion.
Hope it will help you...
I use redirect('yourControllerName', 'refresh').
Read more here: Redirect()
After insertion give the below code
redirect('controller_name/function_name');

How does codeigniter know how to pass parameters from controller to the model

I am starting to learn codeigniters active record and i am querying my database using parameters passed from the controller to the model.
First i am passing the id from the controller to the model and that works.
Controller
function bret($id){
$this->load->model('school_model');
$data = $this->school_model->get_city_and_population($id);
foreach ($data as $row)
{
echo "<b>Name Of The City</b>...........". $row['Name'];
echo "<br/>";
echo "<b>Total Population</b>...........".$row['Population'];
}
}
Model
function get_city_and_population($id){
$this->db->select('Name,Population');
$query = $this->db->get_where('city', array('ID'=>$id));
return $query->result_array();
}
I went ahead and put in multiple parameters expecting to fail but this works but i am not so sure why it worked or what worked.
Controller
public function parameters($id,$name,$district){
$this->load->model('school_model');
$data = $this->school_model->multiple_parameters($id,$name,$district);
foreach ($data as $row)
{
echo "<b>Total Population</b>...........".$row['Population'];
}
}
Model
function multiple_parameters($id,$name,$district){
$this->db->select('Population');
$query = $this->db->get_where('city', array('ID'=>$id,'Name'=>$name,'District'=>$district));
return $query->result_array();
}
In my multiple parameters example i visited http://example.com/env/at/index.php/frontpage/parameters/7/Haag/Zuid-Holland/
Here,i know the name Haag is in id 7 and the district is Zuid-Holland
Here are my questions.How does codeigniter know how to pass the parameters from the url to the model and secondly,what if i was slightly wrong like 7/Haag/Zuid-Hollandes/,how would i show the user that,that url is wrong and fallback to a default value instead of showing blank when the parameters are wrong?.
//In codeiginter URI contains more then two segments they will be passed to your function as parameters.
//if Url: http://example.com/env/at/index.php/frontpage/parameters/7/Haag/Zuid-Holland/
//Controller: forntpage
public function parameters($id,$name,$district){
echo $id.'-'$name.'-'.$district;
}
//and if you are manually getting url from segment & want to set default value instead of blank then use following:
public function parameters(
$this->load->helper("helper");
$variable=$this->uri->segment(segment_no,default value);
//$id=$this->uri->segment(3,0);
}
//or
//Controller: forntpage
public function parameters($id='defaultvalue',$name='defaultvalue',$district='defaultvalue'){
echo $id.'-'$name.'-'.$district;
}
That's just simple uri mapping in CI, or uri param binding if you will.
When you have a method like:
public function something($param1, $param2) {
// get from: controller/something/first-param/second-param
}
That means your uri segments are passed as arguments to your controller method.
The above method could be written as:
public function something() {
$param1 = $this->uri->segment(3);
$param2 = $this->uri->segment(4);
// segment 1 is the controller, segment 2 is the action/method.
}
You need to understand that you have to manually check if the uri segments are exactly what you want them to be, as CI doesn't do anything else than this mapping.
Next, if you want to have some defaults, following statement is true:
public function something($param1 = 'some default value', $param2 = 'other value') {
// get from: controller/something/first-param/second-param
}
That is, if a url like: /controller/something is passed along, you will still get your default values back. When controller/something/test is passed, your first param is overridden by the one from the url (test).
That's pretty much it.

Categories