multiple answers

a complete ready to use php survey system

multiple answers

Postby m2 » Sat Nov 22, 2003 8:47 pm

i need multiple answers.
perhaps somebody needs it too.

my experience in PHP are very less. i'm sure there are better ways to realize this. but this works for me.

take_vote.php:

Code: Select all
<?php

include_once("survey.inc.php");

if ($quick!=1)
   session_start();

connectdb();

$request_values=$_REQUEST;
array_shift($request_values);
for ($i=0, $n=3; $i<$n; $i++) {
array_pop($request_values);
}
$array_values=count($request_values);
if(is_array($request_values) && $array_values > 0) {
foreach($request_values as $key=>$value)
{
$res_answer = mysql_query("update nabopoll_answers set count=count+1 where survey=$survey and question=$question and id=$value");
}
$answer="true";
}

if ($answer != "")
{
   $var = "answer" . $question;
   ${$var} = $answer;

   if ($quick!=1)
      session_register($var);

   $question++;
   $res_question = mysql_query("select * from nabopoll_questions where survey=$survey and id=$question");

   if ($res_question == FALSE || mysql_numrows($res_question) == 0)
   {
      // record all the answers
      $question = 1;
      $res_survey = mysql_query("select * from nabopoll_surveys where id=$survey");
      if ($res_survey == FALSE || mysql_numrows($res_survey) != 1)
         error("survey not found");

      if ($quick!=1)
         session_destroy();

      if (mysql_result($res_survey, 0, "test_ip") == 1)
         $res_ip = mysql_query("insert into nabopoll_ip values($survey, \"$REMOTE_ADDR\")");

      if ($quick!=1)
      {
         opensurvey($survey);
         $survey_end = gettemplate('survey_end');
         $survey_end = surveytags($survey_end);
         $survey_end = str_replace($tags["survey_results"], 'result.php?survey='.$survey, $survey_end);
         echo $survey_end;
         closehtml();
      }
      else
      {
         redirect(getenv("HTTP_REFERER"));
      }
   }

   session_register("question");
}

if ($quick!=1)
   redirect("vote.php?" . ((SID == "") ? "" : "&" . SID));
else
   redirect(getenv("HTTP_REFERER"));
?>



you have to change the radio-button in survey.inc.php too.

change:

Code: Select all
$answer = str_replace($tags["answer_radio"],' <input type="radio" name="answer" value="'.$row_answer["id"].'">', $answer);


into:

Code: Select all
$answer = str_replace($tags["answer_radio"],' <input type="checkbox" name="answer'.$row_answer["id"].'" value="'.$row_answer["id"].'">', $answer);


### EDIT ###

if i add a question, i want to have the choice between single and multiple answers. to get this work:

at first, add a field multiple ( int(2) ) at the end of the table nabopoll_questions

Code: Select all
ALTER TABLE `nabopoll_questions` ADD `multiple` INT( 2 ) DEFAULT '0' NOT NULL ;



question_edit.php


Code: Select all

<?php
include_once("admin.inc.php");
$back_url = "/poll/survey_edit.php";

if ($survey == "")
   error("no survey specified");

connectdb();

$res_survey = mysql_query("select * from nabopoll_surveys where id=$survey");
if ($res_survey == FALSE || mysql_num_rows($res_survey) == 0)
   error("survey not found");

$res_questions = mysql_query("select * from nabopoll_questions where survey=$survey order by id");
if ($res_questions == FALSE)
   error("database error");

openadmin();
$question=0;

echo '<table cellspacing=1><tr bgcolor="gray">';
echo '<td><font color="white" size="-1">&nbsp;#&nbsp;</font></td>';
echo '<td><font color="white" size="-1"><div align=left">&nbsp;Question&nbsp;</div><div align="right">Multiple Answers?</div></font></td>';
echo '<td align="center"><font color="white" size="-1">&nbsp;Actions&nbsp;</font></td>';
echo '</tr><tr><td>&nbsp;</td></tr>';

$count = mysql_num_rows($res_questions);

