Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] MySQL organitzation problem and results question
#1

[eluser]Isern Palaus[/eluser]
Hello,

I'm making a electronic commerce system and I'm having troubles with the tables of the database. I preferred to separate the content from the values to do it multilanguage.

So, the problem is that I need to do a loot of JOINs, like this:
Code:
private function _getProductsByCategoryId($id = 0, $activo = 1, $idioma = 1, $moneda = 1, $orden = 'nombre', $sentido = 'ASC', $total = 25, $inicio = 0)
    {
        $this->db->from('producto')
                 ->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')
                 ->join('precio', 'producto.precio_idprecio = precio.idprecio', 'left')
                 ->join('monedas', 'precio.monedas_idmonedas = monedas.idmonedas', 'left');
                
        $ordenes = array(    'nombre'         =>    'producto_descripcion.nombre',
                            'precio'        =>    'precio.valor',
                            'fabricante'    =>    'fabricante_descripcion.nombre',
                            'referencia'    =>     'producto.referencia');
        
        if(in_array($orden, $ordenes))
            $this->db->order_by($orden, $sentido);
        else
            $this->db->order_by($ordenes['nombre'], $sentido);
            
        $this->db->where('producto.activo', $activo)
                 ->where('producto_has_categoria.categoria_idcategoria', $id)
                 ->where('producto_descripcion.idioma_ididioma', $idioma)
                 ->where('monedas.idmonedas', $moneda)
                 ->where('producto.borrado', 0);
                
        $this->db->distinct();
                
        $this->db->limit($total, $inicio);
            
        return $this->db->get();
    }

I'm using the tables: producto, producto_descripcion, fabricante, fabricante_descripcon, producto_has_categoria, precio, monedas.

The problem is that producto_descripcion, fabricante_descripcion and monedas have a value called 'name' and I don't know how to manage it and get the three values for the three entries in the database without having to modify the databases.

The other question is how to get the results. This produces a lot of results for every entry because I select all. The problem is that I don't want to use a foreach and set every row like $result['name'] = $row->name; , $result['date'] = $row->date; ... and I want to know if there is something to use and array. Actually I'm doing but this it only works for 1 result.

Thank you in advance.

Regards,
Isern
#2

[eluser]rogierb[/eluser]
use "SELECT fieldname as some_name_you_want_to_give_it"

Code:
$this->db->select('producto_descripcion.name as producto_descripcion_name,fabricante_descripcion.anem as fabricante_descripcion_name')

// echo $row->producto_descripcion_name;
#3

[eluser]Isern Palaus[/eluser]
[quote author="rogierb" date="1257534374"]use "SELECT fieldname as some_name_you_want_to_give_it"

Code:
$this->db->select('producto_descripcion.name as producto_descripcion_name,fabricante_descripcion.anem as fabricante_descripcion_name')

// echo $row->producto_descripcion_name;
[/quote]

Hello rogierb,

Thank you for your answer. I'll try it now. By the way, this will not select *... no? I will have to select now all fields that I want, true?

Thank you
Isern
#4

[eluser]rogierb[/eluser]
you can use:
Code:
$this->db->select('producto_descripcion.*, producto_descripcion.name as producto_descripcion_name');
$this->db->select('fabricante_descripcion.*, fabricante_descripcion.name as fabricante_descripcion_name');
if you want to select all fields from a table.
#5

[eluser]Isern Palaus[/eluser]
[quote author="rogierb" date="1257541073"]you can use:
Code:
$this->db->select('producto_descripcion.*, producto_descripcion.name as producto_descripcion_name');
$this->db->select('fabricante_descripcion.*, fabricante_descripcion.name as fabricante_descripcion_name');
if you want to select all fields from a table.[/quote]

Ohhh!!! Thank you!

This is very useful! Big Grin

Regards,
Isern
#6

[eluser]Isern Palaus[/eluser]
Hello,

Rogierb, I've tried what you said and I'm getting a MySQL error.

Quote:A Database Error Occurred
Error Number: 1054

Unknown column 'fabricante_descripcion.name' in 'field list'

SELECT DISTINCT `fabricante_descripcion`.`name` as fabricante_descripcion_name, `monedas`.`name` as monedas_name, `producto`.*, `producto_descripcion`.*, `fabricante`.*, `fabricante_descripcion`.*, `producto_has_categoria`.*, `precio`.*, `monedas`.* FROM (`producto`) LEFT JOIN `producto_descripcion` ON `producto`.`idproducto` = `producto_descripcion`.`producto_idproducto` LEFT JOIN `fabricante` ON `producto`.`fabricante_idfabricante` = `fabricante_idfabricante` LEFT JOIN `fabricante_descripcion` ON `fabricante`.`idfabricante` = `fabricante_descripcion`.`fabricante_idfabricante` LEFT JOIN `producto_has_categoria` ON `producto`.`idproducto` = `producto_has_categoria`.`producto_idproducto` LEFT JOIN `precio` ON `producto`.`precio_idprecio` = `precio`.`idprecio` LEFT JOIN `monedas` ON `precio`.`monedas_idmonedas` = `monedas`.`idmonedas` WHERE `producto`.`activo` = 1 AND `producto_has_categoria`.`categoria_idcategoria` = '1' AND `producto_descripcion`.`idioma_ididioma` = '2' AND `monedas`.`idmonedas` = 1 AND `producto`.`borrado` = 0 ORDER BY `producto_descripcion`.`nombre` ASC LIMIT 25

Code:
private function _getProductsByCategoryId($id = 0, $activo = 1, $idioma = 1, $moneda = 1, $orden = 'nombre', $sentido = 'ASC', $total = 25, $inicio = 0)
    {
        $this->db->from('producto')
                 ->select('fabricante_descripcion.name as fabricante_descripcion_name,monedas.name as monedas_name')
                 ->select('producto.*, producto_descripcion.*, fabricante.*, fabricante_descripcion.*, producto_has_categoria.*, precio.*, monedas.*')
                 ->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')
                 ->join('precio', 'producto.precio_idprecio = precio.idprecio', 'left')
                 ->join('monedas', 'precio.monedas_idmonedas = monedas.idmonedas', 'left');
                
        $ordenes = array(    'nombre'         =>    'producto_descripcion.nombre',
                            'precio'        =>    'precio.valor',
                            'fabricante'    =>    'fabricante_descripcion.nombre',
                            'referencia'    =>     'producto.referencia');
        
        if(in_array($orden, $ordenes))
            $this->db->order_by($orden, $sentido);
        else
            $this->db->order_by($ordenes['nombre'], $sentido);
            
        $this->db->where('producto.activo', $activo)
                 ->where('producto_has_categoria.categoria_idcategoria', $id)
                 ->where('producto_descripcion.idioma_ididioma', $idioma)
                 ->where('monedas.idmonedas', $moneda)
                 ->where('producto.borrado', 0);
                
        $this->db->distinct();
                
        $this->db->limit($total, $inicio);
            
        return $this->db->get();
    }

I've tried to put only $this->db->select('monedas.name as monedas_name') and it shows the same error (with monedas.name). How I can solve this?

Thank you another time,
Isern
#7

[eluser]Isern Palaus[/eluser]
I've found the solution:

Code:
$this->db->select('*,producto_descripcion.nombre AS nombre, fabricante_descripcion.nombre AS fabricante, monedas.nombre AS mnombre')

Thanks for all,
Isern




Theme © iAndrew 2016 - Forum software by © MyBB