Welcome Guest, Not a member yet? Register   Sign In
Parsing data from a MySQL query
#1

[eluser]Isern Palaus[/eluser]
Hello,

I'm coding a electronic commerce system and I'm trying to optimize the code. One of the things that I hate is to "parse" (I don't know if is the best word to describe this) the data that I get from the database.

I actually have this query:
Code:
$this->db->from('producto')
                 ->select('*,producto_descripcion.nombre AS nombre, fabricante_descripcion.nombre AS fabricante')
                 ->join('producto_descripcion', 'producto.idproducto = producto_descripcion.producto_idproducto', 'left')
                 ->join('fabricante', 'producto.fabricante_idfabricante = fabricante_idfabricante', 'left')
                 ->join('fabricante_descripcion','fabricante.idfabricante = fabricante_descripcion.fabricante_idfabricante', 'left')
                 ->join('producto_has_categoria', 'producto.idproducto = producto_has_categoria.producto_idproducto', 'left')
                 ->where('producto_descripcion.idioma_ididioma', $idioma)
                 ->where('producto.borrado', 0);
            
        $this->db->get();

And I want to get the data without having to do some like this (inside the foreach, of course):
Code:
$data[$row->id]['name'] = $row->name;
$data[$row->id]['price'] = $row->price;
...

Looking at the forum and some controllers/models that the people uploaded I've seen this way:
Code:
$productos[$row->idproducto] = $row;

With this, I get the same that if I parse manually but I've one problem... I want to attach another query (and not using join). Like:
Code:
$data[$row->id]['child']['name'] = $row->name;
$data[$row->id]['child']['description'] = $row->description;

There is a way to do it? I tried:
Code:
$productos[$row->idproducto] = $row;
                
                $query = $this->_getPrecios($id, $idioma);

                if($query->num_rows() > 0)
                {
                    foreach($query->result() as $row)
                    {
                        $child_id = $row->idcategoria;
                        
                        $productos[$row->idproducto][] = $row;
                    }
                }

But didn't works...

Thank in advance,
Isern
#2

[eluser]Jondolar[/eluser]
You can skip using the Active Record class and just use the database class and use result_array() to retrieve each row as an array instead of as an object.

Code:
foreach($query->result_arary() as $row)
                    {
                        $child_id = $row->idcategoria;
                        
                        $productos[$row->idproducto][] = $row;
                    }




Theme © iAndrew 2016 - Forum software by © MyBB