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条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启