What I'd like to create is basically something similar like Facebook does. If user have posted his/her's username, the link would be like: www.mysite.com/user/{username}, if not link would be like: www.mysite.com/user/{userid}. With the codes below I'm getting undefined indexes for all of the fields I'm trying to retrieve from the dataabase. It does work only with user_id but not with username. Any help would be much appreciated.
Link to profiles (Views):
<a href="<?=base_url()?>user/<?=isset($event['username']) ? $event['username'] : $event['creatoruserid']?>">
<img src="<?=base_url()?>public/images/uploads/profiles/<?=$event['profilepic']?>" class="event-profile-pic">
</a>
Route to profiles:
$route['user/(:any)'] = "user/profile/$1";
User Controller with user method:
<?php
class User Extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('event_model');
$this->load->model('user_model');
}
public function profile($user_id)
{
// All the data we need in profile page
$data['user'] = $this->user_model->getUserInfo($user_id);
$data['events'] = $this->event_model->getEventsById($user_id);
$data['total_events_created'] = $this->user_model->TotalEventsCreated($user_id);
$data['total_comments'] = $this->user_model->TotalComments($user_id);
$data['total_attending_events'] = $this->user_model->TotalAttendingEvents($user_id);
$this->load->view('/app/header');
$this->load->view('/app/profile', $data);
$this->load->view('/app/footer');
}
}
Model for retrieving the userinfo from database:
<?php
class User_model extends CI_Model
{
public function getUserInfo($user_id)
{
$this->db->select('*')->from('users')->where(['user_id' => $user_id]);
$query = $this->db->get();
return $query->first_row('array');
}
}