Jump to content

Itterating Through Arrays


dinita

Recommended Posts

Please excuse my terrible ability to explain what I'm trying to do,

 

my aim is to draw questions and answers from a database to be output in flash.

 

My problem is: I have created an array listing all of the question ids for a particular quiz, I would like to create another array that lists all the enteries in the database that have those id's so far I have managed to create an array which shows the last ID's answers.

 

what i want is to create 8 seperate arrays not just one?

 

foreach ($quesid as $id)
{
$sql = mysql_query("select answer FROM answers WHERE question_ID= $id ");
$answers = array();
while($row =mysql_fetch_row($sql))
{

$answers[] = $row[0];
}
}
print_r ($answers);
echo "questions=" . $question."&";
print_r ($quesid);

 

here is what is output:

 

Array ( [0] => Bulgaria [1] => Nicaragua [2] => Albania [3] => Romania ) questions=The ‘Sea Swallow’ is an alternative name for which bird?/In which sport would you see a ‘Western Roll’?/Who is better known as ‘Herbert Khaury’?/'Diet' is the parliament of which country?/What is the real first name of Coco Chanel?/'The Aztecs' were natives of which country?/What was invented by‘O.A. North’ in 1869?/King Zog was the ruler of which country?&Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 )

 

 

the first array is what i need but i need it 7 other times.

Link to comment
https://forums.phpfreaks.com/topic/269855-itterating-through-arrays/
Share on other sites

Don't run queries in loops - very inefficient.

 

<?php

$answers = array();
$qids = join(',', $quesid);
$sql = mysql_query("select question_ID, answer FROM answers WHERE question_ID IN ($qids) ");
while($row =mysql_fetch_row($sql))
{
  $answers[$row[0]][] = $row[1];
}

// wiew array
echo '<pre>'.print_r($answers, 1).'</pre>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.