I have this code:
function search($terms){
$query = $this->db->get_where('products',"name LIKE '%$terms%' OR description LIKE '%$terms%'");
$product = $query->result_array();
foreach($product as $p):
$query2 = $this->db->get_where('categories', array('cat_id' => $p['prod_category']));
$category = $query2->row_array();
$product['category'] = $category['cat_link'];
endforeach;
return $product;
}
in my search function. It will return something like:
Array(
[0] => Array(
[prod_id] => 5
[prod_name] => Product
[prod_category] => 1
)
But what I’m after is:
Array(
[0] => Array(
[prod_id] => 5
[prod_name] => Product
[prod_category] => Category1
)
from the name of my category with the id 1. This is in my ‘categories’ table. The foreach loop is not the right way to accomplish this, but what is?
You could simplify your search logic by letting your database handle the joining of your tables.
This is assuming your category name column is named
cat_name. The results would be as follow :Hope this helps!