Forum Moderators: coopster

Message Too Old, No Replies

Can I have some help with a quiz script?

         

CrystalStarLight

7:46 pm on May 13, 2008 (gmt 0)

10+ Year Member



Hi! I found this neat really old simple personality quiz maker script that is distributed under the terms of the GNU General Public License. (It means you can modify it and use it how you would like.) Its so you can make quizzes like they do at quizilla.com

<snip>

This would be amazing for my site, the only thing is people making a quiz can't upload images for their quiz results. Like someone takes a quiz an image shows up on the results page saying their personality.

So thats what I'm trying to figure out how to do. If anyone can give me any advice or if its not too much, come up with the modified script for me. It would be so helpful. I'd appreciate it very much. I've been trying to figure it out for the last week and I'm stumped. I'm also a beginner though.

Here is the script and you can download it all on the website above:

Instructions:

Mongwell Easy Quiz Maker

To use the Easy Quiz Maker, first edit the 'config.php' file. This file will allow you to set the password for the program. Look for the following line:

$password = "enter";

Change the word within the quotes (enter) to the password of your choice. Erase the word (but leave the quotes) if you don't want to use a password.

upload all the .php files to a directory on your server. The name of the directory doesn't matter, we suggest "quiz" or "quizzes" or "easyquiz"

You can set a custom HTML header and footer file for your quizzes. Simply create a header.htm and footer.htm file and upload them to the same directory as the quiz program on your server. This willallow you to have a custom template that you can easily change at will.

In order to create a new quiz, access the quizmake.php file on your server. For example, if the server name is yourserver.com and your quiz directory is "quiz", the URL would be:

http://www.example.com/quiz/quizmake.php

Make sure that you change the permissions on your quiz directory to 755.

When you make a quiz it will give you the instructions to access the URL for your specific quiz.

We recommend planning your quiz out in advance. Determine how many personality types to test for and how many questions you wish to ask. A minimum of 7 questions is recommended, but the more questions you ask the more accurate the test will be.

Your questions don't have to be completely scientific or methodical. Remember, these quizzes are just for fun. Just try to think of questions that each of your personality types will answer differently.

What you need to do is determine the answer that each personality type is most likely to give to each question. Most people will not answer
100% for any single personality type, the system is designed to tally up their answers and see which personality type they answered for most prominently.

So plan out each personality and what it's general characteristics will be, and plan out all your questions and possible answers. Once you've done that it will be easy to enter everything into the forms and create your quiz.

config.php

<?
// Set this to blank if you don't want to set a password
$password = "enter";
?>

quizmake.php (The form to make the quizzes)

<?
// This script shows the forms to create a new quiz.

include('config.php');

// Make sure that the page is set properly
$page = $_GET['page'];
if(empty($page))
{
$page = 1;
}

