J
I think there are several questions in one.In this answer I will try to provide a solution that can be suitable for what you want to do.The answer focuses only on how to consult the data. From that result, you can work them in PHP to organize them as you want.The consultation would be more or less (I do not include all fields for reasons of brevity, the intention is that you understand the concept):SELECT
cl.cliente,
GROUP_CONCAT(CONCAT_WS('_',ar.descripcionArticulo, cc.costo) SEPARATOR '|') articulos
FROM
carrito_20170831 ca
INNER JOIN cliente_20170831 cl ON ca.idCliente=cl.idCliente
INNER JOIN contenido_carrito_20170831 cc ON ca.idCarrito=cc.idCarrito
INNER JOIN articulo_20170831 ar ON cc.idArticulo=ar.idArticulo
WHERE ca.idCarrito='C1'
GROUP BY ca.idCarrito;
Here we consult the data of the cart id equals C1.We will obtain in this case the data as follows:cliente articulos
Pedro Artículo 1_100.99|Artículo 5_85.10|Artículo 3_23.99
If you do two explode correct column articulos (first on | and inside that explode on _, you will be able to like this:Cliente: Pedro
Carrito: C1
-- Demás datos
Artículos:
Descripción Costo
Articulo 1 100.99
Artículo 5 85.10
Artículo 3 23.99
Applying CSS rules and reading the data arrangement that results from the database, you can differentiate by client, by id from cart or whatever you want... but that would be part of another question, so I said it was very wide. However, it's not that complicated you want.Here I leave a full demo with a partial reproduction of your tables, you can test the code. At least we take a first step: resolve the SQL query. I hope it serves you. http://rextester.com/edit/TVOF35144 CREATE TABLE IF NOT EXISTS carrito_20170831 (
idCarrito VARCHAR(20) NOT NULL PRIMARY KEY,
idCliente VARCHAR(20) NOT NULL);
CREATE TABLE IF NOT EXISTS contenido_carrito_20170831 (
idContenidoCarrito INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
idCarrito VARCHAR(20) NOT NULL,
idArticulo VARCHAR(20) NOT NULL,
costo DECIMAL(10,2) NOT NULL);
CREATE TABLE IF NOT EXISTS articulo_20170831 (
idArticulo VARCHAR(20) NOT NULL PRIMARY KEY,
descripcionArticulo VARCHAR(150) NOT NULL);
CREATE TABLE IF NOT EXISTS cliente_20170831 (
idCliente VARCHAR(20) NOT NULL PRIMARY KEY,
cliente VARCHAR(20) NOT NULL);
INSERT INTO carrito_20170831 (idCarrito, idCliente)
VALUES
('C1','CL1'),
('C2','CL2'),
('C3','CL3')
;
INSERT INTO contenido_carrito_20170831 (idCarrito, idArticulo, costo)
VALUES
('C1','A1',100.99),
('C1','A5',85.10),
('C1','A3',23.99),
('C2','A2',40.00),
('C2','A4',33.30)
;
INSERT INTO articulo_20170831 (idArticulo, descripcionArticulo)
VALUES
('A1','Artículo 1'),
('A2','Artículo 2'),
('A3','Artículo 3'),
('A4','Artículo 4'),
('A5','Artículo 5')
;
INSERT INTO cliente_20170831 (idCliente, cliente)
VALUES
('CL1','Pedro'),
('CL2','Santiago'),
('CL3','Juan')
;
SELECT
cl.cliente,
GROUP_CONCAT(CONCAT_WS('_',ar.descripcionArticulo, cc.costo) SEPARATOR '|') articulos
FROM
carrito_20170831 ca
INNER JOIN cliente_20170831 cl ON ca.idCliente=cl.idCliente
INNER JOIN contenido_carrito_20170831 cc ON ca.idCarrito=cc.idCarrito
INNER JOIN articulo_20170831 ar ON cc.idArticulo=ar.idArticulo
-- WHERE ca.idCarrito='C1'
GROUP BY ca.idCarrito;
Resultado:cliente articulos
Pedro Artículo 3_23.99|Artículo 1_100.99|Artículo 5_85.10
Santiago Artículo 2_40.00|Artículo 4_33.30