Jump to content

Recommended Posts

Hi Guys,

I have these two files one is a form with questions and the other is a results.php file. The form has 8 questions with 4 possible answers for each one namely a,b,c,d

I have managed to get the results and display them but I want to say to the user that if mostly a's were selected then it must display what is given in my if..elseif...else loop for a and likewise for b,c or d

here is my results file so far:

 

<?php
$title = "Quiz Results";
echo "<title>$title</title>";
// Below gets the answers from the page before //
if (isset ($_POST['submit'])) {
   $q1 = $_POST['q1'];
   $q2 = $_POST['q2'];
   $q3 = $_POST['q3'];
   $q4 = $_POST['q4'];
   $q5 = $_POST['q5'];
   $q6 = $_POST['q6'];
   $q7 = $_POST['q7'];
   $q8 = $_POST['q8'];
}
// Below checks to see if you forgot anything //
if ($q1 == "") {
   die ("You forgot something, go back and check over your quiz.");
}
if ($q2 == "") {
   die ("You forgot something, go back and check over your quiz.");
}
if ($q3 == "") {
   die ("You forgot something, go back and check over your quiz.");
}
if ($q4 == "") {
   die ("You forgot something, go back and check over your quiz.");
}
if ($q5 == "") {
   die ("You forgot something, go back and check over your quiz.");
}
if ($q6 == "") {
   die ("You forgot something, go back and check over your quiz.");
}
if ($q7 == "") {
   die ("You forgot something, go back and check over your quiz.");
}
if ($q8 == "") {
   die ("You forgot something, go back and check over your quiz.");
}
// Below is where the answers are actually displayed //
{
        echo<<<EOT
<p>Results:<br>
1.) $q1<br>
2.) $q2<br>
3.) $q3<br>
4.) $q4<br>
5.) $q5<br>
6.) $q6<br>
7.) $q7<br>
8.) $q8</p>
<p>

EOT;
}
// This will give me a,b,c or d from each question
$str1 = substr($q1, 0, 1);
$str2 = substr($q2, 0, 1);
$str3 = substr($q3, 0, 1);
$str4 = substr($q4, 0, 1);
$str5 = substr($q5, 0, 1);
$str6 = substr($q6, 0, 1);
$str7 = substr($q7, 0, 1);
$str8 = substr($q8, 0, 1);

// ****************   Issue is from here on   ********************************

$array = array($str1, $str2, $str3, $str4, $str5, $str6, $str7, $str8);
$val = (array_count_values($array));
print_r ($val);

// $val = min(array($str1, $str2, $str3, $str4, $str5, $str6, $str7, $str8));
// print_r ($val);

if ($val=="a")
  echo "Mostly 'A's: IT. Looks like you're a bit of a tech-head. You should check out IT at Monash South Africa.";
elseif ($val=="b")
  echo "Mostly 'B's: Business & Economics. Whoa, look out business world! You should check out Business & Economics at Monash South Africa.";
elseif ($val=="c")
  echo "Mostly 'C's: Health Sciences. Open up and say, 'Aaaah.' You should check out Health Sciences at Monash South Africa.";
elseif ($val=="d")
  echo "Mostly 'D's: Arts. Your engine runs on creative juice. You should check out the Arts at Monash South Africa.";
else
  echo "You are a rare person with above normal interests, You should check out Monash South Africa.";

?>

 

I have been trying various things but I think I am complicating the code to much or something....Please help me...much appreciated

 

Link to comment
https://forums.phpfreaks.com/topic/238633-php-quiz-with-no-database/
Share on other sites

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.