// Show the first page if requested
if($page == 1)
{
?>
<form action="quizmake.php?page=2" method=POST>
<table border=0>
<tr>
<td>Quiz File Name: (optional)</td>
<td><input type=text name="name"></td>
</tr>
<tr>
<td>Quiz Title:</td>
<td><input type=text name="title"></td>
</tr>
<tr>
<td>Number of Personality Types:</td>
<td><input type=text name="types"></td>
</tr>
<tr>
<td>Number of Questions:</td>
<td><input type=text name="questions"></td>
</tr>
<?
if(!empty($password))
{
?>

<tr>
<td>Password:</td>
<td><input type=password name="formpass"></td>
</tr>

<?
}
?>
</table>
<input type=submit value="Submit">
</form>
<?
}
else
{
// Retrieve the data from the previous form
$name = $_POST['name'];
$title = $_POST['title'];
$types = $_POST['types'];
$questions = $_POST['questions'];
$formpass = $_POST['formpass'];

if($formpass != $password)
{
echo "<BR><font color=FF0000>ERROR: Incorrect Password</font><BR><BR>";
die();
}

if(empty($name))
{
$name = time();
}

$name = stripslashes($name);
$title = stripslashes($title);
settype($types, 'integer');
settype($questions, 'integer');

if(($types == 0) ¦¦ ($questions == 0) ¦¦ (empty($title)))
{
echo "<BR><font color=FF0000>ERROR: Please fill in all fields properly</font><BR><BR>";
die();
}

// Change to all lower case
$name = strtolower($name);

// Replace all spaces with dashes
$name=str_replace(" ", "-", $name);

// If the name has more than 10 characters, truncate it.
if(strlen($name2) > 10)
{
$name = substr($name,0,10);
}

?>
<form action="quizmakewrite.php" method=POST>
<B>Personality Types:</B><BR>
Input the description of each personality type here that will be shown to the user.
Generally, it will start with "You are a...". You may use HTML code.
<table border=0>
<?
for($i=0; $i<$types; $i++)
{
$pnum = $i+1;

echo "<tr><td>$pnum:</td><td><textarea name=\"type$i\"></textarea></td></tr>\n";
}
?>
</table><BR>
<?
for($i=0; $i<$questions; $i++)
{
$qnum = $i+1;
echo "<B>Question $qnum: </B><input type=text name=\"question$i\">";
?>
<table border=0>
<?
for($j=0; $j<$types; $j++)
{
$pnum = $j+1;

echo "<tr><td>Answer for type $pnum:</td><td><input type=text name=\"$i-$j\"></td></tr>\n";
}
?>
</table><BR>
<?
}
echo "<input type=hidden name=\"name\" value=\"$name\">";
echo "<input type=hidden name=\"title\" value=\"$title\">";
echo "<input type=hidden name=\"types\" value=\"$types\">";
echo "<input type=hidden name=\"questions\" value=\"$questions\">";
echo "<input type=hidden name=\"formpass\" value=\"$formpass\">";
?><input type=submit value="Submit"><?
}
?>
</form>

quizmakewrite.php (The page for processing the form data after its submitted)

<?
// This script writes the quiz to a file

include('config.php');

// Retrieve the user entered data
$name = $_POST['name'];
$title = $_POST['title'];
$types = $_POST['types'];
$questions = $_POST['questions'];
$formpass = $_POST['formpass'];

if($formpass != $password)
{
echo "<BR><font color=FF0000>ERROR: Incorrect Password</font><BR><BR>";
die();
}

$name = stripslashes($name);
$title = stripslashes($title);
settype($types, 'integer');
settype($questions, 'integer');

// Retrieve the numbered variables
for($i=0; $i<$types; $i++)
{
$type[$i] = $_POST["type$i"];
$type[$i] = stripslashes($type[$i]);
}
for($i=0; $i<$questions; $i++)
{
$question[$i] = $_POST["question$i"];
$question[$i] = stripslashes($question[$i]);
for($j=0; $j<$types; $j++)
{
$answer[$i][$j] = $_POST["$i-$j"];
$answer[$i][$j] = stripslashes($answer[$i][$j]);
}
}

// Open the file for writing
$fp = fopen("$name.qz", 'w') or die("unable to open file");

// Write the data to the file
fwrite($fp, "<?\n\$title=\"$title\";\n\n\$types=\"$types\";\n\$questions=\"$questions\";\n\n");
for ($i=0; $i<$types; $i++)
{
fwrite($fp, "\$type[$i] = \"".$type[$i]."\";\n");
}
for($i=0; $i<$questions; $i++)
{
fwrite($fp, "\n\$question[$i] = \"".$question[$i]."\";\n");
for($j=0; $j<$types; $j++)
{
fwrite($fp, "\$answer[$i][$j] = \"".$answer[$i][$j]."\";\n");
}
}
fwrite($fp, "?>");
fclose($fp);

// Inform the user
echo "Your quiz has been saved. You may access it through: [yoururl.com...] yoururl and yourpath with the URL and path of the quiz script on your server)"
?>

quiz.php (The outputted questions and answers page)

<?
// This script displays the quiz selected

// Display the header
include('header.htm');

