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);
}