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
}
Related
First sorry for my english( it is not my main language ).
I am new in CodeIgniter3 and i like it.
Lets say this is my model:
function login($uname, $upassword)
{
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
foreach ($query->result() as $row) {
$data['zamestnanec'] = $row->tpred_zamestnanci." ".$row->meno_zamestnanci. " ".$row->priezvisko_zamestnanci." ".$row->tza_zamestnanci;;
}
return ($data);
}
And this is my controller:
//Funkcia na prihlásenie používatela
function loginUser()
{
//Načítať model
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
if ($this->user_model->login($uname, $upassword))
{
$this->session->set_userdata('user', $meno);
$data['user'] = $this->session->userdata('user');
redirect('/otk/', $data);
}
else
{
redirect('/user/');
}
}
I want to ask you how to pass/display data from model to session. To $this->session->userdata('user').
Can you explain me the correct process off passing data from model to controller and from model to session. (like if you were trying to explain to a man who is thinking slowly).
I do not want links to documentation, just one or few persons who can explain it on example.
you can pass information from model to controller in two ways.
By using session
first fetch information using query and return that array to controller.
it is good if you return data to controller first then in controller
set up the session by using that returned array.
As in this example.
Model
function login($uname, $upassword)
{
$this->db->select("*");
$tthis->db->from("table_name")
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
// you can user result_array() to get all information in array form.
$this->result = $query->result_array();
return $this->result;
}
In Controller
//Funkcia na prihlásenie používatela
function loginUser()
{
//Načítať model
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
if ($this->user_model->login($uname, $upassword))
{
$this->session->set_userdata('user', $meno); // here you are setting up the session.
$data['user'] = $this->session->userdata('user');
redirect('/otk/', $data);
}
else
{
redirect('/user/');
}
}
Hope this will help you :
get all the user information (in array) from the model whatever you want :
In controller :
First way :
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$lname = $this->input->post('lname');//example
$session_arr['uname'] = $uname;
$session_arr['fullname'] = $fname.' '.$lname; // example
$this->session->set_userdata($session_arr);
Second way :
$user = $this->user_model->login($uname, $upassword);
if ($user != false)
{
// Valid user
// $validate containing user details too. to check add this next line
// print_r($validate);die;
$this->session->set_userdata($user);
redirect('/otk/');
}
for more : https://codeigniter.com/user_guide/libraries/sessions.html#initializing-a-session
$user = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($user);
Just pass the model to controller whether data is correct or not. no need a big loop there in the model
In Model
function login($uname, $upassword)
{
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
$result = $query->result_array();
$count = count($result); # get how many data passed
if ($count == 1) {
return $result;
}
else
{
return false;
}
}
In Controller
function loginUser()
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
//$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
$validate = $this->user_model->login($uname, $upassword);
if ($validate != false)
{
# Valid user
# $validate conating user details too. to check add this next line print_r($validate);die;
$this->session->set_userdata('user', $uname);
redirect('/otk/');
}
else
{
# Invalid User
redirect('/user/');
}
}
And in otk function just call session value user
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');
i have here:
public function index()
{
$this->is_logged_in();
$this->load->model('ReportModel');
$this->load->model('ConsultantModel');
if (isset($_POST['report'])){
$reports=$this->ReportModel->search($_POST['report']);
} else {
$reports=$this->ReportModel->get_last_ten_entries();
}
$models=$this->ReportModel->getModel();
$terms=$this->ReportModel->getTerm();
$username = $this->session->userdata('username');
$data['query'] = $this->ConsultantModel->getConsultantData($username);
//i want to send data to the report list view
//$this->load->view('report/reportlist', $data);
$this->load->view('report/reportlist', array('reports'=>$reports,'terms'=>$terms,'models'=>$models));
}
i want to pass both the array and the data in a single view (report/reportlist). i know that its possible because its very a simple idea but i don't know how. even if it can't, i know you guys know some tricks. please if you know, answer this.
public function index()
{
$this->is_logged_in();
$this->load->model('ReportModel');
$this->load->model('ConsultantModel');
$data = array();
if (isset($_POST['report'])){
$data['reports'] = $this->ReportModel->search($_POST['report']);
} else {
$data['reports'] = $this->ReportModel->get_last_ten_entries();
}
$data['models'] = $this->ReportModel->getModel();
$data['terms'] = $this->ReportModel->getTerm();
$username = $this->session->userdata('username');
$data['query'] = $this->ConsultantModel->getConsultantData($username);
$this->load->view('report/reportlist', $data);
}
You can use as following in your views -
1) Reports -
i) If single value -
echo $reports;
ii) If an array
foreach($reports as $report)
{
echo $report[..];
}
Similarly for rest.
I am new to codeigniter and could be better at php. I have a news item being retrieved from the database table news, it has a foreign key of partner_id that is tied to the partners table. I want to get that value and use it to get the associated partner and display it's info. This is the latest of my attempts. I think I might be making it too hard. All relevant files are below. Thanks in advance.
In the model if the $id_partner is being assigned in the get_news function I don't think it is passing to the get_partner function.
news_model.php
public function get_news($slug_news = FALSE)
{
$this->load->helper('array');
if ($slug_news === FALSE)
{
$news_query = $this->db->get('news');
return $news_query->result_array();
}
$news_query = $this->db->get_where('news', array('slug' => $slug_news));
return $news_query->row_array();
global $id_partner;
$id_partner = element('partner_id', $news_query);
}
public function get_partner($id_partner = FALSE){
$partners_query = $this->db->get('partners');
$partners_query = $this->db->get_where('partners', array('id' => $id_partner));
return $partners_query->row_array();
}
The $slug_news in the view function doesn't apply to the partner part (obviously). but can I have another function?
controller, news.php
public function view($slug_news)
{
$data['news_item'] = $this->news_model->get_news($slug_news);
$partner_data['partner_listing'] = $this->news_model->get_partner($id_partner);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('news/news_partner', $partner_data);
$this->load->view('templates/sidebar');
$this->load->view('templates/footer');
}
view1, news/view.php
<?php
echo '<img src="/images/'.$news_item['thumb'].'" />';
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
echo $news_item['bus_name'];
?><br>
and view2, news/news_partner.php
<?php
echo '<h2>'.$partner_listing['bus_name'].'</h2>';
echo $partner_listing['address1'];
echo $partner_listing['address2'];
echo $partner_listing['city'];
echo $partner_listing['state'];
echo $partner_listing['phone'];
echo $partner_listing['email'];
?>
Your model is a little all over the place. To start, the two lines after the return are never called in get_news. I would just simplify the model and do a simple join:
public function get_news()
{
$this->db->select('*');
$this->db->from('news');
$q = $this->db->join('partners', 'news.partner_id = partners.id');
return $q->result();
}
Here is what I put with your code:
public function get_news($slug_news = FALSE)
{
if ($slug_news === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$this->db->select('*');
$this->db->from('news');
$query_news = $this->db->join('partners', 'news.partner_id = partners.id');
return $query_news ->result();
}
I only used the "view.php" view as well. Does $slug_news need to be in there somewhere? How does it know what line to get? Thanks for your help, I am having so much trouble.
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.