Saturday 15 March 2014

php - CodeIgniter get userinfo by username if username exists, if not get by userID -



php - CodeIgniter get userinfo by username if username exists, if not get by userID -

what i'd create similar facebook does. if user have posted his/her's username, link like: www.mysite.com/user/{username}, if not link like: www.mysite.com/user/{userid}. codes below i'm getting undefined indexes of fields i'm trying retrieve dataabase. work user_id not username. help much appreciated.

link 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 profiles:

$route['user/(:any)'] = "user/profile/$1";

user controller 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) { // info 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 retrieving userinfo 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(); homecoming $query->first_row('array'); } }

you can handle in sql layer. proposition find user record either username or user id. so, in sql (codeigniter active record) can phrase as:

$this->db->select('*') ->from('users') ->where(['user_id' => $user_id]) ->or_where(['user_name'] => $user_id]) ->limit(1);

in case assuming $user_id can string (user_name) or integer (user_id). assuming have no user_names numeric , may chance match user_id. prevent multiple rows beingness returned, add together limit query.

once have user record, need pass in found user record's user_id in other queries instead of user_id passed function.

php mysql codeigniter url

No comments:

Post a Comment