Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,910 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,739 people online right now. Registration is fast and FREE... Join Now!




Escaping php - strings are stored in array from txt file

 
Reply to this topicStart new topic

Escaping php - strings are stored in array from txt file

dls76
15 Mar, 2008 - 03:08 PM
Post #1

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

I'm having a couple of problems. We are creating a test that generates 5 random questions from a txt file. How do I escape php when the questions are stored in an array?

For example a True False question: PHP is enclosed using the <?php and ?>.
Is printing as: PHP is enclosed using the .

The other problem is when the test is graded it is always 0. I'm not sure if I'm storing the users answers and key wrong or if I'm compairing them wrong.

Thank you for your looking at it.

CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Midterm</title>
</head>
<body>

<?php
//This is midterm.php
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~ E_NOTICE);

//read questions file
function read_file ( $file ){
  
//get handle to file
$handle = fopen ( $file , "r" );
  
//if $handle is false, the file wasn't found
if ( $handle == false ){
  die ( "File not found!" );//notify programmer of error
}
  
//get size of file
$size = filesize ( $file );
  
// if size is zero, the file contains nothing
  if ($size == 0 ){
  die ( "This file contains nothing!" );//notify programmer of error
}
  
//read contents of file using fread
$contents = fread ( $handle, $size );
  
//if $contents is NULL, the file is empty
if ( $contents == NULL ){
  die ( "This file is empty!" );
}
  
//close handle to file
fclose ($handle);
  
//return file contents
return $contents;
}

//read file questions.txt
$all_questions = read_file ( "questions.txt" );

//use explode to put list of questions separated by a ~ into an array
$all_questions = explode ( "~" , $all_questions );

//read file choices.txt
$mixed_choices = read_file ( "choices.txt" );  
  
//use explode to put list of choices separated by a ~ into an array  
$mixed_choices = explode ( "~" , $mixed_choices );

//read file answers.txt
$answers = read_file ( "answers.txt" );  
  
//use explode to put list of answers separated by a ~ into an array  
$answers = explode ( "~" , $answers );  

//loop for five questions
for ($q = 1; $q <= 5; $q++){
  
//prepare counters for the outer and inner arrays  
$arrCount = 0;  
$itemCount = 0;  
  
//loop through the $mixed_chices array, putting every 4 items into a  
//different index of all_choices array  
for($i = 0; $i < count($mixed_choices); $i++) {  
    //if there are 4 items in the current index of the outer array, change index  
    if($itemCount == 4) {  
        $itemCount = 0;  
        $arrCount++;  
    }  
    $all_choices[$arrCount][$itemCount] = $mixed_choices[$i];  
    $itemCount++;  
}  
  
//get 4 random question and their choices and print  
$limit = rand(0, (count($all_questions)-1));  

echo "<form action='grade.php' method='post'>" . $all_questions[$limit] . "<br />";  
  
//output the possible choices  
for($i = 0; $i < count($all_choices[$limit]); $i++) {

// $value = 'a';
//set up $value to hold value of radio button
if ( $i == 0){
    $value = "A";
}
else if ( $i == 1){
    $value = "B";
}
else if ( $i == 2){
    $value = "C";
}
else if ( $i == 3){
    $value = "D";
}

echo "<input type='radio' name='<?php $limit ?>' value='<?php $value ?>' />" . $all_choices[$limit][$i] . "<br />";
    
//store question names in $question_name array
$question_name = array ( );
array_push ( $question_name, $limit);
}  
echo "<br />";

//add correct answers to $answer_key
$answer_key = array ( );
array_push ($answer_key, $answers[$limit]);

}
//submit button
echo "<br /> <input type='submit' name='submit' value='Grade Exam' /> </form>";

?>

</body>
</html>


CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Midterm</title>
</head>
<body>

<?php
//This is grade.php
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~ E_NOTICE);

//compare $answer_key to value of $question_name

//loop until all answers have been checked for each question
for ( $x = 0; $x <= (count($all_questions) -1); $x++){    
    if($answer_key[x] === $question_name[x]){       //compare user input to key
        $correct++;                                       //track number correct
    }
}
//calculate and display grade
$grade = ((100/(count($all_questions) -1))) * $correct;
echo "<h4>Your Grade: <br />" . $grade;

?>

</body>

</html>

User is offlineProfile CardPM
+Quote Post

thehat
RE: Escaping Php - Strings Are Stored In Array From Txt File
15 Mar, 2008 - 04:01 PM
Post #2

D.I.C Head
Group Icon

Joined: 28 Feb, 2008
Posts: 217


Dream Kudos: 100
My Contributions
For the first part of your question, use htmlspecialchars() to make html entities safe as you read them from the file:

php

$contents = htmlspecialchars(fread( $handle, $size ));


Now to look at the second part...
User is offlineProfile CardPM
+Quote Post

dls76
RE: Escaping Php - Strings Are Stored In Array From Txt File
15 Mar, 2008 - 04:31 PM
Post #3

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

Oh my, I'd been trying to manually adjust the array and/or the text file.
I took a break and ate supper. Maybe I'll be able to see the other problem now. I think this is one of those times that I can't see the forest because of the trees! You have been a great help on this! I really appreciate it.
User is offlineProfile CardPM
+Quote Post

thehat
RE: Escaping Php - Strings Are Stored In Array From Txt File
15 Mar, 2008 - 05:49 PM
Post #4

D.I.C Head
Group Icon

Joined: 28 Feb, 2008
Posts: 217


Dream Kudos: 100
My Contributions
For your second problem, the main issue is that php data is not passed between pages unless you incorporate it into your form. On the second page, all the data that is sent by your form is held in the $_POST array. You can pass your users selections in this manner no problem, but the correct answers shouldn't be passed like this because they would be visible to anyone viewing the source of the page. These are a few things I would do in your place:

Firstly, you should only have one form tag which you should write outside the five times loop, with one submit button and the closing form tag after the 5 times loop finishes. This will then only post the page when all questions are answered, and will post all the answers at once.

Set the name of your radio buttons to the outer loop iteration (the one that runs 5 times). Radio buttons of the same name will be grouped, and you can access a radio group with the name group1 as $_POST['group1'] in your grade page.

When you select a random number in each loop iteration, remove the chosen index from your arrays with splice. This will prevent you from accidentally displaying the same question twice. Append the random number in each iteration to a string, then after your loop has finished, create a hidden input in your form with the string of random numbers as it's value. On the grade page, read in the answers text file, then for each group of radio buttons, splice the correct answer from your answers array using the numbers in the string. This will ensure that for each set of answers the array remains the same length and order as your questions and choices arrays did on the previous page.


Sorry I didn't have time to write any code here, but hopefully it's all understandable and gives you an idea of what you need to do next. Good luck!


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 09:57PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month