Jump to content

Trouble with a function within a function...


Recommended Posts

Hey guys, I'm having some trouble with this function (well, the function within the function really).

I need the function to echo out a random question from the designated table, this is working fine, but then I need a function within this function to pull out all the answer options and echo them into a dropdown menu.

The dropdown menu shows, but there is no data found within it, although there is definitely data contained in the table. I keep getting the error "Fatal error: Cannot redeclare options()". I've attempted to also do it using mysql_num_rows but that didn't work so I thought I would try something simpler...

 

Here's the code:

 

		function retrieveData($data){
		$$data = mysql_query('SELECT * FROM '.$data.' ORDER BY RAND()') or die(mysql_error());
		$counter = 0;
		while($row = mysql_fetch_array($$data)){
			$question[$counter] = $row['question'];
			$counter++;
		}
		function options($data){
			$i = 0;
			while($row = mysql_fetch_array($$data)){
			$answer[$i] = $row['answer'];
			$i++;
			}
			echo "<option value=\"".$answer[$i]."\">".$answer[$i]."</option>";
		}
		echo $question[0];
		echo "<select name=\"0\">";
		echo "<option value=\"0\">Please select an answer...</option>";
		echo options();
		echo "</select>";
		echo "<br/>";
		echo $question[1];
		echo "<select name=\"1\">";
		echo "<option value=\"0\">Please select an answer...</option>";
echo options();
		echo "</select>";
		echo "<br/>";
	}

 

Any help would be awesome :)

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.