Search in two tables with codeigniter

1

I have the following situation:

  

WhenIselectamarcaintabelacompatibility,IwanttoListallproductsintheProducttable,whosecodeisequaltotheselectedbrandcode.

Example:WhenselectingToyotayoushouldlistCabin,andselectingMazdashouldonlyreturnCabin

I'vedonethefollowing:

$marca=$this->input->post('marca');$marca=explode(',',$marca);$query=array();if(count($marcas)>0){$i=0;foreach($compas$row){$i++;if($row!=="") 
        {
            if ($row !== "0") 
            {
                $query[] = $row;
                $this->db->where_in('marca', $query);
            }
        } 
    }
}

But with this code I can only get the products with the following table:

  

What can I program to map the two tables through the codigo field?

    
asked by anonymous 29.09.2017 / 22:13

1 answer

0

To solve your problem is simple, the code below exemplifies what you need to code:

$this->db
     ->from('compatibilidade')
     ->join('produto', 'compatibilidade.codigo=produto.codigo')
     ->where('compatibilidade.marca', $marca)
     ->select('produto.*, compatibilidade.marca')
     ->get()
     ->result_array();

Some remarks can improve the performance of this SQL

  • Create an index in the codigo fields of the two tables, for example:
  

  • Createanindexinthemarca
  

AsIdonotknowyourbusinessrulewell,howisyourschedulecouldevencreatearelationshipinthetwofields,butwhatwasexemplifiedisenoughtosolveyourproblem.

Reference: Query Builder Class

    
30.09.2017 / 00:35