Home » Php » php – table(s) structure to obtain a specific array format

php – table(s) structure to obtain a specific array format

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am currently designing a db schema to obtain an array like this one:

[{
                    "name": "Sprint 0",
                    "desc": "Analysis",
                    "values": [{
                        "from": "/Date(1320192000000)/",
                        "to": "/Date(1322401600000)/",
                        "label": "Design",
                        "customClass": "ganttGreen"
                    }]
                },{
                    name: " ", //this is again Sprint 0 so make it empty not to display repetitive data
                    desc: "Scoping",
                    values: [{
                        from: "/Date(1322611200000)/",
                        to: "/Date(1323302400000)/",
                        label: "Scoping",
                        customClass: "ganttRed"
                    }]
                },{
                    name: "Sprint 1",
                    desc: "Development",
                    values: [{
                        from: "/Date(1323802400000)/",
                        to: "/Date(1325685200000)/",
                        label: "Development",
                        customClass: "ganttGreen"
                    }]
                },{
                    name: " ",
                    desc: "Showcasing",
                    values: [{
                        from: "/Date(1325685200000)/",
                        to: "/Date(1325695200000)/",
                        label: "Showcasing",
                        customClass: "ganttBlue"
                    }]
                },{
                    name: "Sprint 2",
                    desc: "Development",
                    values: [{
                        from: "/Date(1326785200000)/",
                        to: "/Date(1325785200000)/",
                        label: "Development",
                        customClass: "ganttGreen"
                    }]
                }]

It is a collection of tasks to be displayed. It is a reverse engineering exercise since I am using a plugin to display data and it expects this structure of data.
The data structure would be like that:
foreach “name” you will have multiple “desc” values and for each desc you can have multiples “values”

What would be the most efficient table(s) structure to obtain this doing as much as possible on mysql side? (I think that the part where I replace with empty the “name” value if it is the same as the previuous must be on PHP)

So far I have these tables

GANTT_PHASES
    id  nome                progetto
---------------------------------------
    1   Setup di progetto   1
    2   Blueprint           1
    3   Realizzazione       1
    4   Test                1
    5   Go Live             1

and this one

GANTT   
id  phaseID date_to                 date_from            ordine descr   label   customClass progetto
------------------------------------------------------------------------------------------------------
1   1       2018-10-05 02:00:00     2018-10-03 02:00:00     1   pippo   pippo1  ganttGreen  1
2   1       2018-10-05 02:00:00     2018-10-03 02:00:00     2   pluto   pippo1  ganttGreen  1

the query:

SELECT nome, descr, label, customClass, date_from, date_to 
FROM `gantt_fasi` LEFT JOIN gantt on gantt_fasi.id= gantt.phaseID 
WHERE gantt_fasi.progetto=1 

will produce the following output:

nome                descr   label   customClass date_from               date_to     
--------------------------------------------------------------------------------------------
Setup di progetto   pippo   pippo1  ganttGreen  2018-10-03 02:00:00     2018-10-05 02:00:00
Setup di progetto   pluto   pippo1  ganttGreen  2018-10-03 02:00:00     2018-10-05 02:00:00
Blueprint           NULL    NULL    NULL        NULL                    NULL
Realizzazione       NULL    NULL    NULL        NULL                    NULL
Test                NULL    NULL    NULL        NULL                    NULL
Go Live             NULL    NULL    NULL        NULL                    NULL 

and then I go down with PHP to create the array. Anything I can improve on this approach?

How to&Answers: