我正在我的社交网络上创建用户模块。我正在尝试将其设置为如果您已登录并查看自己的个人资料,$user['id'];
并且如果您已登录并查看其他人的个人资料,那么$prof['id'];
我已经开始完成。
但是,我是 codeigniter 和 php 的新手,并试图弄清楚如何将用户 id 附加到 url 的末尾,以便我的系统可以检查它是否是$user['id'
] 或$prof['id']
控制器:
public function profile()
{
$data['profile_icon'] = 'profile';
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/profile_template', $data);
$this->load->helper('date');
$this->load->library('session');
$session_id = $this->session->userdata['id'];
//USER ID LOOKING AT OWN PROFILE
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
//PROF ID LOOKING AT SOMEONE ELSE'S PROFILE
$this->load->model('account_model');
$prof = $this->account_model->prof();
$data_prof['prof'] = $prof;
if($session_id != $user['id'])
{
echo $prof['id'];
echo "<br />";
// FOR TESTING PURPOSES
echo "other user";
}
elseif($session_id == $user['id'])
{
echo $user['id'];
echo "<br />";
// FOR TESTING PURPOSES
echo "hey it's me ";
}
}
看法:
<div class="edit_prof_header">
// WHAT IS A GOOD WAY TO MAKE THIS DYNAMIC WITH THE `$prof` AND `$user` WITH OUT HAVING TO D O AN IF STATEMENT WITH EACH ELEMENT?
<?php echo $user['first_name']." ".$user['last_name'];?>
</div>
模型://也许我可以解决这里烧录和烧录的全部差异,并结合到控制器和视图中??????
public function user()
{
$session = $this->session->userdata('is_logged_in');
$user_id = $this->session->userdata('id');
$query = $this->db->query("SELECT * FROM users WHERE id=$user_id LIMIT 1");
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
//above returns the single row you need to the controller as an array.
//to the $data['user'] variable.
}
}
public function prof()
{
$session = $this->session->userdata('is_logged_in');
$user_id = $this->session->userdata('id');
$query = $this->db->query("SELECT * FROM users WHERE id=$user_id LIMIT 1");
if($query->num_rows()==1)
{
$data_prof = $query->result_array();
return $data_prof[0];
//above returns the single row you need to the controller as an array.
//to the $data['user'] variable.
}
}
提前致谢。
***编辑//试图让 id 附加到 url
$this->load->view('includes/templates/profile_template', $data);
$this->load->helper('date');
$this->load->library('session');
$session_id = $this->session->userdata['id'];
$this->load->model('account_model');
//USER ID LOOKING AT OWN PROFILE
$data_user['user'] = $this->account_model->user();
$user['id'] = $this->uri->segment(3);
echo $user['id'];
$data['profile_icon'] = 'profile';
$data['main_content'] = 'account/profile/'.$user['id'];
***编辑路线
route['default_controller'] = "home";
$route['404_override'] = '';
$route['profile/:num'] = "auth/profiles";
//控制器
public function profile()
{
$this->load->helper('date');
$this->load->library('session');
$session_id = $this->session->userdata['id'];
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
$user['id'] = $this->uri->segment(4);
$data['profile_icon'] = 'profile';
$data['main_content'] = 'account/profile/'.$user['id'];
$this->load->view('includes/templates/profile_template', $data);
}