consult with Internet in codeigniter

Asked

Viewed 864 times

0

Good afternoon, folks, I’d like to ask for your help on something I’ve been racking my brain about and I can’t seem to get it right

have 4 tables

  1. products ->id | Slug | title | info1 | info2 | info3
  2. prodnutri -> idNutricion | idProducto | porcion | info2 | info3
  3. prodpreparao -> idPreparo | idProducto | info1 | info2 | info3
  4. prodingredientes -> idIngredientes | idProducto | info1 | info2 |info3

the tables will have enough information and did not want to leave all in one.. so I believe it is better to do in dividing the information to 4.

I’m trying to do in Model the query and I’m missing, I’ve tried several ways and I can’t..

I can list the products and call everything straight, this way domain.com/products/see/product name

NA MODEL / Productmodel -- my basic query where I can bring all table product data

 public function find($slug){      //
    $this->db->where('slug', $slug);
    return $this->db->get('productos')->row();
}

but when you do the Inner -- with a table, you need it to be with the 3 + tb product, I cannot and error.

public function find($slug){      //
    $this->db->where('slug', $slug);
    $this->db->select("productos.slug");
    $this->db->from('productos');
    $this->db->join('prodnutri', 'prodnutri.idNutricion = productos.slug');
    $query = $this->db->get();
    return $this->db->get('productos')->row();
}    

NO CONTROLLER / products

public function ver($slug)
{   
    $this->load->view('incluir/cabecalho');
    $data['producto'] = $this->ProductModel->find($slug);
    $this->load->view('productos/producto', $data);
    $this->load->view('incluir/rodape');
}

The problem is that I am not able to do the query in the model (I believe the controller is right! because all the data of the product tabla it brings right, but when I want to bring the data of the other Ablas it is giving error..

PHP Error was encountered Severity: Notice Message: Undefined Property: stdClass::$porcion

in view to using the vector like this

<?Php echo $producto->titulo;?> -- titulo da tb Productos
<?Php echo $producto->porcion;?> -- titulo da tb prodnutri

Can anyone help, I will be very grateful in a light to resolve the table with Inner Join..

  • tries to give a var_dump($data['producto']); after the $data['producto'] in your controller, that you see exactly what is returning.

2 answers

0

First, you’re calling twice the way get, for me it’s wrong:

$query = $this->db->get();
return $this->db->get('productos')->row();

You’re not using that first line of $query for nothing.

Second, try to give a var_dump($data['producto']); after the $data['producto'] in your controller, that you see exactly what is returning. If you actually got the search result.

Remembering that $this->db->get('products')->Row(); returns only one result and $this->db->get('products')->result(); returns an array.

Anyway var_dump() will help you.

0

Try this solution in your MODEL query method:

try {
    $query = $this->db->query("SELECT * FROM productos p ".
        "INNER JOIN prodnutri pn INNER JOIN prodpreparo pp ".
        "INNER JOIN prodingredientes pin ".
        "ON (p.id = pn.idProducto AND p.id = pp.idProducto AND p.id = pin.idProducto)");

    if ($query === false) {
        throw new Exception($this->db->error()['message'], $this->db->error()['code']);
    }
    return $query->result();

} catch (Exception $e) {
    return null;
}

The return is in array form, so you can traverse the desired form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.