dongtiannai0654 2014-10-07 15:20
浏览 79
已采纳

如果用户名存在,CodeIgniter按用户名获取userinfo,如果没有得到userID

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');
    }
}
  • 写回答

2条回答 默认 最新

  • doulei2100 2014-10-07 15:42
    关注

    You can probably handle this in your SQL layer. The proposition is to find the user record either by their username or by their user ID. So, in SQL (CodeIgniter Active Record) you can phrase this as:

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

    In this case we are assuming the $user_id can be a string (user_name) or an integer (user_id). We are also assuming that you have no user_names that are numeric and may by chance match a user_id. To prevent multiple rows being returned, you could add a limit to the query.

    Once you have a user record, then you will need to pass in the found user record's user_id in the other queries instead of the user_id passed into the function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效