从sql DB获取单个用户图片(CodeIgniter)

时间:2016-10-20 05:47:03

标签: php sql codeigniter phpmyadmin

所以我正在为一个班级创建一个电子商务网站。在我的个人资料页面上,我打算显示存储在数据库中的各种信息。现在我只是弄乱了用户照片。我在模型中有一个函数,它从DB'users'中的'Image_Path'列中获取Users照片。我的问题是,对于每个配置文件视图,它显示相同的照片。我认为它返回整个图片阵列,也许只是将它设置为第一个?

这是模型

<?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>

2 个答案:

答案 0 :(得分:2)

首先,尝试更改查询的选择部分。应该是这样的:

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

如果仍无效,请尝试在get_single_profile()

中回显您的上一个查询
echo $this->db->last_query();
exit;

答案 1 :(得分:1)

Changed my Model function to:

  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();

}//end get_single_profile

And My Controller Index To:

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

in the model I changed the Username from the table and set it to $id, In the controller I used a previously defined session variable to set $id to the session...I think