morocco-iceberg Posted May 23, 2010 Share Posted May 23, 2010 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 Link to comment https://forums.phpfreaks.com/topic/202626-trouble-with-a-function-within-a-function/ Share on other sites More sharing options...
morocco-iceberg Posted May 23, 2010 Author Share Posted May 23, 2010 *I've removed the echo commands from before options(); but that hasn't fixed it.. Link to comment https://forums.phpfreaks.com/topic/202626-trouble-with-a-function-within-a-function/#findComment-1062129 Share on other sites More sharing options...
kenrbnsn Posted May 23, 2010 Share Posted May 23, 2010 Move the function definition outside the while loop. You only have to define the function once, then you can use it many times. Ken Link to comment https://forums.phpfreaks.com/topic/202626-trouble-with-a-function-within-a-function/#findComment-1062133 Share on other sites More sharing options...
morocco-iceberg Posted May 23, 2010 Author Share Posted May 23, 2010 I don't really understand what you mean.. I can't see where the function is in a while loop? I'm probably blind... Link to comment https://forums.phpfreaks.com/topic/202626-trouble-with-a-function-within-a-function/#findComment-1062134 Share on other sites More sharing options...
kenrbnsn Posted May 23, 2010 Share Posted May 23, 2010 My mistake. I didn't see the closing bracket. Anyway you can't have a function defined inside another function, define it outside the function. Ken Link to comment https://forums.phpfreaks.com/topic/202626-trouble-with-a-function-within-a-function/#findComment-1062138 Share on other sites More sharing options...
morocco-iceberg Posted May 23, 2010 Author Share Posted May 23, 2010 Lol, I'm still learning what I can and can't do syntax wise thanks for the help! I took the function outside and its perfect. Link to comment https://forums.phpfreaks.com/topic/202626-trouble-with-a-function-within-a-function/#findComment-1062141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.