Infinite Loop, object orientation

3

Stack Overflow .

Stack Overflow

I have a function to list all tickets for the user who is logged in:

public function list_ticket() {
        try {
            $session = $_SESSION[SESSION_PREFIX . 'email_username'];
            $sql = "SELECT * FROM 'tickets' WHERE email=:session ORDER BY id DESC";
            $pdo = $this->connection()->prepare($sql);
            $pdo->bindParam(':session', $session, PDO::PARAM_STR);
            $pdo->execute();
            return $pdo->fetch(PDO::FETCH_NUM);
        } catch (PDOException $e) {
            die('Ocorreu um erro: ' . $e->getMessage() . ' Na linha: ' . $e->getLine() . ' No arquivo: ' . $e->getFile());
        }
    }

So far beauty, let's go to the main one, I have in my html, a while where I put all my code block HTML , but when I use while , it only captures 1 data from my table and enters in Loop Infinito , see below the structure:

<table class="table table-striped">
    <thead>
        <tr>
            <th>Ticket #</th>
            <th>Criado em</th>
            <th>Autor</th>
            <th>Email</th>
            <th>Assunto</th>
            <th>Departamento</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        <?php
        while ($row = $ticket->list_ticket()) {
            ?>
            <tr>
                <td>
                    <a href="<?php echo BASE_URL; ?>"><?php echo $row[0]; ?></a>
                </td>
                <td>
                    <?php echo $row[1]; ?>
                </td>
                <td>
                    <?php echo $row[2]; ?>
                </td>
                <td>
                    <?php echo $row[3]; ?>
                </td>
                <td>
                    <?php echo $row[4]; ?>
                </td>
                <td>
                    <?php echo $row[5]; ?>
                </td>
                <td>
                    <?php
                    switch ($row[7]) {
                        case 0:
                            echo '<span class="label label-success">Aberto</span>';
                            break;
                        case 1:
                            echo '<span class="label label-info">Respondido</span>';
                            break;
                        case 2:
                            echo '<span class="label label-danger">Fechado</span>';
                            break;
                    }
                    ?>
                </td>
            </tr>
        <?php } ?>
    </tbody>
</table>

I await answers.

    
asked by anonymous 04.02.2016 / 19:39

1 answer

3

The infinite loop happens because every time the same result is returned, the resultset does not progress, ie the first row of the table is returned N times and never returns false while the while for even.

One option is to throw the while into the function and return a complete array.

Function

$pdo->execute();
$lista = array();
while($row = $pdo->fetch(PDO::FETCH_NUM)){
   $lista[] = $row;
}
return $lista;

When calling your code do

foreach($ticket->list_ticket() as $row){ ...

Or

$arr = $ticket->list_ticket()
foreach($arr as $row){ ...
    
04.02.2016 / 19:46