我想创建的基本上是类似Facebook的东西。如果用户发布了他/她的用户名,则链接将如下所示:www.mysite.com/user/{username},如果不是,则链接将如下所示:www.mysite.com/user/{userid}。使用下面的代码,我得到了我试图从数据库中检索的所有字段的未定义索引。它只与user_id一起使用,但不与用户名一起使用。任何帮助将非常感激。CodeIgniter通过用户名获得用户信息,如果用户名存在,如果没有得到用户ID
链接到简档(查看):
<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['user/(:any)'] = "user/profile/$1";
用户控制器与用户的方法:
<?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');
}
}
型号从数据库中检索用户信息:
<?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');
}
}
or_where做的伎俩,欢呼兄弟! :) – Cooper 2014-10-07 15:52:41