PDA

View Full Version : need help with old php script



BLiZZaRD
03-03-2006, 05:37 PM
Hi there. I have an old php script that generates a personality quiz. You have seen the types... what kind of nerd are you, how goth are you, etc.

Anyway, this script works well except for the very last part.

Basically it works like this:

You browse to quizmake.php there you names your quiz, tell the script how many "personalities" to test for, and what those types are, how many questions in the quiz and then go to page 2, there you input the actual questions and answers for each personality type.

the script then uses quizmakewrite.php to turn your inputs into the GUI the player will see. It spits out the new address for the test.qz file it wrote.

When the test is taken and the questions are all answered, you click the submit button and the magic happens. Your answers are tallied, and based on which persoanlity type you answered the most questions for, that type is shown to you.

But this is where my problem is. No matter how I answer my quiz, I am ALWAYS shown the personality type that was input FIRST back on page one of quizmake.php

Here are the php pages (this is an open source GPL script, but the site that I got if from so long ago, doesn't offer support, as it is not a "free script" site, and this is one of 5 they were "giving away")

quizmake.php =


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

// 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 Name:</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>
</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'];

$name = stripslashes($name);
$title = stripslashes($title);

// 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\">";
?><input type=submit value="Submit"><?
}
?>
</form>


Then it is written with quizmakewrite.php:


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

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

$name = stripslashes($name);
$title = stripslashes($title);

// Retrieve the numbered variables
for($i=0; $i<=$types; $i++)
{
$type[$i] = $_POST["type$i"];
$types[$i] = stripslashes($types[$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: http://www.yoururl.com/yourpath/quiz.php?quiz=$name<BR><BR>(replace yoururl and yourpath with the URL and path of the quiz script on your server)"
?>


you then browse to quiz.php with the ?=blah blah
quiz.php=


<?
// 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');
?>


And finally the results are calculated with this one:
quizresults.php=


<?
// 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 ($$qvar);
}

// Determine the dominant answer type
$dominant = 1;
$domval = $nowval[1];
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];

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


ANY help with how I can make the final stage work properly would be AWESOME!

Thank you