title: CodeIgniter - Search function (products->category_id database relationship)
body: <p>I have this code:</p>
  
  <pre><code>function search($terms){
      $query      = $this-&gt;db-&gt;get_where('products',"name LIKE '%$terms%' OR description LIKE '%$terms%'");
      $product    = $query-&gt;result_array();
  
      foreach($product as $p):            
          $query2 = $this-&gt;db-&gt;get_where('categories', array('cat_id' =&gt; $p['prod_category']));
          $category   = $query2-&gt;row_array();
  
          $product['category'] = $category['cat_link'];
      endforeach;
  
      return $product;
  }
  </code></pre>
  
  <p>in my search function.  It will return something like:</p>
  
  <pre><code>Array(
  [0] =&gt; Array(
     [prod_id] =&gt; 5
     [prod_name] =&gt; Product
     [prod_category] =&gt; 1
  )
  </code></pre>
  
  <p>But what I'm after is:</p>
  
  <pre><code>Array(
  [0] =&gt; Array(
     [prod_id] =&gt; 5
     [prod_name] =&gt; Product
     [prod_category] =&gt; Category1
  )
  </code></pre>
  
  <p>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?</p>