Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.