...

View Full Version : Array an database row



MHaris
08-17-2007, 09:49 PM
I have a table called answers.

The structure of the table is:

id
candidate_id
question_id
answer

I want to create an array based on candidate_id answer and in question_id order.

For example,

Answer of question(question_id) 1 is 1(answer) answered by candidate(candidate_id) 2
Answer of question_id 2 is 2 answered by candidate_id 2
Answer of question_id 3 is 1 answered by candidate_id 2
Answer of question_id 1 is 1 answered by candidate_id 2
Answer of question_id 1 is 2 answered by candidate_id 2

So the array would be


$array = array(
'candidate_2' => array(1,2,1,1,2);
)

I have around 15 candidates and 15 questions.

mlse
08-17-2007, 10:08 PM
How about:



if ($result = mysql_query("select * from mytable") === FALSE) die ("error!");
while ($row = mysql_fetch_assoc($result)) $binned["candidate_".$row["candidate_id"]][] = $row["answer"];
mysql_free_result($result);


That would produce an array in $binned like the one you specify in your code snippet.

MHaris
08-17-2007, 10:39 PM
How about:



if ($result = mysql_query("select * from mytable") === FALSE) die ("error!");
while ($row = mysql_fetch_assoc($result)) $binned["candidate_".$row["candidate_id"]][] = $row["answer"];
mysql_free_result($result);


That would produce an array in $binned like the one you specify in your code snippet.

Very nice, thank you. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum