pocobueno1388 Posted April 4, 2007 Share Posted April 4, 2007 Okay, I am back with the same question I had earlier except I reconstructed my database table. I have a survey and when it is submitted all the answers go into the database. In one row in the DB the questions are stored as q1, q2, q3, .... all the way up to q15 because that is how many questions there are on the survey. Then in the next column of the DB the answers are stored. The rows the for answer column will either contain a,b,c, or d. What I am trying to do is loop through every questions and every possible answer for that question without having to use repeated code with minor changes 15 times. Here is the code I have so far, but it ONLY works for the first question. <?php //get how many people took the survey $ppl = mysql_query("SELECT question FROM survey_new WHERE voteID > 0"); $total = mysql_num_rows($ppl); //all possible answers $answer = array ('a','b','c','d'); //loop through each question and possible answers. for ($i=0; $i<count($answer); $i++){ echo "SELECT question, answer FROM survey_new WHERE question='q1' AND answer='$answer[$i]'<br>"; } ?> This code displays: SELECT question, answer FROM survey_new WHERE question='q1' AND answer='a' SELECT question, answer FROM survey_new WHERE question='q1' AND answer='b' SELECT question, answer FROM survey_new WHERE question='q1' AND answer='c' SELECT question, answer FROM survey_new WHERE question='q1' AND answer='d' This is what I want displayed: SELECT question, answer FROM survey_new WHERE question='q1' AND answer='a' SELECT question, answer FROM survey_new WHERE question='q1' AND answer='b' SELECT question, answer FROM survey_new WHERE question='q1' AND answer='c' SELECT question, answer FROM survey_new WHERE question='q1' AND answer='d' SELECT question, answer FROM survey_new WHERE question='q2' AND answer='a' SELECT question, answer FROM survey_new WHERE question='q2' AND answer='b' SELECT question, answer FROM survey_new WHERE question='q2' AND answer='c' SELECT question, answer FROM survey_new WHERE question='q2' AND answer='d' SELECT question, answer FROM survey_new WHERE question='q3' AND answer='a' SELECT question, answer FROM survey_new WHERE question='q3' AND answer='b' SELECT question, answer FROM survey_new WHERE question='q3' AND answer='c' SELECT question, answer FROM survey_new WHERE question='q3' AND answer='d' SELECT question, answer FROM survey_new WHERE question='q4' AND answer='a' SELECT question, answer FROM survey_new WHERE question='q4' AND answer='b' SELECT question, answer FROM survey_new WHERE question='q4' AND answer='c' SELECT question, answer FROM survey_new WHERE question='q4' AND answer='d' All the way up to question 15. So my question is how would I create another for loop inside [or outside] of the other one that will allow me to do this without repeated code? Any help is greatly appreciated XD Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/45596-solved-creating-a-for-loop-that-will-work/ Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 You need an outside loop to change the question: <?php $answer = array ('a','b','c','d'); $questions = array('q1','q2','q3','q4'); //loop through each question and possible answers. for ($j=0; $j<count($questions); $j++) { for ($i=0; $i<count($answer); $i++) echo "SELECT question, answer FROM survey_new WHERE question='" . $questions[$j] . "' AND answer='" . $answer[$i] . "'<br>"; echo '<br>'; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/45596-solved-creating-a-for-loop-that-will-work/#findComment-221403 Share on other sites More sharing options...
pocobueno1388 Posted April 4, 2007 Author Share Posted April 4, 2007 Perfect =D Thanks Ken. Quote Link to comment https://forums.phpfreaks.com/topic/45596-solved-creating-a-for-loop-that-will-work/#findComment-221406 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.