while ($row = mysql_fetch_array($res_questions))
{
   $question = $row["id"];

   echo '<tr><td valign="top"><b><font size="-1">' . $question . '</font></b></td>';

   echo '<form action="question_alter.php" method="post">';
   echo '<input type="hidden" name="survey" value="' . $survey . '">';
   echo '<input type="hidden" name="question" value="' . $question . '">';
   echo '<td valign="top"><input type="text" class="txtfld" size=30 name="prompt" value="' . $row["question"] . '">
<input type="checkbox" name="multiple" value="1"';
 if($row["multiple"] == 1) { echo ' checked'; }

echo '></td>';

   echo '<td><table><tr>';

   echo '<td><input type="image" src="../images/alter.gif" alt="edit" value="edit"></form></td>';

   echo '<form action="question_delete.php" method="post">';
   echo '<input type="hidden" name="survey" value="' . $survey . '">';
   echo '<input type="hidden" name="question" value="' . $question . '">';
   echo '<td><input type="image" src="../images/delete.gif" alt="delete" value="delete"></form></td>';

   echo '<form action="question_insert.php" method="post">';
   echo '<input type="hidden" name="survey" value="' . $survey . '">';
   echo '<input type="hidden" name="question" value="' . $question . '">';
   echo '<input type="hidden" name="question" value="' . $multiple . '">';
   echo '<td><input type="image" src="../images/insert.gif" alt="insert before" value="insert before">';
   echo '</form></td>';

   if ($count != mysql_num_rows($res_questions))
   {
      echo '<form action="question_move.php" method="post">';
      echo '<input type="hidden" name="survey" value="' . $survey . '">';
      echo '<input type="hidden" name="question" value="' . $question . '">';
      echo '<input type="hidden" name="direction" value="up">';
      echo '<td><input type="image" src="../images/up.gif" alt="move up" value="move up">';
      echo '</form></td>';
   }
   else
   {
      echo '<td width=16></td>';
   }

   if ($count != 1)
   {
      echo '<form action="question_move.php" method="post">';
      echo '<input type="hidden" name="survey" value="' . $survey . '">';
      echo '<input type="hidden" name="question" value="' . $question . '">';
      echo '<input type="hidden" name="direction" value="down">';
      echo '<td><input type="image" src="../images/down.gif" alt="move down" value="move down">';
      echo '</form></td>';
   }
   else
   {
      echo '<td width=16></td>';
   }

   echo '<form action="answer_edit.php" method="post">';
   echo '<input type="hidden" name="survey" value="' . $survey . '">';
   echo '<input type="hidden" name="question" value="' . $question . '">';
   echo '<td><input type="image" src="../images/inspect.gif" alt="edit answers" value="edit answers">';
   echo '</form></td>';

   echo '</tr></table></td></tr>';
   $count--;

}

$question++;
echo '<form action="question_add.php" method="post">';
echo '<input type="hidden" name="survey" value="' . $survey . '">';
echo '<input type="hidden" name="question" value="' . $question . '">';
echo '<tr><td valign="top"><b><font size="-1">'.$question.'</font></b></td>';
echo '<td valign="top"><input type="text" class="txtfld" size=30 name="prompt" value="">&nbsp;<input type="checkbox" name="multiple" value="1"></td>';
echo '<td><input type="image" src="../images/add.gif" alt="add" value="add"></form></td></tr>';

echo '</table>';

echo '<p><a href="survey_edit.php">back</a></p>';
closeadmin();

?>



question_add.php

Code: Select all

<?php
include_once("admin.inc.php");

if ($survey == "")
   error("no survey specified");

if ($question == "")
   error("no question specified");

if ($prompt == "")
   error("text mandatory");
connectdb();
if($multiple == 1)
{
$is_multiple=1;
}
else {
$is_multiple=0;
}
$res = mysql_query("insert into nabopoll_questions values($survey, $question, \"$prompt\", $is_multiple)");
redirect("question_edit.php?survey=" . $survey);

?>



question_alter.php

Code: Select all
<?php
include_once("admin.inc.php");

if ($survey == "")
   error("no survey specified");

if ($question == "")
   error("no question specified");

if ($prompt == "")
   error("text mandatory");

connectdb();

$res = mysql_query("update nabopoll_questions set question=\"$prompt\", multiple=\"$multiple\" where survey=$survey and id=$question");

redirect("question_edit.php?survey=" . $survey);

?>


survey.inc.php
in function showquestion:
Code: Select all
// get the list of answers
   $survey_id = $row_survey["id"];
   $question_id = $row_question["id"];
   $res_answer = mysql_query("select * from nabopoll_answers where survey=$survey_id and question=$question_id order by id");
   $res_question = mysql_query("select * from nabopoll_questions where survey=$survey_id and id=$question_id order by id");
   if ($res_answer == FALSE || mysql_numrows($res_answer) == 0)
      error("error in database or no answers for this question");

   while ($row_answer = mysql_fetch_array($res_answer))
   {
      $count++;

      // get the answer template
      $start = strpos($question_text, $tags["answer_start"]);
      $end = strpos($question_text, $tags["answer_end"]);
      if ($start==FALSE || $end==FALSE)
      {
         if ($quick==1)
         {
            echo 'error in template';
            return;
         }
         else
         {
            error("error in template");
         }
      }
      $answer_tmpl = substr($question_text, $start, $end-$start+strlen($tags["answer_end"]));

      // build the answer
      $answer = substr($answer_tmpl, strlen($tags["answer_start"]), strlen($answer_tmpl)-strlen($tags["answer_start"])-strlen($tags["answer_end"]));
      if($row_question['multiple'] == 1) {
      $answer = str_replace($tags["answer_radio"],' <input type="checkbox" name="answer'.$row_answer["id"].'" value="'.$row_answer["id"].'">', $answer);
      }
      else {
      $answer = str_replace($tags["answer_radio"],' <input type="radio" name="answer" value="'.$row_answer["id"].'">', $answer);
      }
      
      $answer = str_replace($tags["answer_id"], $row_answer["id"], $answer);
      $answer = str_replace($tags["answer_text"], $row_answer["answer"], $answer);

      // if this is the last answer then we stop adding the template
      if ($count == mysql_numrows($res_answer))
         $answer_tmpl = "";

      // new string
      $question_text = substr($question_text, 0, $start) . $answer . $answer_tmpl . substr($question_text, $end+strlen($tags["answer_end"]));
   }


comments welcome ;)

micha
m2
 
Posts: 1
Joined: Sat Nov 22, 2003 8:23 pm

Return to nabopoll

Who is online

Users browsing this forum: No registered users and 1 guest