Get user products with using user_id in codeigniter

Hey guys I'm making a website in codeigniter and I'm trying to make the page for another user profile. On the profile of another user I want to show all the products that that user uploaded. I tried to do it like the same as you're own user profile but with that I'm using the session user_id.

This is what I have right now:

Controller function:

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

 }

function in model:

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

So when I'm on a user profile page, I want to show all the products that belong to the profile user_id I clicked on.

I also have this function already to get the user_id of that profile page I'm on:

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

Anyone got model code of how I can do it? thanks

database information:

Table 1 : products:

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

Upvotes: 1

Views: 587

Answers (2)

Praveen Kumar
Praveen Kumar

Reputation: 2408

You can modify your function as below

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

Upvotes: 1

Farrukh Subhani
Farrukh Subhani

Reputation: 2038

Create a similar function like this

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

then in your controller call that function after you have user_id for example

$userProducts = product_other_user_id($user_id);

When you return combine both user and their products in an array or an associate array whatever you feel comfortable with.

Upvotes: 0

Related Questions