How can I use a foreach
for this situation?
I would be selecting an element of the tabela dois
- event - and for each event, get the tabela um
data that also have the same event in its structure.
By the way, when asking the related vi question about INNER JOIN
, would that be a path?
Update-Nowitworks!:)
FirstwassomedetailsabouttheHTMLtable.
Iwasusing<thread>
butit<thead>
andthe<th>
ofthetitleoutofa<tr>
doesnotwork,Istartedusing<h>
.
Nowthechangesthatreallymattertologic.
Ichangedthefetch()loopstructuretofetchAll().ThedifferenceisthatbeforeIwascreatingasimplearrayandIwasnotgoingthroughalltheelementsinthetimetofillthetables.WithFetchAllIpassedallinformationfromtheEntriestabletoavariable-justlikeinthe@Pauloexample.
AndtheIf
conditionnowworksfine,takingthesamenamedeventsandloopingforeach.
MySQL
CREATEDATABASEExercicio;USEExercicio;CREATETABLEIFNOTEXISTSeventos(IDSMALLINTAUTO_INCREMENTPRIMARYKEY,eventoVARCHAR(50)NOTNULL);INSERTINTOeventos(evento)VALUES('um');INSERTINTOeventos(evento)VALUES('dois');INSERTINTOeventos(evento)VALUES('tres');CREATETABLEIFNOTEXISTSingressos(IDSMALLINTAUTO_INCREMENTPRIMARYKEY,nomeVARCHAR(50)NOTNULL,mesaVARCHAR(50)NOTNULL,eventoVARCHAR(50)NOTNULL);INSERTINTOingressos(nome,mesa,evento)VALUES('fulano','30','um');INSERTINTOingressos(nome,mesa,evento)VALUES('fulana','35','um');INSERTINTOingressos(nome,mesa,evento)VALUES('ciclano','10','dois');INSERTINTOingressos(nome,mesa,evento)VALUES('ciclana','31','dois');INSERTINTOingressos(nome,mesa,evento)VALUES('beltrano','60','tres');INSERTINTOingressos(nome,mesa,evento)VALUES('beltrana','35','tres');
HTML
andPHP
<?php
include 'conexao.php';
$ingressos = $con->prepare("SELECT ingressos.nome, ingressos.mesa, ingressos.evento, eventos.evento FROM ingressos INNER JOIN eventos ON ingressos.evento = eventos.evento");
$ingressos ->execute();
$ingressos->setFetchMode(PDO::FETCH_ASSOC);
$dados = $ingressos->fetchAll();
$eventos = $con->prepare("SELECT evento FROM eventos");
$eventos ->execute();
?>
<!doctype html>
<html>
<body>
<?php
if($ingressos){
foreach($eventos as $evt){
echo " <div class='box[]'>
<table border='1px'>
<thead>
<h3> ".$evt['evento']." </h3>
<tr>
<th>mesa</th>
<th>nome</th>
</tr>
</thead> ";
foreach($dados as $ingr){
if($ingr['evento'] == $evt['evento']){
echo " <tbody> ";
echo " <tr> ";
echo "<td>" .$ingr['mesa']. "</td>";
echo "<td>" .$ingr['nome']. "</td>";
}}}}
echo " </tr> ";
echo " </tbody> ";
echo " </table> ";
echo "</div>";
?>
</body>
</html>