Page 1 of 1

PHP Array Question

Posted: Sun Dec 28, 2003 12:51 am
by Saethyr
It's late and I am having braincramps if you were pulling information that spanned many rows in a MySQL table, how would one write it to a multidimensional array, for example...

query:
"SELECT * FROM some_table"

returns:
10 rows

information I need spans 5 columns:
1. question
2. correct answer
3, 4, 5. wrong answers

How can I pull this info into an array such as the one that follows:

Code: Select all

$questAnswers = array($incrementingnum => array(0 => $question, 1 => $correctans, 2 => $wrongans1, 3 => $wrongans2, 2 => $wrongans3);
so that I end up with all 10 questions and 40 answers in a seperate array(i.e. $questAnswers[0][0] - $questAnswers[9][4], each one being a question and all it's possible answers:


Then I need to be able to shuffle each one, would i just do this using:

Code: Select all

shuffle($questAnswers[0]);

Thanks in advance,

Saethyr

Posted: Sun Dec 28, 2003 1:09 am
by devork

Code: Select all

<?php
$questAnswers = array();
$array_answers = array();
$incrementingnum = 0;

while($row=mysql_fetch_array($result) ){
// this will make an array that has 1-5 column values 
for ($i=0;$i<5,$i++){
	$array_answers[]=$row[$i];
}//for ends

// Add the questions, correct ans, and wrong ans values and increment the quesAnswers
$questAnswers[$incrementingnum++] = $array_answers

// unset this row, so that it's values are not included next time
unset($array_answers);

}//while ends

?>
hope it gives you some idea :)

Posted: Sun Dec 28, 2003 1:36 am
by Saethyr
Danke,

Seems to be working great!


Saethyr

Posted: Sun Dec 28, 2003 11:17 am
by devork
yep you can modify/customize it to meet your demands
:)

happy phping

-devork