2
Good afternoon,
I have a system, with the following tables :
pagina: id,nome ,icone, idGrupo.
paginaGrupo: id,nome,icone, idGrupoUser.
I’m setting up a dynamic menu on top of these tables. I take the id of the groups that the user is part of, and play in the query to get the Groups of pages and the pages that he will have access to.
The query is as follows:
SELECT gp.id as idG,gp.nome grupo,gp.uri uriG,gp.ordemMenu,gp.icone as iconeG,'
. 'p.id,p.name as pagina,p.uri uriP ,p.icon as iconeP from tblPaginas as p '
. 'inner join tblGrupoPaginas as gp '
. 'on (p.idGrupo = gp.id) '
. 'where gp.idGrupoAcesso IN('.implode(",",$this->getUserGrupoId() ).') '
. 'order by gp.ordemMenu ASC
So I get this return:
Array (
[0] => Array (
[idG] => 1
[grupo] => Monitoria de vendas
[uriG] => monitoria-de-vendas
[ordemMenu] => 1
[iconeG] =>
[id] => 14
[pagina] => agenda
[uriP] => agenda
[iconeP] =>
)
[1] => Array (
[idG] => 1
[grupo] => Monitoria de vendas
[uriG] => monitoria-de-vendas
[ordemMenu] => 1
[iconeG] =>
[id] => 15
[pagina] => paginas
[uriP] => paginas
[iconeP] =>
)
[....]
[45] => Array (
[idG] => 7
[grupo] => Ferramentas
[uriG] => ferramentas
[ordemMenu] => 8
[iconeG] =>
[id] => 10
[pagina] => equipamentos
[uriP] => equipamentos
[iconeP] =>
)
In the array, there is the field 'idG' ( id of the group ). I would like the return to be from a multidimensional array above the value of the 'idG' field'.
If you have no way to return by query, how could you create this array by PHP ?
From now on, thank you.
I guess that answers your question: php array group
– João Pinho
you use
PDO
orMysqli
?– novic
@Virgilionovic , sorry for the delay, I use PDO.
– Henrique Felix
@Joãopinho, man, worked out by the link you sent. I edit my question and put the answer or you answer here ?
– Henrique Felix
@Henriquefelix has a shape with PDO is in my view the correct!
– novic
@Virgilionovic , opa how it would be ? Currently I do so: public Function getMenuPaginas(){ Try{ $sql = 'Cod sql'; $stmt = Connection::getInstance()->prepare($sql); $stmt->execute(); if($stmt->rowCount() >= 1){ $line = $stmt->fetchAll(PDO::FETCH_ASOC); $result = array(); foreach ($line as $data) {...} Return $result; }Else { ... } } catch (Exception $ex) { ... } }// END FUNCTION
– Henrique Felix
Look I reflected my answer in a very basic example I believe you’ll understand! @Henriquefelix, if you don’t ask.
– novic