I created a class for dynamic generation of tables with results coming from MySQL. The syntax I use after instantiating the class is as follows:
$table->topo('Menu:','menu','menu');
$table->Corpo('menu_sub','ID,Nome,Menu,Icones','id,nome,id_menu,icone','','','','id_menu,icone','menu','id','nome,nome');
The last fields refer to a query in a second MySQL table
The name field works fine but as you can see the field marked in red it is pulling the other fields from the main table, I have decided this by modifying this part of the code
if ($v == $z) {
echo 'Acho ->'.$z;
}else{
echo 'p'; //MUDEI ESTA PARTE AQUI DO CODIGO
}
}
Well now that comes my biggest problem I want to query on more than one table.
I thought I would sort by separating the fields by% with% of what happens I show in the following image
$table->topo('Menu:','menu','menu');
$table->Corpo('menu_sub','ID,Nome,Menu,Icones','id,nome,id_menu,icone','','','','id_menu,icone','menuicones','id,icone','menu,icone');
In short, I can only pull the results from the second table, but if I try to pull the result from more than one table, I have the problem shown in the image above, I have a duplicate result.
The class responsible for creating the PivotTable follows:
PS. This class is without the SQL responsible for (Tipo: id_menu,icone)
in second table
<?php
class Tabela{
/*
* TOPO BARRA DE NAVEGAÇÃO DA TABELA MASTER
*/
function topo($nome,$modulo,$arquivo){
echo '
<div style="padding:0 10px;"><div style="border-bottom: solid 3px #297ACC">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="92%"><strong>'.$nome.'</strong></td>
<td width="8%" align="right"><ul class="nav span7" style="margin-bottom:0px;">
<li class="pull-right"><a href="?pg=modulos/'.$modulo.'/list_'.$arquivo.'">Listar</a></li>
<li class="pull-right"><a href="?pg=modulos/'.$modulo.'/add_'.$arquivo.'">Cadastrar</a></li>
</ul></td>
</tr>
</table>
</div>
<br />
';
}
/*
* GERADOR AUTOMATICO DE TABELA
* ESTA FUNÇÃO IRA GERA UMA SQL ONDE OS DADOS VIRÃO DINAMICAMENTE
*/
function Corpo($db,$th,$campos,$where,$modulos,$files,$_campo,$_tb,$_where,$_valor){
$pag = "$_GET[pag]";
if($pag >= '1'){
$pag = $pag;
}else{
$pag = '1';
}
$maximo = '10';
$inicio = ($pag * $maximo) - $maximo;
$sql = mysql_query("SELECT * FROM $db $where LIMIT $inicio, $maximo");
/*
* AQUI FICA O CABEÇALHO DA TABELA
*/
echo
'<table border="1" width="100%" >
<tr>
';
$v = explode(',',$th);;
foreach($v as $x){
echo '<th>'.ucfirst($x).'</th>';
}
/*
* AQUI VAI O LOOP DA TABELA
*/
echo '
<th colspan="2">Ações</th>
</tr>
<tr>';
while($dados = mysql_fetch_array($sql)){
foreach(explode(',',$campos) as $v) {
$x = $dados[$v];
$d = explode(',', $_campo);
echo '<td>';
foreach ($d as $z) {
if ($v == $z) {
echo 'Acho ->'.$z;
}else{
}
}
echo 'p';
echo '</td>';
}
echo '
<td width="1"><a href="?pg=modulos/'.$modulos.'/edit_'.$files.'&id='.$dados[id].'" class="fa fa-edit"></a></td>
<td width="1"><a href="?pg=modulos/'.$modulos.'/dell_'.$files.'&id='.$dados[id].'" class="fa fa-close" style="color:#FF0000;"></a> </td>
</tr>';
}
echo '
</table>';
/*
* PAGINAÇÃO COM NUMERAÇÃO
*/
echo '<ul class="pagination">';
$sql_res = mysql_query("SELECT * FROM $db $where");
$total = mysql_num_rows($sql_res);
$paginas = ceil($total/$maximo);
$links = '5';
echo "<li><a href=\"?pg=modulos/$modulos/list_$files&pag=1\">«</a></li>";
for ($i = $pag-$links; $i <= $pag-1; $i++){
if($i >= 0){
echo "<li><a href=\"?pg=modulos/$modulos/list_$files&pag=$i\">$i</a></li>";
}
}
echo '<li class="disabled"><a href="#">'.$pag.'</a></li>';
for($i = $pag +1; $i <= $pag+$links; $i++){
if($i > $paginas){
} else {
echo "<li><a href=\"?pg=modulos/$modulos/list_$files&pag=$i\">$i</a></li>";
}
}
echo "<li><a href=\"?pg=modulos/$modulos/list_$files&pag=$paginas\">»</a></li>";
}
}
Here I leave the link to the project that I put in the GIT.