So I have in my database a table called "Clients"
and an other one called "Pets"
.
They are linked by an associative table which means that a "Clients"
can have two "Pets"
.
I am using a FOREACH
loop to display my client and pets but when a client have many pets, my foreach loop print everything, as many as the client have pets.
here is my foreach
loop
JavaScript
x
foreach ($files as $file) :
echo $file['address'];
endforeach
My problem is that I want to have everything diplayed normally except the "pets"
(<?= $file['petName']; ?>
) and see that the client have 2 pets..
Thanks for your help !
My SQL query
JavaScript
SELECT *
FROM clients
LEFT JOIN owners ON owners.idClients = clients.idClients
LEFT JOIN pets ON pets.idPets = owners.idPets
LEFT JOIN clientsdocs ON clientsdocs.idClients = clients.idClients
WHERE clients.idClients='$id'
Advertisement
Answer
You can change query like:
JavaScript
SELECT
clients.*,
GROUP_CONCAT(petName) AS petsNames # implode pets names s into 1 string
FROM clients
LEFT JOIN owners ON owners.idClients = clients.idClients
LEFT JOIN pets ON pets.idPets = owners.idPets
LEFT JOIN clientsdocs ON clientsdocs.idClients = clients.idClients
GROUP BY clients.idClients # this is group all client's data in single row
WHERE clients.idClients='$id';
It’s not solution, but can can give you an idea for best solution.