
CREATE TABLE `polls` ( `ID` int(11) NOT NULL auto_increment, `title` varchar(255) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM; CREATE TABLE `poll_questions` ( `ID` int(11) NOT NULL auto_increment, `ID_poll` int(11) NOT NULL, `poll_question` varchar(255) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM; CREATE TABLE `poll_options` ( `ID` int(11) NOT NULL auto_increment, `ID_question` int(11) NOT NULL, `ID_poll` int(11) NOT NULL, `poll_option` varchar(255) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM; CREATE TABLE `poll_answers` ( `ID` int(11) NOT NULL auto_increment, `ID_option` int(11) NOT NULL, `ID_question` int(11) NOT NULL, `ID_poll` int(11) NOT NULL, `visitor_ip` varchar(32) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM;Our PHP survey web app. is using 1 main page (it started with 4 actually but I compressed it somewhere in day 3 lol) to add, delete surveys, questions and answers, 2 pages for displaying the charts (results per question or per survey) and some pop-ups to take a survey, grab the html code for implementing it on your pages etc. index.php
<?php
$getsurveys = new Pagination();
$getsurveys->start = ($_GET['start']) ? $_GET['start'] : '0';
$getsurveys->filePath = 'index.php';
$getsurveys->select_what = '*';
$getsurveys->the_table = 'polls';
$getsurveys->add_query = 'ORDER BY ID DESC';
$getsurveys->limit = 5;
$surveys = $getsurveys->getQuery(TRUE);
$bg1 = "t9"; // color one
$bg2 = "t10"; // color two
$row_count = 0;
$i = (($_GET['start']) ? $_GET['start'] : '0')+1;
$content = ' <table width="100%" border="0" cellspacing="0" cellpadding="0" class="t0">'."\n";
$content .= ' <tr>'."\n";
$content .= ' <td class="t1"><div align="center"><b>No.</b></div></td>'."\n";
$content .= ' <td class="t2"><div align="center"><b>Title</b></div></td>'."\n";
$content .= ' <td class="t3"><div align="center"><b>Answers</b></div></td>'."\n";
$content .= ' <td class="t4"><div align="center"><b>Action</b></div></td>'."\n";
$content .= ' </tr>'."\n";
if(mysql_num_rows($surveys)>0)
{
$row = mysql_fetch_assoc($surveys);
do {
$row_color = ($row_count % 2) ? $bg1 : $bg2;
$content .= ' <tr>'."\n";
$content .= ' <td class="t5 '.$row_color.'">'."\n";
$content .= ' <div align="center">'.$i++.'</div></td>'."\n";
$content .= ' <td class="t6 '.$row_color.'">'."\n";
$content .= ' <div align="left"><a href="view_survey.php?ID='.$row['ID'].'" target="_blank">'.$row['title'].'</a>'."\n";
$content .= ' <img src="images/add.png" width="16" height="16" border="0" align="bottom" class="cat_img" onclick="MM_effectSlide(\'survey_'.$row['ID'].'\', 700, \'0%\', \'100%\', true, false, true)" />'."\n";
$content .= '</div>'."\n";
$content .= '<div style="height:25px">'."\n";
$content .= '<div class="q_form" id="survey_'.$row['ID'].'">'."\n";
$content .= ' <form action="'.selfURL().'" method="POST">'."\n";
$content .= ' <input type="hidden" name="ID_survey" value="'.$row['ID'].'" size="32" />'."\n";
$content .= ' <input type="text" name="new_question" value="" size="32" />'."\n";
$content .= ' <input type="submit" name="add_question" value="Add question" />'."\n";
$content .= ' </form>'."\n";
$content .= '</div>'."\n";
$content .= '</div>'."\n";
$getQuestions = mysql_query("SELECT * FROM poll_questions WHERE ID_poll = '".$row['ID']."'");
if(mysql_num_rows($getQuestions)>0)
{
$row_questions = mysql_fetch_assoc($getQuestions);
$q = 1;
do {
$content .= ' <div style="padding-left:100px;">'."\n";
$content .= ' '.$q++.' : '.$row_questions['poll_question'].''."\n";
$content .= ' <a href="javascript:void(0);" title="add option">'."\n";
$content .= ' <img src="images/add.png" width="16" height="16" border="0" onclick="MM_effectSlide(\''.$row_questions['ID'].'\', 700, \'0%\', \'100%\', true, false, true)" />'."\n";
$content .= ' </a>'."\n";
$content .= ' <a href="javascript:popUp(\'chart.php?ID='.$row_questions['ID'].'\');" title="take survey">'."\n";
$content .= ' <img src="images/chart.png" width="16" height="16" border="0" />'."\n";
$content .= ' </a>'."\n";
$content .= ' <a href="javascript:void(0);" title="delete">'."\n";
$content .= ' <img src="images/delete.png" width="16" height="16" border="0" onClick="xajax_admin_action_questions(\'delete\', \''.$row_questions['ID'].'\');" />'."\n";
$content .= ' </a>'."\n";
$getOptions = mysql_query("SELECT * FROM poll_options WHERE ID_question = '".$row_questions['ID']."'");
if(mysql_num_rows($getOptions)>0)
{
$row_options = mysql_fetch_assoc($getOptions);
do {
$content .= ' <div style="padding-left:30px; height:18px">'."\n";
$content .= '→'.$row_options['poll_option'];
$content .= ' <a href="javascript:void(0);" title="delete">'."\n";
$content .= ' <img src="images/delete.png" width="16" height="16" border="0" onClick="xajax_admin_action_options(\'delete\', \''.$row_options['ID'].'\');" />'."\n";
$content .= ' </a>'."\n";
$content .= ' </div>'."\n";
} while ($row_options = mysql_fetch_assoc($getOptions));
}
$content .= '<div style="height:25px">'."\n";
$content .= '<div class="q_form" id="'.$row_questions['ID'].'">'."\n";
$content .= ' <form action="'.selfURL().'" method="POST" class="form">'."\n";
$content .= ' <input type="hidden" name="ID_question" value="'.$row_questions['ID'].'" size="32" />'."\n";
$content .= ' <input type="hidden" name="ID_poll" value="'.$row['ID'].'" size="32" />'."\n";
$content .= ' <input type="text" name="new_option" value="" size="32" />'."\n";
$content .= ' <input type="submit" name="add_option" value="Add option" />'."\n";
$content .= ' </form>'."\n";
$content .= '</div>'."\n";
$content .= '</div>'."\n";
$content .= ' </div>'."\n";
} while ($row_questions = mysql_fetch_assoc($getQuestions));
}
$content .= ' </td>'."\n";
$content .= ' <td class="t7 '.$row_color.'">'."\n";
$content .= ' <div align="center">'.getSurveyAnswers($row['ID']).'</div></td>'."\n";
$content .= ' <td class="t8 '.$row_color.'">'."\n";
$content .= ' <div align="center">'."\n";
$content .= ' <form name="form'.$row['ID'].'" id="form'.$row['ID'].'" class="form" >'."\n";
$content .= ' <input name="id_survey" type="hidden" value="'.$row['ID'].'" />'."\n";
$content .= ' <div align="center">'."\n";
if(checkSurvey($row['ID'])):
$content .= ' <a href="javascript:popUp(\'survey.php?ID='.$row['ID'].'\');" title="take survey">'."\n";
$content .= ' <img src="images/survey.png" width="16" height="16" border="0" />'."\n";
$content .= ' </a>'."\n";
$content .= ' <a href="charts.php?ID='.$row['ID'].'" title="view results" target="_blank">'."\n";
$content .= ' <img src="images/chart.png" width="16" height="16" border="0" />'."\n";
$content .= ' </a>'."\n";
$content .= ' <a href="javascript:popUp(\'code.php?ID='.$row['ID'].'\');" title="get the code">'."\n";
$content .= ' <img src="images/code.png" width="16" height="16" border="0" />'."\n";
$content .= ' </a>'."\n";
endif;
$content .= ' <a href="javascript:void(0);" title="delete">'."\n";
$content .= ' <img src="images/delete.png" width="16" height="16" border="0" onClick="xajax_admin_action(\'delete\', \''.$row['ID'].'\');" />'."\n";
$content .= ' </a>'."\n";
$content .= ' </div>'."\n";
$content .= ' </form>'."\n";
$content .= ' </div></td>'."\n";
$content .= ' </tr>'."\n";
$row_count++;
} while ($row = mysql_fetch_assoc($surveys));
$content .= '</table>'."\n";
echo $content;
echo $getsurveys->paginate();
}
else {
echo '<div align="center" style="margin:200px auto auto auto;">No surveys defined</div>';
}
?>
The above code represents the main interface of our php survey, you will notice in the first lines that it uses a class (Pagination) to pull the surveys out from the database, limiting them at 5 per page.
$getsurveys = new Pagination();
$getsurveys->start = ($_GET['start']) ? $_GET['start'] : '0';
$getsurveys->filePath = 'index.php';
$getsurveys->select_what = '*';
$getsurveys->the_table = 'polls';
$getsurveys->add_query = 'ORDER BY ID DESC';
$getsurveys->limit = 5;<?php
if(checkSurvey($_GET['ID']))
{
$getQuestions = mysql_query("SELECT poll_questions.ID AS ID, poll_questions.poll_question AS poll_question FROM poll_questions WHERE poll_questions.ID_poll='".mysql_real_escape_string($_GET['ID'])."' AND NOT EXISTS(SELECT poll_answers.ID FROM poll_answers WHERE poll_answers.ID_question = poll_questions.ID AND poll_answers.visitor_ip = '".getIP()."')");
if(mysql_num_rows($getQuestions)>0)
{
$row_questions = mysql_fetch_assoc($getQuestions);
echo $row_questions['poll_question']. '<br /><hr />';
$getOptions = mysql_query("SELECT * FROM poll_options WHERE ID_question = '".mysql_real_escape_string($row_questions['ID'])."' ORDER BY ID ASC") or die(mysql_error());
$row_options = mysql_fetch_assoc($getOptions);
do {
echo '<br /><input type="radio" name="answer" id="'.$row_options['ID'].'" value="'.$row_options['ID'].'" onClick="xajax_insert_answer(\''.$row_options['ID'].'\', \''.$_GET['ID'].'\', \''.$row_questions['ID'].'\');" /><label for="'.$row_options['ID'].'">'.$row_options['poll_option'].'</label>';
} while ($row_options = mysql_fetch_assoc($getOptions));
}
else {
echo 'Thank you for taking this survey! You may <a href="'.$url.'charts.php?ID='.$_GET['ID'].'" title="view survey results" target="_blank">view results</a>';
}
}
else {
echo 'This poll has no questions or options to questions';
}
?>
The hard thing to achieve here was this simple goal: I wanted to use some ajax and display the options helped by html radio inputs. Presented with the options, the visitor will have to select one of them and then, automatically, the page will show the next set of options. Sounds simple but remember that on this "simple" change, the page will have to store the answer using AJAX, refresh the page and perform a SELECT statement to display the next question for that survey. The hard part was to create that select in such a way by actually showing it what exactly not to select: "... AND NOT EXISTS(SELECT poll_answers.ID FROM poll_answers WHERE poll_answers.ID_question = poll_questions.ID AND poll_answers.visitor_ip = '".getIP()."')". As you can see we're using the visitor's IP address to determine if he already answered to that question (if we have an entry of that question with this IP in the answers table) and if we do, we skip it until there are no more questions - a moment when we show a message with a link to the chart representing the results of that survey (echo 'Thank you for taking this survey! You may <a href="'.$url.'charts.php?ID='.$_GET['ID'].'" title="view survey results" target="_blank">view results</a>';).
You can also combine this application with PHP login and make it available for use with membership required and build your queries based on user ID.
Ok. I already talked too much (or too little) for this application. Let's have some fun. Here's a demo (you won't be able to modify add or delete any survey). Download link, lower on the page.
No. |
Attachments |
Downloads |
|
|
1 |
PHP survey script An archive containing the PHP survey script files. Uploaded on: 2007-09-21 | 12:56 am |
561 |
|

2009-02-11 | 08:58 pm