Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing %like% and JOIN together in CodeIgniter
    text
    copied!<p>I have three tables: 'users', 'products', and 'product_types'. The 'users' table stores all usual data about my site users (including an <code>id</code> column); the 'products' table lists the <code>product_id</code> of the product, the <code>user_id</code> of the user who added it, and the <code>product_type</code>; the 'product_types' table has an <code>id</code> column (which corresponds with the <code>product_type</code> int value in the 'products' table), and a varchar <code>name</code> column.</p> <p>I want to perform a <code>%like%</code> search in the 'users' table so that when a user searches for product types, it shows all users who have products that match the searched product type. Here's what I'm currently using in my CodeIgniter model:</p> <pre><code>$string = urldecode($string); $this-&gt;db-&gt;select('users.*'); $this-&gt;db-&gt;from('users'); $this-&gt;db-&gt;like('users.company_name',$string,'both'); $this-&gt;db-&gt;or_like('users.address_1',$string,'both'); $this-&gt;db-&gt;or_like('users.city',$string,'both'); $this-&gt;db-&gt;or_like('users.contact',$string,'both'); $this-&gt;db-&gt;or_like('product_types.name',$string,'both'); $this-&gt;db-&gt;join('products','users.id = products.client_id'); $this-&gt;db-&gt;join('product_types','products.product_type = product_types.id'); $this-&gt;db-&gt;distinct(); $users = $this-&gt;db-&gt;get(); foreach($users-&gt;result() as $user){ // search result } </code></pre> <p>I then feed this into a foreach statement... But it's only showing the first row most of the time - other times it comes back with no results. Can anybody tell me where I'm going wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload