Jump to content

I late Loops!


flyersun

Recommended Posts

I'm trying to do this..

$poll_id_query = mysql_query("SELECT * FROM tblPollQuestions WHERE username = '$user' ORDER BY pollID DESC LIMIT 1 ");

$poll_id_array = mysql_fetch_array($poll_id_query);

$poll_id = $poll_id_array['pollID'];

$poll_question = $poll_id_array['question'];

 

echo "<p>$poll_question</p><br /><p>";

 

$poll_query = mysql_query("SELECT * FROM tblPollAnswers1 WHERE pollID = '$poll_id' ");

 

while ($poll_array = mysql_fetch_array($poll_query)){

 

$amount = $poll_array['amount'];

$total = $total + $amount;

 

}

 

while ($poll_array = mysql_fetch_array($poll_query)){

$answer = $poll_array['answer'];

$amount = $poll_array['amount'];

    $amount = round((($amount * 100) / $total),1);

 

echo "$answer - <img src='sections/line.jpg' width='14%' height='5'><br />";

}

 

But the second loop doesn't run cos the first one is there.. anyone have any idea what I can do instead?

 

I'm probably coding this really stupidly but it's really late at night and I can't think.

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/88304-i-late-loops/
Share on other sites

Why are you doing two of the same loops, just with different information in them? Combine them.

 

<?php

$poll_id_query = mysql_query("SELECT * FROM tblPollQuestions WHERE username = '$user' ORDER BY pollID DESC LIMIT 1");
$poll_id_array = mysql_fetch_array($poll_id_query);
$poll_id = $poll_id_array['pollID'];
$poll_question = $poll_id_array['question'];

echo "<p>$poll_question</p><p>";

$poll_query = mysql_query("SELECT * FROM tblPollAnswers1 WHERE pollID = '$poll_id' ");

while ($poll_array = mysql_fetch_array($poll_query)) {
    
    $amount = $poll_array['amount'];
    $total = $total + $amount;
    $answer = $poll_array['answer'];
    $amount = round((($amount * 100) / $total),1);
    
    echo "$answer - <img src='sections/line.jpg' width='14%' height='5'>";
}

?>

 

See if that does what you want.

 

Link to comment
https://forums.phpfreaks.com/topic/88304-i-late-loops/#findComment-451885
Share on other sites

try to change 2nd loop to

    $poll_query = mysql_query("SELECT answer, sum(amount) as total FROM tblPollAnswers1 WHERE pollID = '$poll_id' LIMIT 1");
    $poll_array = mysql_fetch_array($poll_query)
    $total = $poll_array['total'];
    $answer = $poll_array['answer'];
    $amount = round((($amount * 100) / $total),1);

Link to comment
https://forums.phpfreaks.com/topic/88304-i-late-loops/#findComment-452299
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.