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.

Edited by dinita
Link to comment
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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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