Jump to content

Recommended Posts

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.

 

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

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.