Codecraker
Codecraker

Reputation: 337

Unable to join and get the data from database in Codeigniter

I want to join three table and get the data from the function but unable to do so. I am able to get the category joint data but not the product line data.

I have three tables. One is cv_products second one is cv_category and last one is product_line . I have a table field in cv_products named product_line.

This is my function in the model

public function get_products($id = 0, $slug = FALSE){
if($id === 0 && $slug === FALSE){

$this->db->order_by('cv_products.id', 'DESC');

$this->db->select('cv_products.*, cv_category.name as categoryname', 'product_line.name as linename','cv_category.id as category_id' );

$this->db->join('cv_category','cv_category.id = cv_products.category', 'left');

$this->db->join('product_line','product_line.id = cv_products.product_line', 'left');

$query= $this->db->get('cv_products');

return $query->result_array();

}
$this->db->select('cv_products.*, cv_category.name as categoryname', 'product_line.name as linename','cv_category.id as category_id' );

$this->db->join('cv_category','cv_category.id = cv_products.category', 'left');

$this->db->join('product_line','product_line.id = cv_products.product_line', 'left');

$query= $this->db->get_where('cv_products', array('cv_products.id' => $id, 'slug' => $slug));

return $query->row_array();



}

In view it giving an error like Severity: Notice Message: Undefined index: linename.

as I have a field name in both cv_products, cv_category and product_line so at the time of selection i've given product_line.name as linename. It's alright with the cv_category joint but unable to get the same in case of product_line joint time.

Upvotes: 0

Views: 24

Answers (1)

Simon K
Simon K

Reputation: 1523

The quotes seem to be a little off in your select call.

Change this:

$this->db->select('cv_products.*, cv_category.name as categoryname', 'product_line.name as linename','cv_category.id as category_id' );

To this:

$this->db->select('cv_products.*, cv_category.name as categoryname, product_line.name as linename,cv_category.id as category_id' );

Notice that I open the quotation at the start of the select statement and close it at the end. individual fields to not need to be quoted as $this->db->select() then potentially treats them as function parameters.

Repeat this approach for the second select statement in your function also.

Let me know if it works for you.

Upvotes: 1

Related Questions