Jump to content

Trying to create a multiple question survey


GrandmasterB

Recommended Posts

Hey guys...i'm new here and tried searching for a solution, but couldn't find anything. Anyhow, I've got a problem creating a survey/poll with multiple questions.

 

I've created a database with two tables 'poll_questions' and 'poll_answers', and inserted data for each. The survey/poll basically is submitted, the votes are tallied and displayed on a results page. I have been able to get it to work with a single question, but I'm stuck trying to get the data to update with multiple questions. Can you guys help? Code is below for the poll and results files.

 

poll.php

<?php

//connect to database
//connect to database
mysql_connect('localhost', 'login', 'pass');
mysql_select_db('dbname');

//run query to pull poll question from database
$question = mysql_query("SELECT ID, Question FROM poll_questions;");

echo "<form method=\"post\" action=\"results2.php\">";
echo "<br />";

while ($row = mysql_fetch_assoc($question)) {
	extract($row, EXTR_PREFIX_ALL, 'poll');
	echo "<b>$poll_Question</b><br /><br />";

//run query to pull poll_answers from database
$answer = mysql_query("SELECT ID, Answer FROM poll_answers WHERE Question = $poll_ID;");

//run while loop through answer options - kill once end is reached
while ($row = mysql_fetch_assoc($answer)) {
	extract($row, EXTR_PREFIX_ALL, 'vote');
	echo "<input type=\"radio\" name=\"$poll_ID\" VALUE=\"$vote_ID\">  $vote_Answer</option><br />";
	}
echo "<br />";	
}	

echo "<br /><input type=\"submit\" value=\"Vote\" />";
echo "<input type=\"hidden\" name=\"poll\" value=\"$poll_ID\" />";
echo "</form>";

?>

 

results.php

<?php

//connect to database
mysql_connect('localhost', 'login', 'pass');
mysql_select_db('dbname');

if (isset($_POST['vote'])) {
	$votenum = $_POST['vote'];
	$result = mysql_query("SELECT ID FROM poll_answers WHERE ID = '$votenum' AND Question = '{$_POST['poll']}';");
	if (mysql_num_rows($result)) {
		$result = mysql_query("UPDATE poll_answers SET Votes = Votes + 1 WHERE ID = $votenum;");
		echo "Thanks for voting!<br /><br />";
	} else {
		exit;
	}

}

echo "<strong>Results of poll:</strong><br />";
//run query to pull poll question from database
$question = mysql_query("SELECT ID, Question FROM poll_questions;");

while ($row = mysql_fetch_assoc($question)) {
	extract($row, EXTR_PREFIX_ALL, 'poll');
	echo "<b>$poll_Question</b><br />";

//run query to pull answer and vote totals from database
$answer = mysql_query("SELECT ID, Answer, Votes FROM poll_answers WHERE Question = $poll_ID ORDER BY Votes DESC;");
echo "<ul>";

	while ($row = mysql_fetch_assoc($answer)) {
		extract($row, EXTR_PREFIX_ALL, 'poll');
		echo "<li>$poll_Answer: $poll_Votes</li>";	
	}
echo "</ul>";
}
?>

This isn't a fix for your problem, but I made a script for a customer that used an xml file for the questions/answers. Since the server was running php5, I was able to use simpleXML to parse the questions/answers. Doing so was very simple:

 

$quiz = new SimpleXMLElement($quizxml);
foreach ($quiz->questions->question as $question) {
++$count;
echo "<div class='question'>\n
		<div class='listnumber'>$count )  </div>\n
		<div class='listquestion'>" .$question->q. "</div>\n";
		foreach ($question->option as $thisOption) {
			foreach ($thisOption->choice as $thisChoice){
				echo "<div class=\"choice\">\n
						<input type=\"radio\" name=\"Q$count\" value=\"$thisChoice\" />  $thisChoice\n
					</div>\n";
			}
		}
		echo '</div>';
}

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.