Hi @benanamen how do I do it? Sorry never came across this. Do I need to group the building IDs first and then execute a foreach loop and a new mysql query?
PM me an SQL Dump of your DB with at least a few sample records. Also, a description of the real problem you are trying to solve will be helpful. What you have provided is your attempted solution to the real problem. What I am asking for is a high level overview of what you are doing.
If you are reading the data from the database then creating the array from that data and you are now asking how to further process the data then it will be better to do it all in SQL if possible and I think that is what benanamen is asking. I know that for about two decades I have seen developers attempting to solve a fundamental problem (real problem as benanamen says) and then they ask for help with the solution they have thought of. Often there are better solutions to the fundamental problem.
But to answer the question as asked, I think it is simply a matter of creating an array with the building id as the key and another array as the value.
Hi @SamuelCalifornia thanks for your replay, I really appreciate the support and advices I get in this forum. I agree with you that sometimes there are several approaches to solve the problem and some of them are better then others. I read several articles and posts online and I understand that is better to prefer mysql over php. My problem here is that I can create the html table with the datas I have from the query but I need to group them before they are rendered into the table. That is why I decided to edit the array. I tried to group them using mysql but either my database structure is not correct or is me that still haven’t understood how to use a query with group by.
OP, the first place to start is making sure your DB design is correct and then going from there. So again, I would ask that you provide an SQL dump of your DB.
Hi @benanamen thanks to your help I’ve managed to do some progress, I wasn’t aware of pivot table in mysql so I’ve done some researches and also discovered mysql stored procedures, this is my final code:
DELIMITER ;;
DROP PROCEDURE IF EXISTS pippo;;
CREATE PROCEDURE pippo()
BEGIN
SELECT
GROUP_CONCAT(
CONCAT("MAX(IF(km_kondomanager_millesimal_table_value_table_id='", km_kondomanager_millesimal_table_value_table_id, "',km_kondomanager_millesimal_table_millesimal_value ,NULL)) AS '", km_kondomanager_millesimal_table_name, "'"), "\n"
)INTO @answers
FROM (
SELECT DISTINCT km_kondomanager_millesimal_table_value_table_id, km_kondomanager_millesimal_table_name FROM km_kondomanager_millesimal_table_values INNER JOIN km_kondomanager_millesimal_table
ON km_kondomanager_millesimal_table_values . km_kondomanager_millesimal_table_value_table_id = km_kondomanager_millesimal_table. km_kondomanager_millesimal_table_id WHERE km_kondomanager_millesimal_table_value_group_id = 13
) A;
SET @query :=
CONCAT(
'SELECT km_kondomanager_millesimal_table_value_building_id, ', @answers,
' FROM km_kondomanager_millesimal_table_values WHERE km_kondomanager_millesimal_table_value_group_id = 13 GROUP BY km_kondomanager_millesimal_table_value_building_id'
);
PREPARE statement FROM @query;
EXECUTE statement;
END;;
DELIMITER ;
Now normally when I call a query in PHP I use prepared statement, the parameters in the queue are specificed by “?” and I pass them with the function bind paramenters. Is it possible to do the same with a stored procedure?