Infinite loop, object orientation

Asked

Viewed 530 times

3

Well, I’ve been doing some research, I found several topics talking about it, but none of them were useful to me, so I decided to turn to Stack Overflow.

I have a role to list all user tickets that are open:

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());
        }
    }

Until then beauty, let’s go to the main, I have in my html, a while where I put all my code pad HTML, but when I use the while, it captures only 1 data from my table and goes into 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.

  • I already tried, returns me the following error: Notice: Undefined offset: 1.

1 answer

3


The infinite loop happens because at each turn of the while the same result is returned the resultset does not advance or the first row of the table is returned N times and will never return false there while not for even.

One option is to play while inside the function and return a complete array.

Function

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

On the call of your code do:

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

Or

$arr = $ticket->list_ticket()
foreach($arr as $row){ ...
  • I did what you said, but now even load the page... I’m not able to proceed with the project...

  • @Guilhermegouveiaalves, put it in the beginning of the file, ini_set('display_errors', true); error_reporting(E_ALL);

  • Put, not showing any error, the loop continues after some time stopped.

  • @Guilhermegouveiaalves edits the answer, lacked the return $lista

  • Haha, lack of attention on my part too, thank you, problem solved.

Browser other questions tagged

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