Infinite Loop, WHILE inside a FOR

Asked

Viewed 80 times

0

Hello, how are you all?


I’m having a problem with INFINITE LOOP, my problem comes from a FOR that inside him I pass one WHILE, can be a matter of logic or really error in the code, follow the example.

I have a table called OPTIONS in my bank Mysql, within this table, I have the following fields: poll_id, id, candidate, votes. my intention is to bring the registered candidates, along with their votes, my function that will do this, is written like this.

public function resultsPoll()
{
    global $tpl;

    $recvTitle = "SELECT id, title, date, active FROM polls WHERE active = 1";
    $conn = connection::prepare($recvTitle);
    $conn->execute();
    $res = $conn->fetch(PDO::FETCH_OBJ);

    $id = $res->id;

    $sql = "SELECT votes, candidate FROM options WHERE poll_id = $id ORDER BY votes ASC";
    $stmt = connection::prepare($sql);
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->execute();

    while($result = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        $options[]  = $result['candidate'];
        $votes[]    = $result['votes'];
    }

    $count = count($options);
    $amount = array_sum($votes);

    for($i = 0; $i < $count; $i++) 
    {
        $countVotes = $votes[$i] / $amount;

        while($i < $count)
        {
            $tplTemp = '
                <div class="col-12 opcoes">
                    <h4>' . $options[$i] . '</h4>
                    <div class="progress">
                        <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: ' . number_format($countVotes * 100) . '%"></div>
                    </div>
                </div>';
        }

    }

    $tpl->set("showResults", $tplTemp);
    unset($tplTemp);
}

What would be the most correct way to pass this request and receive all options? If I remove this WHILE, I will bring only one value from the bank, but it is not what I need.

  • I think in place of while you should use another for, pq vc will need another control variable to compare with the variable $count.

  • Hello @Sam, good afternoon! With another FOR, he keeps listing only one value, but thank you very much for the tip.

  • @Sam, really I did not realize the variable, complete lack of attention from me, a POINT, completely changed the structure of the code, thank you for the contribution. Thanks for the guidance.

1 answer

1


Apparently you don’t need a second loop inside the for. Just go through the array $options as you are doing, and missed to concatenate the variable $tplTemp every turn of the loop. The way you are doing, you are overwriting the value of this variable every loop of the loop, resulting after the completion in the last assigned value.

To concatenate you need to add the point before the sign =:

    AQUI CONCATENA
         ↓
$tplTemp .= '
    <div class="col-12 opcoes">
        <h4>' . $options[$i] . '</h4>
        <div class="progress">
            <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: ' . number_format($countVotes * 100) . '%"></div>
        </div>
    </div>';

Declare the variable $tplTemp before also.

Then your code would look like this:

$tplTemp;
for($i = 0; $i < $count; $i++){
  $countVotes = $votes[$i] / $amount;

   $tplTemp .= '
       <div class="col-12 opcoes">
           <h4>' . $options[$i] . '</h4>
           <div class="progress">
               <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: ' . number_format($countVotes * 100) . '%"></div>
           </div>
       </div>';
}

Browser other questions tagged

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