Good morning!
1 °) Your "Product" table has identical duplicate products. That is, if you had 20,000 blue Bic pens, there should be no 20,000 records with the same thing "blue Bic pens". For example, there are 2 records with the same name "Wheel", "Toyota" mark and code "0001". The correct thing is to have a "quantity" column instead of doubling the product.
2 °) The "Brand" column in the "Product" table is denormalized. That is, there should be a "Brand" table with columns "code" and "brand name", and in the table "Product" only the brand code, without name. And it is on top of this implementation that the answer below rests.
3 °) The "product" column of the "Product" table should be called "name". And it is on top of this implementation that the answer below rests.
4 °) Evaluate count($marca) > 0
is incorrect because $marca
is not array
, but is a string coming from a post.
Asked by the question is an issue to be solved in the model. In the controller it is only necessary to sanitize the brand code before passing it on to the model.
No controller:
<?php
...
$id_marca = intval($this->input->post('marca'));
$array_produtos = this->produto_model->get_produtos_by_marca($id_marca);
// Só para visualizar o retorno
print_r($array_produtos);
...
?>
And in the model you make the filter:
<?php
...
public function get_produtos_by_marca($id_marca) {
$this->db->select('produto_id, nome');
$query = $this->db->get_where('produto', array('id_marca' => $id_marca));
// Ou volta um array com resultados ou um array vazio
return $query->num_rows() ? $query->result_array() : array();
}
...
?>