While Times Limit - Arrayiterator

Asked

Viewed 162 times

0

I’m doing two queries in different BD tables to generate a page with different views depending on the result. For this, I am mounting 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 used Arrayiterator and both queries are in a class Sessao.php. The logic should be:

1) If the "displays" field of the "sessoes" table is equal to 1, it displays "complete" html; otherwise it falls into the second check...

2) If there is record of that session for that user in the "sess_users" table, it displays "complete" HTML; otherwise, it displays "simplified" HTML".

The queries in the Sessao.php class look 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 loop by 1 time, the problem would be solved. Or is there some other way to do?

  • I believe that its primary problem is located in the consultations conducted by Sessao.php. Probably a filter is missing in the second query (while internal) that refers to the first query (while external).

  • I will edit the question and enter the code of the two queries, thank you.

  • @Marcoauréliodeleu tried to rewrite the function query list using INNER JOIN, this way: SELECT DISTINCT sessoes.* FROM sessoes INNER JOIN sess_users ON sess_users.id_servico = sessoes.id_servico WHERE sessoes.id_servico = ".$_GET['id']." AND sess_users.id_user = ".$_SESSION['usrid']." ORDER BY sessoes.id ASC . And I tried to use only this function in the two queries (for each While). Only this did not solve, because the IF of the second While (internal) loses the id_sessao value...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.