I am making two queries in different tables of the DB to generate a page with different views depending on the result. For this, I am assembling in each query a different While. In fact, one is within the other, inserted in a conditional logic.
The code:
<?php
$sessao = new Sessao;
$dados = $sessao->listarSessao();
if(empty($dados)):
echo "Nenhum serviço cadastrado";
else:
$p = new ArrayIterator($dados);
while($p->valid()):
if($p->current()->exibe == 1){
?>
<div id="sessao-list">
<div class="tSession">Sessão <?php echo $p->current()->numero_sessao; ?></div>
<div class="textS"><?php echo $p->current()->nome_sessao; ?></div>
<div class="Access"><a href="sessao.php?link=<?php echo $_GET['link']; ?>&servico=<?php echo $_GET['id']; ?>&id=<?php echo $p->current()->id; ?>" title="<?php echo $p->current()->nome_sessao; ?>" class="info-tooltip" target="_self"><img src="images/shared/access.png"/></a></div>
</div>
<?php
} else {
$session = new Sessao;
$infos = $session->liberarSessao();
$i = new ArrayIterator($infos);
while($i->valid()):
if($p->current()->id == $i->current()->id_sessao){
?>
<div id="sessao-list">
<div class="tSession">Sessão <?php echo $p->current()->numero_sessao; ?></div>
<div class="textS"><?php echo $p->current()->nome_sessao; ?></div>
<div class="Access"><a href="sessao.php?link=<?php echo $_GET['link']; ?>&servico=<?php echo $_GET['id']; ?>&id=<?php echo $p->current()->id; ?>" title="<?php echo $p->current()->nome_sessao; ?>" class="info-tooltip" target="_self"><img src="images/shared/access.png"/></a></div>
</div>
<?php
} else {
?>
<div id="sessao-list">
<div class="tSessionD">Sessão <?php echo $p->current()->numero_sessao; ?></div>
<div class="textSD"><?php echo $p->current()->nome_sessao; ?></div>
</div>
<?php
};
$i->next();
endwhile;
};
$p->next();
endwhile;
endif;
?>
I am using ArrayIterator and both queries are in a Session.php class. The logic should be:
1) If the "displays" field of the "sessoes" table equals 1, it displays "full" html; otherwise, it falls on the second check ...
2) If there is a record of that session for that user in the "sess_users" table, it displays "complete" HTML; otherwise, it displays "simplified" HTML.
Queries in the Session.php class are like this:
public function listarSessao(){
$pdo = parent::getDB();
$listarSessao = $pdo->prepare("select * from sessoes WHERE id_servico =" .$_GET['id']. " ORDER BY id ASC");
$listarSessao->execute();
return $listarSessao->fetchAll(PDO::FETCH_OBJ);
}
public function liberarSessao(){
$pdo = parent::getDB();
$liberarSessao = $pdo->prepare("select * from sess_users WHERE id_servico =" .$_GET['id']. " AND id_user=" .$_SESSION['usrid']. " ORDER BY id ASC");
$liberarSessao->execute();
return $liberarSessao->fetchAll(PDO::FETCH_OBJ);
}
The problem is that the second While is duplicating each record, as many times as there are records in the "sess_users" table. I believe that if there is any way to limit the bond in 1 time, the problem would be solved. Or is there any other way to do it?