2016-10-20 11 views
0

私はクラスのためにEコマースサイトを作成中です。私のプロフィールページでは、dbに保存されているさまざまな情報を表示する予定です。今、私はユーザーの写真をばかげているだけです。私はDBの 'ユーザー'の列 '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> 

答えて

-1

まず、クエリの選択部分を変更してみます。それはまだ動作しない場合は、に私のモデルの機能を変更get_single_profile()

echo $this->db->last_query(); 
exit; 
+0

終了記号が付いていて何も表示されていない場合、$ id変数をコントローラにNULLとして渡すときに、最後のクエリがデータベース内の最初のユーザーの画像しか取得していないことがわかります。私が特定のUser_ID 27を渡すと、そのユーザー行の画像が表示されます。 – user3412695

+0

私はこの行を考えました:$ this-> db-> where( 'User_ID'、$ id); – user3412695

+0

は、ユーザーIDを$ idに割り当てます。 (私はここでコメントするのが嫌いです) – user3412695

0

であなたの最後のクエリをエコーし​​よう

$this->db->select('Image_Path, Username');

::のようにそれがあるべき

public function get_single_profile($id){ 
    //get whole table 


    $this->db->select('Image_Path'); 
    $this->db->from('users'); 
    $this->db->where('Username', $id); 
    $query = $this->db->get(); 
    echo $this->db->last_query(); 
    return $query->result(); 

}//終了get_single_profile

とMy Controller Inde xに:モデルで

public function index() 
    { 

     if($this->session->userdata('logged_in')) 
     { 
      $data['profilepicture'] =  $this->Profile_Model->get_single_profile($this->session->userdata('logged_in') ); 

       $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 

私はセッションに$ IDを設定するために、以前に定義されたセッション変数を使用し、コントローラでは、テーブルからユーザー名を変更します$ idに設定し...私は

を考えます
関連する問題