flyersun Posted January 29, 2008 Share Posted January 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88304-i-late-loops/ Share on other sites More sharing options...
pocobueno1388 Posted January 29, 2008 Share Posted January 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88304-i-late-loops/#findComment-451885 Share on other sites More sharing options...
flyersun Posted January 29, 2008 Author Share Posted January 29, 2008 The first loop finds the $total I need this before I can do the math in the second loop. Quote Link to comment https://forums.phpfreaks.com/topic/88304-i-late-loops/#findComment-452280 Share on other sites More sharing options...
sasa Posted January 29, 2008 Share Posted January 29, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/88304-i-late-loops/#findComment-452299 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.