我在我的社交網絡上創建用戶模塊。我試圖將其設置爲如果您已登錄並查看自己的個人資料,您是$user['id'];
,並且如果您已經登錄並查看其他人的個人資料,您就是我已經開始完成的$prof['id'];
。如何將用戶標識附加到代碼點火器中的網址?
不過我一起新笨和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>
型號: //也許我可以解決SINGE的全差分D在這裏和在這裏,並將其納入控制者和觀點?????
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.
}
}
在此先感謝。
***編輯//試圖讓attched到URL中的ID
$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'];
***編輯ROUTES
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);
}
基本上兩者都是相同的,所以你只需要使用if語句添加缺失的數據,而不是像你現在正在做的那樣。 無論如何codeigniter的會話使用正常的大括號()not [] – ahmad
$ user ['id']和$ prof ['id']不是由會話助手完全創建的。 – LightningWrist