因此,我正在創建一個類的電子商務網站。在我的個人資料頁面上,我打算顯示存儲在數據庫中的各種信息。現在我只會搞亂用戶照片。我在模型中有一個函數,可以從DB'users'的'Image_Path'列中獲取用戶照片。我的問題是,每個配置文件視圖顯示相同的照片。我認爲它會返回所有圖片,也許只是將它設置爲第一個?從sql DB獲取單個用戶圖片(CodeIgniter)
這裏是模型
<?php
class Profile_Model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
// public function get_profile_picture()
// {
//
// $query = $this->db->get('users');
// return $query -> result();
// }
public function get_single_profile($id){
//get whole table
$this->db->select('Username');
$this->db->select('Image_Path');
$this->db->from('users');
$this->db->where('User_ID', $id);
$query = $this->db->get();
return $query->result();
}//end get_single_profile
//for uploading profile pics
public function set_profile_pic(){
}
}//CLASS
?>
的控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Profile extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('session'); //needed to use global session variables
$this->load->helper('language');
$this->load->model('Profile/Profile_Model');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$data['profilepicture'] = $this->Profile_Model->get_single_profile();
$this->load->view('Profile/Profile', $data);
}// echo session variable logged_in
else{
redirect(base_url().'Login'); //redirect to user profile view after verification)
}
}//END Index
public function logout(){
$this->session->sess_destroy();
redirect(base_url(). 'Login');
}//END LOGOUT
public function display_profile_pic(){
$data['profilepicture'] = $this->Profile_Model->get_profile_picture();
$this->load->view('Product/Product', $data);
}
}//END CLASS
觀
<!DOCTYPE html>
<html>
<head>
<title><?php echo $_SESSION['logged_in']?></title>
<style type="text/css">
.container {
width: 15%;
text-align: center;
clear:both;
}
.container input {
width: 100%;
clear: both;
}
.logout {
position: fixed;
bottom: 10%;
right: 10%;
}
.aboutme{
position:fixed;
right:50%;
bottom:50%;
}
.shopbutton{
position:fixed;
right:17%;
top:10%;
}
.checkout{
position:fixed;
right:10%;
top:10%;
}
</style>
</head>
<body>
<h3>Logged In As: <?php echo $_SESSION['logged_in']?> </h3>
**在顯示畫面
<?php foreach ($profilepicture as $row)
{
echo '<img src='.base_url().'../'.$row->Image_Path.' style="Height: 300px; Width:200px;"><br>';
} ?>
<form action=" <?php echo base_url().'Profile';?>" method='post'>
<input type='submit' value='Upload Picture'>
</form>
<div class = 'aboutme'>
<p>About Me</p>
</div>
<div class = 'logout'>
<form action= '<?php echo base_url().'Profile/logout';?>' method='post'>
<input type="submit" value="Log Out">
</form>
</div>
<div class = 'checkout'>
<form action = '<?php echo base_url().'Cart/display_cart';?>' method='post'>
<input type='submit' value='Check Out'>
</form>
</div>
<div class = 'shopbutton'>
<form action = '<?php echo base_url().'Product/display_products';?>' method='post'>
<input type='submit' value='Shop'>
</form>
</div>
</body>
</html>
與退出標誌它什麼也沒有顯示,但我沒有看到最後一個查詢只是獲取數據庫中的第一個用戶圖片,當我嘗試將$ id變量傳遞給控制器時,它將作爲NULL出現。如果我通過特定的User_ID說27,它確實顯示該用戶行的圖片。 – user3412695
我想這條線:$ this-> db-> where('User_ID',$ id); – user3412695
會將用戶標識分配給$ id。 (我討厭這裏評論) – user3412695