Advertisement
Guest User

Untitled

a guest
May 7th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.67 KB | None | 0 0
  1. <?php
  2.  
  3. /** database details */
  4. $dbHost = "localhost";
  5. $dbUser = "root";
  6. $dbPass = "";
  7. $dbName = "questionnaire";
  8.  
  9. /** connect to database */
  10. if (!$dbLink = mysql_connect($dbHost, $dbUser, $dbPass)) {
  11.     die(mysql_error());
  12. }
  13.  
  14. /** select the database */
  15. if (!$dbSelected = mysql_select_db($dbName, $dbLink)) {
  16.     die(mysql_error());
  17. }  
  18.  
  19. /**
  20.  * Function to fetch all questions from the database.
  21.  *
  22.  * @return  array
  23.  */
  24. function fetchAllQuestions() {
  25.     $results = mysql_query("
  26.        SELECT *
  27.          FROM questions
  28.    ");
  29.    
  30.     while ($question = mysql_fetch_array($results)) {
  31.         $questions[] = $question;
  32.     }
  33.    
  34.     return $questions;
  35. }
  36.  
  37. /**
  38.  * Function to fetch available answers for specific question.
  39.  *
  40.  * @param   int     Question Id
  41.  * @return  array
  42.  */
  43. function fetchAllAnswers($questionId) {
  44.     $results = mysql_query("
  45.        SELECT answers.answerid
  46.             , answers.answer
  47.          FROM answers as answers
  48.         WHERE answers.questionid = $questionId
  49.         ORDER
  50.            BY answers.order
  51.           ASC
  52.    ");
  53.    
  54.     while ($answer = mysql_fetch_array($results)) {
  55.         $answers[] = $answer;
  56.     }
  57.    
  58.     return $answers;    
  59. }
  60.  
  61. ?>
  62.  
  63. <h1>Example Site</h1>
  64.  
  65. <?php if ($_POST['do'] == 'showanswers') : ?>
  66.     <!-- show answers start -->
  67.     <h2>Submitted Answers</h2>
  68.     <ul>
  69.     <?php foreach ($_POST['answers'] as $questionid => $answerid) : ?>     
  70.         <li>
  71.             <strong>For questionid <?php echo $questionid; ?>, you select answerid <?php echo $answerid; ?></strong><br />
  72.             insert into `user_answers` (`userid`, `questionid`, `answerid`) values (1, <?php echo $questionid; ?>, <?php echo $answerid; ?>);          
  73.         </li>      
  74.     <?php endforeach; ?>
  75.     </ul>
  76.     <br />
  77.     <br /> 
  78.     <!-- //show answers end -->
  79. <?php endif ; ?>
  80.  
  81. <form action="questions.php" method="post">
  82. <input type="hidden" name="do" value="showanswers">
  83. <table border="1" cellpadding="6" width="70%">
  84.    
  85.     <?php foreach (fetchAllQuestions() as $question) : ?>
  86.         <tr>
  87.             <td><?php echo $question['question']; ?></td>
  88.             <td>
  89.                 <select name="answers[<?php echo $question['questionid']; ?>]">
  90.                     <?php foreach(fetchAllAnswers($question['questionid']) as $answer) : ?>
  91.                     <option value="<?php echo $answer['answerid']; ?>"><?php echo $answer['answer']; ?></option>
  92.                     <?php endforeach; ?>
  93.                 </select>
  94.                
  95.             </td>
  96.         </tr>      
  97.     <?php endforeach; ?>
  98.  
  99.     <tr>
  100.         <td colspan="2">
  101.             <input type="submit" name="submit" value="submit">
  102.         </td>
  103.     </tr>
  104. </table>
  105. </form>
  106.  
  107. <?php mysql_close($dbLink);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement