Jump to content

Recommended Posts

Well, in this code which i created:

<?php 
$name2 = $_REQUEST['name'] ;
$name = strip_tags($name2);
$ip = $_SERVER['REMOTE_ADDR']; 


if($name == '') {print "<center><br /><br /><b><a href=\"javascript:history.go(-1)\">You have not entered a name, click here to go back.</a></b></center>";}
else {
?>

<?php
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];
$q5 = $_POST['q5'];
$q6 = $_POST['q6'];
$q7 = $_POST['q7'];
$q8 = $_POST['q8'];
$q9 = $_POST['q9'];
$q10 = $_POST['q10'];
$q11 = $_POST['q11'];
$q12 = $_POST['q12'];
$q13 = $_POST['q13'];
$q14 = $_POST['q14'];
$q15 = $_POST['q15'];
$q16 = $_POST['q16'];
$q17 = $_POST['q17'];
$q18 = $_POST['q18'];
$q19 = $_POST['q19'];
$q20 = $_POST['q20'];

$score = 0;
if ($q1== 'a')
$score++;
if ($q2 == 'b')
$score++;
if ($q3 == 'b')
$score++;
if ($q4 == 'a')
$score++;
if ($q5 == 'a')
$score++;
if ($q6 == 'b')
$score++;
if ($q7 == 'c')
$score++;
if ($q8 == 'a')
$score++;
if ($q9 == 'd')
$score++;
if ($q10 == 'a')
$score++;
if ($q11 == 'b')
$score++;
if ($q12 == 'd')
$score++;
if ($q13 == 'a')
$score++;
if ($q14 == 'e')
$score++;
if ($q15 == 'b')
$score++;
if ($q16 == 'c')
$score++;
if ($q17 == 'd')
$score++;
if ($q18 == 'a')
$score++;
if ($q19 == 'b')
$score++;
if ($q20 == 'd')
$score++;

$db;
$sql="INSERT INTO mstest2_scores (name, score, ip) VALUES ('".$name."','".$score."','".$ip."')";

if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }
    echo "Success! Go here to see a list of everyone's scores: <a href=\"/?p=msresultscores2\">All Scores</a>";

echo '<h1>Your Results for the <b>Quiz</b>!</h1>';

echo '<b>Your knowledge score was ' . $score . '/20</b><br><br>';
if ($score <= 10)
echo 'You failed the test.';
else if ($score == 20)
echo 'Congratulations, you got a perfect score.';
else
echo 'Not bad, you scored average.'; 
?>
<?php
$result = mysql_query("SELECT * FROM mstest2_qa");

while($row = mysql_fetch_array($result))
  {
  $question_number =  $row['question_number'];
  $question = $row['question'];
  $answer = $row['answer'];
  $row_a = $row['a'];
  $row_b = $row['b'];
  $row_c = $row['c'];
  $row_d = $row['d'];
  $row_e = $row['e'];
  $row_f = $row['f'];
  $row_g = $row['g'];
  $row_h = $row['h'];
  

echo ('<b><u>Question '.$question_number.': '.$question.'</u></b><br />');

if($row_a == ""){ } else{
echo (' a.  '.$row_a.' <br />'); }

if($row_b == ""){ } else{
echo ('b. '.$row_b.' <br />'); } 

if($row_c == ""){ } else{
echo ('c. '.$row_c.' <br />'); } 

if($row_d == ""){ } else{
echo ('d. '.$row_d.' <br />'); } 

if($row_e == ""){ } else{
echo ('e. '.$row_e.' <br />'); } 

if($row_f == ""){ } else{
echo ('f. '.$row_f.' <br />');
} 

if($row_g == ""){ } else{
echo ('g. '.$row_g.' <br />');
} 

if($row_h == ""){ } else{
echo ('h. '.$row_h.' <br />');
}


$showanswer = $q1++;

echo '<u><i>You Answered: ' . $showanswer . '<br></u>';
echo '<u>Correct Answer: '.$answer.'<br></i></u><hr>';
  }
mysql_close($conn);
}
?>

 

Right where it says $showanswer = q1++;

Instead of it going a,b,c,d,e,f and so on.

I need to be able to change that variable so it becomes

q1,q2,q3,q4,q5,q6 and so on.

 

How do i get it to do that instead?

I re-wrote your code to a simpler, properly indented example.

 

if (!empty($_POST)) {
  $answers = array(
    ''/*!important*/, 'a', 'b', 'b', 'a', 'a', 'b', 'c', 'a', 'd', 'a', 'b',
    'd', 'a', 'e', 'b', 'c', 'd', 'a', 'b', 'd');
  
  $score = 0;
  foreach ($answers as $key => $value) {
    if (isset($_POST['q' . $key])) {
      if ($value === $_POST['q' . $key]) {
        $score += 1;
      }
    }
  }
  
  $sizeof = sizeof($answers) - 1;
  if ($sizeof === $score) {
    echo 'Congratulations, you got a perfect score.';
  } else if (floor($sizeof / 2) < $score) {//11+
    echo 'Not bad, you scored average.';
  } else {
    echo 'You failed the test.';
  }
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.