简体   繁体   中英

survey php mysql counter

I am working on a small php/mysql survey. I have all my questions/answers/cateogries stored in a mysql db. I also have my categories stored in an array in my php script. categories array=("Person","A Mom","A Student","Somebody",etc) each category have 4 questions and 4 answers. there are 8 categories. a user answers the survey by moving from category 1, to the next category etc...

here is a sql query that i have hard coded the category 2, so that i can display the 4 questions that is associated with category 2: select a. , b. from category a, questions b where a.id = b.id and a.id='2'

my question is, i will like to make the number "2" dynamic. dynamic here means, when the user click on the "NEXT" button, i would like to the new sql query to be like this:

select a. , b. from category a, questions b where a.id = b.id and a.id='3'

clicking on next button again goes to: select a. , b. from category a, questions b where a.id = b.id and a.id='4' etc...

below is a code snippet:

 <?php
    $categories = array ("Person","A Mom","A Student","Somebody");

    $sql="select a.*, b.* from category a, questions b where a.id = b.id and a.id='2'";
    $result=mysql_query($sql,$db_connection);
    if (!$result)
    {
   die('Error: ' . mysql_error());
    }    
 ?>
  <tr>
    <td height="32" colspan="5" bgcolor="#ECA4DD"><b><?php echo $result['name'];?></b></td></tr>
  <tr>
  <tr>
    <td colspan="5" class="content_2"><?php echo $result['description'];?></td>
  </tr>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

     <?php
    while ($row = mysql_fetch_assoc($result)) 
    {

 ?>

   <tr>
    <td class="content_2"><?php echo "Question1"; ?></td>
    <td align="center" valign="middle"><input type="radio" name="q1" id="s1" value="<?php echo $row['q1']; ?>"/> Rare </td>
    <td align="center" valign="middle"><input type="radio" name="q1" id="s1" value="<?php echo $row['q1']; ?>"/> Sometimes</td>
    <td align="center" valign="middle"><input type="radio" name="q1" id="s1" value="<?php echo $row['q1']; ?>"/> True</td>
    <td align="center" valign="middle"><input type="radio" name="q1" id="s1" value="<?php echo $row['q1']; ?>"/> Always</td>
  </tr>
  <tr>
       <td height="108"></td>
       <td align="center" valign="middle">&nbsp;</td>
       <td align="center" valign="middle">&nbsp;</td>
       <td colspan="2" align="right" valign="middle"><input type="submit" name="save" value="NextButton"/>
       </td>
      </tr>

   <?php
   mysql_free_result($result);
   }
  ?>      

 </form>

You can pass the current question number via $_GET variables or something of that nature. So at the start of the page grab the current question:

$current_question = isset($_GET['current_question']) ? $_GET['current_question'] : '1';

And then in your query you can run something like:

$sql = "SELECT `a`.*, `b`.* FROM `category` AS `a`, `questions` AS `b` WHERE `a`.`id` = `b`.`id` AND `a`.`id` = '{$current_question}'";

In your form you need to indicate either which step you're currently on or which the next step is. This way you can increment it to move you through the form.

So, in your form, put in a hidden variable like this:

<input type='hidden' name='thisCategoryID' value='2'>

Then, in your PHP code, you can do this:

// If there was a category ID posted, set some variables and increment it to load the next section
if(isset($_POST['thisCategoryID]) && is_numeric($_POST['thisCategoryID']) {
    $currentCategoryID = $_POST['thisCategoryID'];
    $nextCategoryID = $currentCategoryID + 1;
} else {
    // There's something wrong with your input, show an appropriate error
}

// Now run your query with the updated Category ID
$sql="select a.*, b.* from category a, questions b where a.id = b.id and a.id='{$nextCategoryID}'";
$result=mysql_query($sql,$db_connection);
if (!$result) {
    die('Error: '.mysql_error());   
}

I could have done a little more to prevent against SQL injection in the above query, in this example I'm just making sure you have a numeric value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM