【问题标题】:Getting a single User Picture from sql DB (CodeIgniter)从 sql DB (CodeIgniter) 获取单个用户图片
【发布时间】:2017-03-01 23:29:01
【问题描述】:

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

这是模型

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

【问题讨论】:

    标签: php sql codeigniter phpmyadmin


    【解决方案1】:

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

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

    如果仍然无法正常工作,请尝试在 get_single_profile() 中回显您的最后一个查询

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

    【讨论】:

    • 带有退出标志它什么也没有显示,但我没有看到最后一个查询只是获取数据库中的第一个用户图片,当我尝试将 $id 变量传递给控制器​​时,它会出来作为NULL。如果我传递一个特定的 User_ID 比如说 27,它会显示该用户行的图片。
    • 我想到了这一行:$this->db->where('User_ID', $id);
    • 会将用户 ID 分配给 $id。 (我讨厌在这里评论)
    • 退出标志将终止脚本的执行。
    • 因此,如果: echo $this->db->last_query() 没有显示任何内容(它应该打印正在执行的 SQL 查询。)那么您在以下语句中做错了。 $this->db->select('Image_Path'); $this->db->from('users'); $this->db->where('用户名', $id); $query = $this->db->get();
    【解决方案2】:

    将我的模型函数更改为:

      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

    我的控制器索引到:

    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 设置为会话...我认为

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-01
      • 2017-12-20
      • 1970-01-01
      • 2012-02-27
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多