Hi everyone, I'm the webmaster for my university's library. For some time now, they've had me working on updating an old "Information Literacy Tutorial" that was originally a University of Texas project, back in the early 00s. Anyways, we were just about ready to launch it, when the geniuses in our IT department decided to do something with the PHP on the server that our page was on. Long story short, it broke everything.
I've been through most of the other pages and managed to fix them all, but there is one block of pages that I cannot get to work, those are the pages for the quizzes at the end of each section. I'm at a loss for why these have suddenly stopped working, I'm not really that experienced with PHP. Can anyone spot the problem(s) in these?
First question in one of the quizzes:
Code:
<?php
require_once "../../include/functions.php";
$question = 1;
//Check that question hasn't been answered
$msg = chk_question($next_question);
if ($msg != "")
$next_question = 1;
//Check that at least one response has been entered
if (count($q1) < 2 && $next_question == 2){
$err_msg = "Please select two choices";
$next_question = 1;
}
if (count($q1) > 2 && $next_question == 2){
$err_msg = "Please select only two choices";
$next_question = 1;
}
//Print and record question results
if ($next_question == 2){
$total = count($q1);
for($i = 0; $i < $total; $i++){
$response .= "q1:".$q1[$i];
if ($i < ($total - 1))
$response .= "&";
}
$msg .= "<h4>Answers to Question One:</h4>";
if ($q1[0] == "journal" && $q1[1] == "index"){
$msg .= "<b>You are correct.</b>
Periodical indexes
help you determine which journals have scholarly articles on
your topic.<br>";
$correct = 2;
}else if ($q1[0] == "journal" || $q1[1] == "journal"){
$msg .= "<b>Well, you are half right.</b>
Journals contain scholarly articles. The other correct answer is
the source that indexes those journal articles.<br>";
$correct = 1;
}else if ($q1[0] == "index" || $q1[1] == "index"){
$msg .= "<b>Well, you are half right.</b>
Periodical indexes do help you locate scholarly articles. But
magazines, newspapers and email are not the best sources for
finding scholarly materials.<br>";
$correct = 1;
}else{
$msg .= "<b>Hmmmm.</b>
Neither of these answers are correct. Perhaps the phrase of
\"scholarly information\" was confusing. An example of scholarly
information is a journal article written by a person who has done
research in their area of expertise. Often these articles are
indexed by periodical indexes so you can easily find them.<br>";
$correct = 0;
}
$response = $_COOKIE["response"];
$response .= "+q1:".$q1[0]."&q1:".$q1[1];
setcookie ("response", $response);
setcookie ("next", "2");
setcookie ("ans[0]", $correct);
$msg .= "<p><div align='center'><a href='quiz.php'>Go on to question 2</a></div></p>";
}
require_once "../../include/quiz_header.html";
?>
<h2>Question One:</h2>
<h3 align="CENTER">
Imagine you have an assignment to write a paper based on scholarly<br>
information. Which would be the most appropriate sources to use?
(Choose two.)</h3>
<form method="POST" action="q1.php">
<div align="CENTER">
<table border="0" width="60%" cellspacing="2" cellpadding="2">
<tr>
<td>
<?php
if ($q1[0] == "journal"):
print "<input type=\"checkbox\" name=\"q1[]\" value=\"journal\" CHECKED>";
else:
print "<input type=\"checkbox\" name=\"q1[]\" value=\"journal\">";
endif;
?>
A. Journal
</tr>
<tr>
<td>
<?php
if ($q1[0] == "Magazine" || $q1[1] == "Magazine" ):
print "<input type=\"checkbox\" name=\"q1[]\" value=\"Magazine\" CHECKED>";
else:
print "<input type=\"checkbox\" name=\"q1[]\" value=\"Magazine\">";
endif;
?>
B. Magazine
</tr>
<tr>
<td>
<?php
if ($q1[0] == "Newspaper" || $q1[1] == "Newspaper" ):
print "<input type=\"checkbox\" name=\"q1[]\" value=\"Newspaper\" CHECKED>";
else:
print "<input type=\"checkbox\" name=\"q1[]\" value=\"Newspaper\">";
endif;
?>
C. Newspaper
</tr>
<tr>
<td>
<?php
if ($q1[0] == "index" || $q1[1] == "index" ):
print "<input type=\"checkbox\" name=\"q1[]\" value=\"index\" CHECKED>";
else:
print "<input type=\"checkbox\" name=\"q1[]\" value=\"index\">";
endif;
?>
D. Periodical index
</tr>
<tr>
<td>
<?php
if ($q1[1] == "email" ):
print "<input type=\"checkbox\" name=\"q1[]\" value=\"email\" CHECKED>";
else:
print "<input type=\"checkbox\" name=\"q1[]\" value=\"email\">";
endif;
?>
E. Email
</tr>
</table>
<p>
<input type="hidden" name ="next_question" value="2">
<?php
if ($msg == ""){
//is msg != "" then the form has been submitted
print "<input type=\"submit\" value=\"submit\">";
}
?>
</p>
</div>
</form>
<?php
//Print error message or results
if ($err_msg != ""){
print "
<script language=\"JavaScript\">
<!--
onLoad=window.alert(\"$err_msg\")
-->
</script>";
}
print "$msg";
require_once "../../include/quiz_footer.html";
?>
The included "Functions" file:
Code:
<?php
function chk_question( $question ){
$msg = "";
$next_q = $_COOKIE["next"];
if( $next_q == "" )
$next_q = 1;
if( ( $next_q >= $question ) && $question != "" ){
$msg .= "<div class='redText' align=\"CENTER\">You have already answered this question.<br>
Please move on to <a href='quiz.html'>question $next_q</a></div>";
}
return $msg;
}
function percent( $num, $denom ){
if( $denom == 0 ) /* undefined */
return 0;
else
return round ( $num / $denom * 100 )."%";
}
function colorPercent( $num, $denom ){
$percent = percent( $num, $denom );
if( $percent > 75 )
$msg = "<span class='greenText'>";
else if ( $percent > 50 )
$msg = "<span class=\"orangeText\">";
else
$msg = "<span class=\"redText\">";
$msg .= $percent."</span>";
return $msg;
}
function formatQuiz( $quiz_ary, $correct_ary ){
$total_questions = 0;
$total_correct = 0;
$msg = "
<table width=60% cols=4 cellpadding=0 cellspacing=1 border=0>
<tr><th align='left'>Question</td><th align='center'># Correct</th>
<th align='right'># Incorrect</th><th align='right'>Percent</th></tr>
<tr><td colspan=4><hr noshade></td></tr>";
for ( $x = 0; $x < count( $quiz_ary ); $x++ ){
$msg .= "<tr><td align='left'>".( $x + 1 )."</td><td align='center'>".$quiz_ary[$x]."</td>
<td align='right'>".( $correct_ary[$x] - $quiz_ary[$x] )."</td>
<td align='right'>".colorPercent( $quiz_ary[$x], $correct_ary[$x] )."</td></tr>\n";
$total_correct += $quiz_ary[$x];
$total_incorrect += $correct_ary[$x] - $quiz_ary[$x];
$total += $correct_ary[$x];
}
$msg .= "<tr><td colspan=4><hr noshade></td></tr>
<tr><th align='left'>Total</th><th align='center'>$total_correct</th><th align='right'>$total_incorrect</th>
<th align='right'>".colorPercent( $total_correct, $total )."</th></tr></table>";
return $msg;
}
function getUserName ( $reg_id ){
/* Print user name on report */
if ( $reg_id == "" ):
$cn = "Guest";
else:
/* mySql Operations */
require "../../include/db_info.php"; //Database Vars
$link = mysql_connect( $host, $uname, $pass ) //Connect to the Database
or die ( "Could not connect to database" );
mysql_select_db( $db )
or die ( "Could not select database" );
$sql = "SELECT first_name, last_name
FROM person
WHERE person_id = $reg_id";
$result = mysql_query( $sql );
$row = mysql_fetch_row( $result );
$cn = $row[0]." ".$row[1];
endif;
return $cn;
}
?>
Let me know if you need any more information than these files provide. The PHP version on the server is 5.1.6. Thanks!