I am using codeigniter. I used session variable to store a id in a function and get the stored value in another function. I have a controller file which is given below:
public function select_service($id)
{
$this->load->library('session');
$this->session->unset_userdata('employee');
$this->session->set_userdata('employee', ['total'=>$id]);
// $id = $this->uri->segment(4);
$data['service'] = $this->add_service_model->get_services();
$data['main_content'] = 'admin/service_limitation/service_view';
$this->load->view('includes/template', $data);
}
In the above code I get the id from a form and store in the session variable
public function services()
{
//here i need to get the session variable employee['total']
$id = $this->uri->segment(5);
$data_to_store=array('employee_id'=>$id,'service_id'=>$this->input->post("services"));
$this->add_service_model->save($data_to_store);
$data['addservice'] = $this->add_service_model->get_addservices();
$data['main_content'] = 'admin/service_limitation/newview';
$this->load->view('includes/template', $data);
}
In this function service I need to retrieve the stored value. both functions are in same controller. can some one help me with the code?
Manuall
Auto load the session library. This way the session is always loaded.
when setting the id do: $this->session->set_userdata('employee', $id);
when you want to get that id user $id = $this->session->userdata('employee');
Related
I want to ask about how to get data from variables that are in other functions but still in the same controller....
this is the function, where the variable will be sent named $whs
function deliver($whs_id)
{
$whs = $whs_id; // here is variable to pass
$this->cart->destroy();
$this->template->set_title('Package');
$this->breadcrumbs->push('Package', '/Package');
$this->breadcrumbs->push('Create Package', '/pacakge/index');
$data['item'] = $this->MY_Model->join2_where('box_stock','product_code','box_product','product_code','whs_id',$whs_id)->result();
$this->template->load('templates/Template', $this->folder.'/item', $data);
}
function save()
{
$whs2 =$whs; // i will get data in here
// judul halaman
$this->template->set_title('Package');
// Breadcrumbs
$this->breadcrumbs->push('Package', 'package');
$this->breadcrumbs->push('Save', '/package/save');
// JS Init
$js_init = "";
}
how can I get the data that is in another function in the same controller ?
Am trying to pass some data from one function to another when i set the data into session and print the session data i get the correct data, but whe trying to use the data in another function i get the word "Assets" i dont know where this word come from. Session library is auto loaded.Any help please.
These are my codes:
First function:
$client_id = $this->uri->segment(3);
$sess_array = array(
'cl_id' => $client_d,
'selected_client'=>TRUE,
);
$this->session->set_userdata('selected_client',$sess_array);
Second function:
$client_sess = $this->session->userdata('selected_client');
$client_id= $client_sess['cl_id'];
Try this i think this will give you some idea.
function one(){
$client_id = $this->uri->segment(3);
$sess_array = array(
'cl_id' => $client_d,
'selected_client'=>TRUE,
);
$this->two($sess_array);
}
function two($id){
$client_id= $id;
}
Here is what the Model looks like:
function getResponse($gettingresponse)
{
$enrollresponse=$gettingresponse['sendresponse'];
return $enrollresponse;
}
The Controller is as follows:
public function Register()
{
$this->load->view('firstview');
$this->load->view('secondview');
if($_POST) {
$gettingresponse=array(
'sendresponse'=>$_POST['source'],
'receiverresponse'=>$_POST['destination']
);
$registration_confirm=$this->systemModel->responselogin($gettingresponse);
$resposeflag=$this->systemModel->getEmail($gettingresponse);
$data['resposeflag']=$gettingresponsevalue;
if($registration_confirm){
$this->token($data);
}
}
$this->load->view('thirdview');
}
public function token($data=array())
{
$this->load->view('firstview');
$data['resposeflag'];
$this->load->view('token',$data);
$this->load->view('thirdview');
}
The following View shows the data that has been passed between the functions of the Controller.
<?php
echo form_input(array('name'=>'source','readonly'=>'true','value'=>$resposeflag));
?>
I just started learning codeigniter and i must say its pretty easy but I have a problem dealing with wrong urls, for example:
if I have an anchor tag like this
http://example.com/info/2
in the controller if I have
public function info( $x ) {
$data['body'] = "Personal_info";
$data['details'] = $this->person_model->get_detail( $x );
$this->load->view('view', $data);
}
the controller grabs the links
segment (3)
and then grab the details of the id from the database.
now for instance if a user manually edit the link on the browser and change the
segment(3)
to lets say 7 and there is no id in the database as 4.
how do I handle such a problem? I am a beginner so please pardon me
You could use empty method to check if there is data and if not redirect away from the page.
public function info( $x )
{
$details = $this->person_model->get_detail( $x );
if(empty($details))
redirect('other/url');
$data['body'] = "Personal_info";
$data['details'] = details;
$this->load->view('view', $data);
}
This way it doesn't throw errors and potentially attempt to display something that doesn't exist.
you can check if the passed id exists in database before trying to fetch related data, like:
$data_exists = $this->person_model->data_exists( $x );
if( $data_exists ) {
$data['details'] = $this->person_model->get_detail( $x );
$this->load->view('view', $data);
}
else {
//load some view for showing no such id exists in db
}
where data_exists() can be a function in model which returns TRUE or FALSE depending on existance of your id in database.
I am having trouble retrieving and putting the user id in the url into a variable. Here is my controller that I am trying to do this with. I have read the documentation in the user guide,however I am not getting any results.
here is my url structure:
clci.dev/account/profile/220
Controller:
public function profile()
{
$this->load->helper('date');
$this->load->library('session');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
$data['session_id'] = $session_id;
//TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
$user_get = $this->input->get($user['id']);
echo $user_get;
if($user['id'] == $session_id)
{
$data['profile_icon'] = 'edit';
}
else
{
$data['profile_icon'] = 'profile';
}
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/profile_template', $data);
}
Am I flat out doing this wrong, or are there adjustments that I need to make in my config files?
thanks in advance
In codeigniter, instead of having something.com/user.php?id=2 we use something.com/user/2 and the way to get that 2 is using this:
$this->uri->segment(3)
for more info http://ellislab.com/codeigniter/user-guide/libraries/uri.html
edit:
based on your url: clci.dev/account/profile/220 you would need $this->uri->segment(4)
You would set up your controller function as follows
public function profile($id = false)
{
// example: clci.dev/account/profile/222
// $id is now 222
}
I suppose at this point that the value for $_GET['id'] is intended to be 220 so here:
To get 220, you would have to do it this way (except the get value in question is other than 220 as shown in your url above)
Let's say you visit: clci.dev/account/profile/220. Follow the comments for more info.
public function profile()
{
$this->load->helper('url'); //Include this line
$this->load->helper('date');
$this->load->library('session');
$session_id = $this->session->userdata('id'); //Ensure that this session is valid
$this->load->model('account_model');
$user = $this->account_model->user(); //(suggestion) you want to pass the id here to filter your record
$data['user'] = $user;
$data['session_id'] = $session_id;
//TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
$user_get = $this->uri->segment(3); //Modify this line
echo $user_get; //This should echo 220
if($user_get == $session_id) //Modify this line also
{
$data['profile_icon'] = 'edit';
}
else
{
$data['profile_icon'] = 'profile';
}
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/profile_template', $data);
}
I hope that helps your gets your started in the right track.
you can directly get like this:
public function profile($user_id = 0)
{
//So as per your url... $user_id is 220
}
I am creating the user module on my social network. I am trying to set it up to where if you are signed in and looking at your own profile you are $user['id']; and if you are signed in and looking at someone else's profile you are $prof['id']; which I have started to accomplish.
However I am new to codeigniter and php all together and trying to figure out how to attach the users id to the end of the url so my system can check to see if it's $user['id'] or $prof['id']
Controller:
public function profile()
{
$data['profile_icon'] = 'profile';
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/profile_template', $data);
$this->load->helper('date');
$this->load->library('session');
$session_id = $this->session->userdata['id'];
//USER ID LOOKING AT OWN PROFILE
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
//PROF ID LOOKING AT SOMEONE ELSE'S PROFILE
$this->load->model('account_model');
$prof = $this->account_model->prof();
$data_prof['prof'] = $prof;
if($session_id != $user['id'])
{
echo $prof['id'];
echo "<br />";
// FOR TESTING PURPOSES
echo "other user";
}
elseif($session_id == $user['id'])
{
echo $user['id'];
echo "<br />";
// FOR TESTING PURPOSES
echo "hey it's me ";
}
}
View:
<div class="edit_prof_header">
// WHAT IS A GOOD WAY TO MAKE THIS DYNAMIC WITH THE `$prof` AND `$user` WITH OUT HAVING TO D O AN IF STATEMENT WITH EACH ELEMENT?
<?php echo $user['first_name']." ".$user['last_name'];?>
</div>
Model:
// PERHAPS I COULD SOLVE THE WHOLE DIFFERENCE OF SINGED IN AND OUT HERE AND INCORPORATE TO THE CONTROLLERS AND VIEWS?????
public function user()
{
$session = $this->session->userdata('is_logged_in');
$user_id = $this->session->userdata('id');
$query = $this->db->query("SELECT * FROM users WHERE id=$user_id LIMIT 1");
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
//above returns the single row you need to the controller as an array.
//to the $data['user'] variable.
}
}
public function prof()
{
$session = $this->session->userdata('is_logged_in');
$user_id = $this->session->userdata('id');
$query = $this->db->query("SELECT * FROM users WHERE id=$user_id LIMIT 1");
if($query->num_rows()==1)
{
$data_prof = $query->result_array();
return $data_prof[0];
//above returns the single row you need to the controller as an array.
//to the $data['user'] variable.
}
}
Thanks in advance.
***EDIT//Trying to get the id attched to the url
$this->load->view('includes/templates/profile_template', $data);
$this->load->helper('date');
$this->load->library('session');
$session_id = $this->session->userdata['id'];
$this->load->model('account_model');
//USER ID LOOKING AT OWN PROFILE
$data_user['user'] = $this->account_model->user();
$user['id'] = $this->uri->segment(3);
echo $user['id'];
$data['profile_icon'] = 'profile';
$data['main_content'] = 'account/profile/'.$user['id'];
***EDIT FOR ROUTES
route['default_controller'] = "home";
$route['404_override'] = '';
$route['profile/:num'] = "auth/profiles";
//CONTROLLER
public function profile()
{
$this->load->helper('date');
$this->load->library('session');
$session_id = $this->session->userdata['id'];
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
$user['id'] = $this->uri->segment(4);
$data['profile_icon'] = 'profile';
$data['main_content'] = 'account/profile/'.$user['id'];
$this->load->view('includes/templates/profile_template', $data);
}
First of all I don't see the need for switching ID's like that, it serves no practical purpose to be honest. If you're viewing a profile you do it by userId, regardless of if it's your profile or someone else's profile. If you want the ability to edit a profile page you can do that simply by checking if the userId for the profile matches the session userId.
For example in your view you could have a link:
<?php
if($user['userId']==$this->session->userdata('userId'))
{
echo 'Edit my profile';
}
You would also check again that the userId matched the session before doing any saving. By the way above is how you would create a link with the userId attached, to use that link in a controller you'd check the uri segment, so something like this:
Example URL: www.example.com/profiles/view_profile/23
$userId = $this->uri->segment(3);
Understanding uri segments.
I think sometimes people get confused about the URI segment. Your base_url is 0, you start counting from there. It doesn't matter where it is in the actual URL structure as long as you remember the base_url is always 0. For example if your base URL is www.example.com/hello/world Then uri segment 1 comes after world NOT .com.
You also do a lot of beginner things that to be honest I have no idea why I see tutorials showing people all the time.
$prof = $this->account_model->prof();
$data_prof['prof'] = $prof;
Why not do this? What is the purpose of setting a variable to set a variable?:
$data_prof['prof'] = $this->account_model->prof();
Also in your user query take the LIMIT out. That is going to ensure you only get one result even if your query matches more than one result, you should ensure you can't have multiple users with the same ID at all times obviously but having that in the query sort of defeats the purpose of the if num rows statement. You're going to get 1 or 0 no matter what.