8 Replies - 1769 Views - Last Post: 14 March 2008 - 07:01 PM Rate Topic: -----

#1 dls76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-February 08

Multi Demensional Arrays

Posted 14 March 2008 - 02:15 PM

I have no idea where to start on this! I have an array containing questions and an array containing the four multiple choices for those questions. So I need $all_questions[0] = array ($all_choices[0], $all_choices[1], $all_choices[2], $all_choices[3]). I need it to go through the whole array and set 4 choices to each question. Can someone give me atleast a little tip on this. Right now I just have it printing a random question.
<!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

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 
$all_choices = read_file ( "choices.txt" ); 

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

//get 4 random question and choices and print
$limit = rand(0, (count($all_questions)-1));

echo $all_questions[$limit];

?>

</body>

</html>


This post has been edited by dls76: 14 March 2008 - 06:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Multi Demensional Arrays

#2 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Multi Demensional Arrays

Posted 14 March 2008 - 05:51 PM

Well, there a number of ways you could go about this. The three that spring to mind immediately are:
  • If you know that the pattern will repeat every six lines, then keep a counter running as you move through the file, and assign each line to the required array based on your counter's 1-6. This idea is dangerous because it doesn't allow for errors in the text file.
  • Insert Character beofre each line in your text file, for example: Q. for a question, P1. for possible answer 1 etc.
  • Use XML rather than a text file. XML allows you to create structured documents that make sense on their own, and are easy to read with php. This would be my preferred technique.

Good luck!
Was This Post Helpful? 0
  • +
  • -

#3 dls76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-February 08

Re: Multi Demensional Arrays

Posted 14 March 2008 - 06:07 PM

Well I got it to read the second file so I left the questions, choices, answers in seperate txt files. I know I'm going to get completely confused by doing this but I'm trying to do an array inside an array. I edited the post above which has my code and what I'm trying to do.

I have never used php before this semester and the instructor has the students teaching the leasons and when someone ask a question she just tells us to figure it out.

I really appreciate any help on this. Even if it is just an example so I can see the syntax.
Was This Post Helpful? 0
  • +
  • -

#4 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Multi Demensional Arrays

Posted 14 March 2008 - 06:26 PM

Try this (untested) code:
$mixed_choices = read_file ( "choices.txt" );

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

//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 choices and print
$limit = rand(0, (count($all_questions)-1));

echo $all_questions[$limit] . "<br />";

//output the possible choices
for($i = 0;$i < count($all_choices[$limit]); $i++) {
	echo $all_choices[$limit][$i] . "<br />";
}



I hope that works, its 1.30am here and I'm tired!

EDit: I knew I was too tired, errors fixed!

This post has been edited by thehat: 14 March 2008 - 06:46 PM

Was This Post Helpful? 0
  • +
  • -

#5 dls76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-February 08

Re: Multi Demensional Arrays

Posted 14 March 2008 - 06:46 PM

I'm getting the Parse error: syntax error, unexpected T_INC, expecting ')' msg for this line:
for($i = 0; $i < count($mixed_choices); i++)

I don't see why, though!
Was This Post Helpful? 0
  • +
  • -

#6 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Multi Demensional Arrays

Posted 14 March 2008 - 06:47 PM

Loads of reasons, I've fixed it now though :P

Edit: that particular one was a missing $ from i++

This post has been edited by thehat: 14 March 2008 - 06:48 PM

Was This Post Helpful? 1

#7 dls76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-February 08

Re: Multi Demensional Arrays

Posted 14 March 2008 - 06:52 PM

I've been looking at this way too long! I looked at that line 3 or 4 times and didn't see it.
Was This Post Helpful? 0
  • +
  • -

#8 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Multi Demensional Arrays

Posted 14 March 2008 - 06:55 PM

Yeah, sorry about that. I've got to learn to test first, post second! So anyway, that should set you on your way now. The answers will be just like the questions, and adding html form bits around your current output should be pretty straight forward. Now I think it's time to get some sleep.

Enjoy :)
Was This Post Helpful? 0
  • +
  • -

#9 dls76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-February 08

Re: Multi Demensional Arrays

Posted 14 March 2008 - 07:01 PM

Thank you so much for your help. Have a good night!

This post has been edited by dls76: 14 March 2008 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1