PHP Array Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

PHP Array Question

Post 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
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post 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 :)
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

Danke,

Seems to be working great!


Saethyr
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

yep you can modify/customize it to meet your demands
:)

happy phping

-devork
Post Reply