Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating questions from CSV file

Status
Not open for further replies.

drimades

IS-IT--Management
Nov 8, 2004
221
0
0
MK
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.
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?
 
read the data into an array (fgetcsv)
randomly shuffle the array
select questions in the new array order until the total becomes 20 or above.

if more than 20 deselect the last question and then iteratively select the next until the total becomes 20. if that does not work, deselect the last two questions and repeat or reshuffle completely and repeat.

or decide that each test will contain x * 2 pt, y *3pt and z *4pt questions and put each 'pointed' question into an independent array and shuffle each, then simply select the right number of questions from each pot.
 
thanks!
it's a useful solution. I m trying it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top