Jump to content

MySql into to php array issue


gmc1103

Recommended Posts

Hello

Can somepne help me with this issue

I have a query who is returning some records

ID        disciplina                                moduloUfcd             idcpDisciplinas                         anoTurma 

58,      Comunicação Visual,                 8599,                                   49,                                           11
59,      Comunicação Visual,                 133,                                     49,                                           11

60,      Comunicação Visual,                 134,                                     49,                                           10

When i trying to put this into an array with just one line (merge all with number 49 (idcpDisciplinas) and (anoTurma) 11) i can't get the desired output

 

My sql query is this one

select c.idPlan, cD.disciplina, cM.moduloUfcd, p.Nome, t.Turma, a.Ano, c2.curso,
                               c.validCron, c.cronograma, c.dataLimite, c.idProfessor,cM.horas,cM.tempos,
                               c.pdfCronograma, c2.curso, cM.discModulo, cM.idcpDisciplinas, cM.ano as anoTurma 
                                from cpDiscProfessores c
                                         inner join cpDisciplinas cD on c.idcpDisciplinas = cD.idCpDisciplinas
                                         inner join cpModulos cM on c.idCpModulos = cM.idCpModulos
                                         inner join professores p on c.idProfessor = p.idProfessor
                                         left join turmas t on c.idTurma = t.idTurma
                                         inner join anosescolares a on c.idAnoEscolar = a.idAnoEscolar
                                         left join cursos c2 on c.idCursos = c2.idCursos
                                where a.Estado = 1;

 

And in php what i have is this

 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $final = array();
        $json = array();
        foreach ($result as $row) {
            $moduloUfcd= $row['moduloUfcd'];
            if (!isset($moduloUfcd[$moduloUfcd])) {
                $horas= $row['horas'];
                $final[$moduloUfcd]['idPlan'] = $row['idPlan'];
                $final[$moduloUfcd]['disciplina'] = $row['disciplina'];
                $final[$moduloUfcd]['Nome'] = $row['Nome'];
                $final[$moduloUfcd]['Turma'] = $row['Turma'];
                $final[$moduloUfcd]['anoTurma'] = $row['anoTurma'];
                $final[$moduloUfcd]['curso'] = $row['curso'];
                $final[$moduloUfcd]['validCron'] = $row['validCron'];
                $final[$moduloUfcd]['cronograma'] = $row['cronograma'];
                $final[$moduloUfcd]['dataLimite'] = $row['dataLimite'];
                $final[$moduloUfcd]['idProfessor'] = $row['idProfessor'];
                $final[$moduloUfcd]['pdfCronograma'] = $row['pdfCronograma'];
                $final[$moduloUfcd]['discModulo'] = $row['discModulo'];
                $final[$moduloUfcd]['idcpDisciplinas'] = $row['idcpDisciplinas'];
                $final[$moduloUfcd]['moduloUfcd'] = array();

            }
            $final[$moduloUfcd]['moduloUfcd'][] = $row['moduloUfcd'];
            $final[$moduloUfcd]['horas'][] = $row['horas'];
            $final[$moduloUfcd]['tempos'][] = $row['tempos'];


        }
        foreach ($final as $moduloUfcd => $cron) {

            $json[] = $cron;
        }

        echo json_encode($json);

What it gives is :

[
{
idPlan: "60",
disciplina: "Comunicação Visual",
Nome: "XXXXXXXXX",
Turma: "11ºO",
anoTurma: "11",
curso: "Técnico de Audiovisuais",
validCron: "-1",
cronograma: "0",
dataLimite: "2020-10-30",
idProfessor: "168",
pdfCronograma: "/XXXXX/pdf/profissionais/cronogramas/Cronograma -Técnico de Audiovisuais-8599.pdf",
discModulo: "8599",
idcpDisciplinas: "49",
moduloUfcd: [
"8599"
],
horas: [
"25"
],
tempos: [
" 34 "
]
}
]

 

Link to comment
Share on other sites

If you are starting with this (which could be the result from a table subquery) ...

+----+----------------------+------------+-----------------+----------+
| id | disciplina           | moduloUfcd | idcpDisciplinas | anoTurma |
+----+----------------------+------------+-----------------+----------+
| 58 | Comunicação Visual   |       8599 |              49 |       11 |
| 59 | Comunicação Visual   |        133 |              49 |       11 |
| 60 | Comunicação Visual   |        134 |              49 |       10 |
+----+----------------------+------------+-----------------+----------+

then this query ...

SELECT group_concat(id separator ', ') as ids
     , disciplina
     , group_concat(moduloUfcd separator ', ') as mods
     , idcpDisciplinas
     , anoTurma
FROM gmc
GROUP BY idcpDisciplinas, anoTurma;

gives ...

+--------+----------------------+-----------+-----------------+----------+
| ids    | disciplina           | mods      | idcpDisciplinas | anoTurma |
+--------+----------------------+-----------+-----------------+----------+
| 60     | Comunicação Visual   | 134       |              49 |       10 |
| 58, 59 | Comunicação Visual   | 8599, 133 |              49 |       11 |
+--------+----------------------+-----------+-----------------+----------+

 

  • Great Answer 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.