$quiz = $_GET['quiz'];
$quizfile = $quiz.".qz";

include($quizfile);
echo "<h2>$title</h2>";
?>
Answer these questions as honestly as you can. If you think more than one applies to you, just try
to pick the one that <i>most</i> applies to you.<BR><BR>
<center>
<form action="quizresults.php?quiz=<? echo $quiz ?>" method=post>
<table border=0 width="100%">
<?

// Show all the questions and allow the user to select answers
for ($i=0; $i<$questions; $i+=1)
{
?>
<tr>
<td colspan=2 bgcolor="#999999"><font color="#000000"><? echo $question[$i]; ?></font></td>
</tr>
<?
for ($j=0; $j<$types; $j+=1)
{
?>
<tr>
<td valign=top width=1><input type="radio" name="q_<? echo $i+1; ?>" value="<? echo $j+1; ?>">
<td><? echo $answer[$i][$j]; ?></td>
</td></tr>
<?
}
}
?>
</TABLE>
<BR><BR>
<input type="submit" value="Give me my results!"></center>
</form>
<?
// Display the footer
include('footer.htm');
?>

quizresults.php (The results page that shows their personality after they took the quiz)

<?
// This script determines the results of the quiz
// and displays them to the user

// Display the header
include('header.htm');

$quiz = $_GET['quiz'];
$quizfile = $quiz.".qz";

include($quizfile);

// Set up the current max value sets
for ($i=1; $i<=$types; $i+=1)
{
$nowval[$i] = 0;
}

// Add up each question's answers
for ($i=1; $i<=$questions; $i+=1)
{
$qvar = "q_$i";
tally ($_POST[$qvar]);
}

// Determine the dominant answer type
$dominant = 1;
$domval = $nowval;
for ($i=2; $i<=$types; $i+=1)
{
if ($domval < $nowval[$i])
{
$dominant = $i;
$domval = $nowval[$i];
}
}

// Function to tally the number of responses for each answer
function tally ($question)
{
global $nowval;
$nowval[$question]++;
}
?>
<h2>Your Quiz Results:</h2>
<?
echo $type[$dominant-1];

if (is_file("$quiz.rsl"))
{
$fp = fopen("$quiz.rsl", 'r');
$line = fgets($fp, 1024);
fclose($fp);
$people = explode("¦", $line);
$people[$dominant-1] += 1;

$timestaken = 0;
foreach($people as $tally)
{
settype($tally, 'integer');
$timestaken += $tally;
}

$fp = fopen("$quiz.rsl", 'w');
for($i=0; $i<$types; $i++)
{
fwrite($fp, $people[$i]."¦");
}
fclose($fp);
}
else
{
for($i=0; $i<$types; $i++)
{
$people[] = 0;
}
$people[$dominant-1] += 1;
$timestaken = 1;

$fp = fopen("$quiz.rsl", 'w');
for($i=0; $i<$types; $i++)
{
fwrite($fp, $people[$i]."¦");
}
fclose($fp);
}

$percentage = ($people[$dominant-1] / $timestaken) * 100;

$dec=2;
$format="%.$dec" . "f";
$number=sprintf($format,$percentage);
$percentage=strtok($number,".");
$dc=strtok(".");

if ($dec!=0)
{
$percentage = "$percentage" . ".$dc";
}

echo "<HR>This quiz has been taken $timestaken times. Your personality type matches $percentage% of the people who have taken this quiz.";

// Display the footer
include('footer.htm');
?>

[1][edited by: dreamcatcher at 9:18 pm (utc) on May 13, 2008]
[edit reason] No urls please! [/edit]

CrystalStarLight

11:28 am on May 14, 2008 (gmt 0)

10+ Year Member



Hi! Sorry about the url.

Also if anyone can just let me know an image uploading script I can figure out how to integrate it.

Just one that uploads images to a specified directory and displays the image onto the next page. One where I can call the image address with a variable to put in code or a textarea box. That would be so helpful. It seems like a simple script but I've been looking everywhere for something like that and haven't been able to find any.