Hi!
I have a CSV file with some sample questions. The fields of the CSV file are as follows:
number of the question, text of the question, points of the question
Now I m reading the file in php and trying to generate a number of questions from it.
I need to generate a test with 20 points total. The questions have different amount of points (2, 3, 4, etc.) and of course with the solution above it generates a test with more than 20 points. Any idea?
I have a CSV file with some sample questions. The fields of the CSV file are as follows:
number of the question, text of the question, points of the question
Now I m reading the file in php and trying to generate a number of questions from it.
Code:
<?PHP
$file_handle = fopen("hum131cepele.csv", "r");
$tezat = array();
//print "<table border=1>";
$i = 0;
$count_lines = 0;
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
$count_lines = $count_lines + 1;
};
echo "<br>";
// print "</table>";
$rndnrs = array();
while ( count($rndnrs) < 10 ) { // generate 10 unique random numbers
$x = mt_rand(1,$count_lines);
if ( !in_array($x,$rndnrs) ) { $rndnrs[] = $x; }
}
rewind($file_handle);
// printing the questions of the test
print "<table border=1>";
$totpoints = 0; // total point for the test
$currpoints = 0;
while (!feof($file_handle) && $totpoints < 20) {
$line_of_text = fgetcsv($file_handle, 1024);
$current = (int)$line_of_text[0];
if (in_array($current,$rndnrs))
{
print "<tr>";
print "<td>".$line_of_text[0] ."</td><td>". $line_of_text[1]."</td><td>". $line_of_text[2]."</td>";
$currpoints = (int)$line_of_text[2];
$totpoints = $totpoints + $currpoints;
print "</tr>";
};
};
fclose($file_handle);
?>
I need to generate a test with 20 points total. The questions have different amount of points (2, 3, 4, etc.) and of course with the solution above it generates a test with more than 20 points. Any idea?