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

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




Multi Demensional Arrays

 
Reply to this topicStart new topic

Multi Demensional Arrays

dls76
14 Mar, 2008 - 01:15 PM
Post #1

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

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.
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

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 Mar, 2008 - 05:00 PM
User is offlineProfile CardPM
+Quote Post

thehat
RE: Multi Demensional Arrays
14 Mar, 2008 - 04:51 PM
Post #2

D.I.C Head
Group Icon

Joined: 28 Feb, 2008
Posts: 217


Dream Kudos: 100
My Contributions
Well, there a number of ways you could go about this. The three that spring to mind immediately are:
  1. 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.
  2. Insert Character beofre each line in your text file, for example: Q. for a question, P1. for possible answer 1 etc.
  3. 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!

User is offlineProfile CardPM
+Quote Post

dls76
RE: Multi Demensional Arrays
14 Mar, 2008 - 05:07 PM
Post #3

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

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.
User is offlineProfile CardPM
+Quote Post

thehat
RE: Multi Demensional Arrays
14 Mar, 2008 - 05:26 PM
Post #4

D.I.C Head
Group Icon

Joined: 28 Feb, 2008
Posts: 217


Dream Kudos: 100
My Contributions
Try this (untested) code:
php

$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 Mar, 2008 - 05:46 PM
User is offlineProfile CardPM
+Quote Post

dls76
RE: Multi Demensional Arrays
14 Mar, 2008 - 05:46 PM
Post #5

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

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!
User is offlineProfile CardPM
+Quote Post

thehat
RE: Multi Demensional Arrays
14 Mar, 2008 - 05:47 PM
Post #6

D.I.C Head
Group Icon

Joined: 28 Feb, 2008
Posts: 217


Dream Kudos: 100
My Contributions
Loads of reasons, I've fixed it now though tongue.gif

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

This post has been edited by thehat: 14 Mar, 2008 - 05:48 PM
User is offlineProfile CardPM
+Quote Post

dls76
RE: Multi Demensional Arrays
14 Mar, 2008 - 05:52 PM
Post #7

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

I've been looking at this way too long! I looked at that line 3 or 4 times and didn't see it.
User is offlineProfile CardPM
+Quote Post

thehat
RE: Multi Demensional Arrays
14 Mar, 2008 - 05:55 PM
Post #8

D.I.C Head
Group Icon

Joined: 28 Feb, 2008
Posts: 217


Dream Kudos: 100
My Contributions
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 smile.gif
User is offlineProfile CardPM
+Quote Post

dls76
RE: Multi Demensional Arrays
14 Mar, 2008 - 06:01 PM
Post #9

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 18

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

This post has been edited by dls76: 14 Mar, 2008 - 06:15 PM
User is offlineProfile CardPM
+Quote Post

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

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