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 anotherfor
, pq vc will need another control variable to compare with the variable$count
.– Sam
Hello @Sam, good afternoon! With another
FOR
, he keeps listing only one value, but thank you very much for the tip.– rengaw
@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.
– rengaw