GrandmasterB Posted February 22, 2008 Share Posted February 22, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/92479-trying-to-create-a-multiple-question-survey/ Share on other sites More sharing options...
sKunKbad Posted February 22, 2008 Share Posted February 22, 2008 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/92479-trying-to-create-a-multiple-question-survey/#findComment-473802 Share on other sites More sharing options...
GrandmasterB Posted February 22, 2008 Author Share Posted February 22, 2008 SkunKbad...I just sent you an email. I'm trying to figure out your way of doing this questionnaire. Might be easier than my way for sure. Quote Link to comment https://forums.phpfreaks.com/topic/92479-trying-to-create-a-multiple-question-survey/#findComment-473809 Share on other sites More sharing options...
sKunKbad Posted February 22, 2008 Share Posted February 22, 2008 Hey GmB, I just replied to your email. I sent you some files. Call me if you have questions. Phone number is in the email. Quote Link to comment https://forums.phpfreaks.com/topic/92479-trying-to-create-a-multiple-question-survey/#findComment-473857 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.