在代码点火器中使用user_id获取用户产品



嘿伙计们,我正在 codeigniter 中制作一个网站,我正在尝试为另一个用户配置文件制作页面。在另一个用户的个人资料中,我想显示该用户上传的所有产品。我试图像您自己的用户配置文件一样执行此操作,但是我正在使用会话user_id。

这就是我现在拥有的:

控制器功能:

public function userdetails($user_id)
 {
  //load the model
  $this->load->model('User_model');

  $data['userdetail_list'] = $this->User_model->getdata($user_id);
  $data['products'] = $this->User_model->userproducts();
  $this->load->view('profiel_user',$data);
 }

模型中的功能:

public function userproducts($user_id)
{
    $this->db->where('user_id',$user_id);
    $query = $this->db->get('products');
    if($query->num_rows()>0)
    {
       return $query->result_array();
    }
    return array();
}

因此,当我在用户个人资料页面上时,我想显示属于我点击user_id个人资料的所有产品。

我也已经有了这个功能来获取我所在的个人资料页面的user_id:

function getdata($user_id){
  $this->db->select("*"); 
  $this->db->from('users');
  $this->db->where('user_id', $user_id);
  $query = $this->db->get();
  return $query->result();
 }

有人知道我该怎么做吗?谢谢

数据库信息:

表1:产品:

product_id
product_naam
product_beschrijving
user_id
ophaal_plaats
product_foto
product_foto_thumb
date_created
date_updated
category_id
table 2 : users:
user_id
email
voornaam
achternaam
beschrijving
profiel_foto

您可以按如下方式修改函数

function getdata($user_id){
  $this->db->select("products.*,users.*"); 
  $this->db->from('products');
  $this->db->join('users','users.user_id = products.user_id','left');
  $this->db->where('users.user_id', $user_id);
  $query = $this->db->get();
  return $query->result();
 } 

创建一个类似的函数

public function product_other_user_id($user_id)
      {
          $this->db->where('user_id', $user_id);
          return $this->db->get('products')->result();
      }

然后在控制器中调用该函数,例如user_id

$userProducts = product_other_user_id($user_id);

当您返回时,将用户及其产品组合成一个阵列或关联数组,无论您觉得舒服。

最新更新