freddyw Posted September 10, 2009 Share Posted September 10, 2009 Hi there, My aim is to create a quiz application. The application has 5 catagories. Each catagory holds 100 questions in the database. 10 questions are randomly selected for each quiz. So far I have this. Which I have suprised myself getting it to work. $conn = @mysql_connect ("localhost","freddy","password") or die ("connection error"); $rs = @mysql_select_db ("freddy", $conn) or die ("db error"); $id = rand(0,4); $sql = sprintf("SELECT * FROM test WHERE id='$id'"); $rs = mysql_query ($sql, $conn); while ($row = mysql_fetch_array ($rs)) { echo ("Question 1: " . $row["question"] ."<br><br>"); echo ("A: " . $row["answer1"] ."<br>"); echo ("B: " . $row["answer2"] ."<br>"); echo ("C: " . $row["answer3"] ."<br>"); echo ("D: " . $row["answer4"] ."<br>"); } This randomly selects a question from the database along with four possable answers. I want to advance this further. I could repeat this code ten times. However all good programmers are lazy programmers. I want to create a code that simpply says... repeat this code if question number <10. This way it will generate 10 questions. not just 1. Further to this rather than having A: answer B: answer i would like the letters to be replaced with radio buttons. any help on how to acchieve either of these tasks? Quote Link to comment https://forums.phpfreaks.com/topic/173835-solved-php-mysql-and-radio-values/ Share on other sites More sharing options...
mikesta707 Posted September 10, 2009 Share Posted September 10, 2009 well whats the range for acceptable ID numbers? 1-4? once You know that do something like for ($i = 0; $i < 10; $i++){ //this will repeat 10 times, but we need to change it //so that you get a different random number each time. //what is the acceptable range of id numbers? $id = rand(0,4); $sql = sprintf("SELECT * FROM test WHERE id='$id'"); $rs = mysql_query ($sql, $conn); while ($row = mysql_fetch_array ($rs)){ echo ("Question 1: " . $row["question"] ."<br><br>"); echo ("A: " . $row["answer1"] ."<br>"); echo ("B: " . $row["answer2"] ."<br>"); echo ("C: " . $row["answer3"] ."<br>"); echo ("D: " . $row["answer4"] ."<br>"); } } Quote Link to comment https://forums.phpfreaks.com/topic/173835-solved-php-mysql-and-radio-values/#findComment-916305 Share on other sites More sharing options...
freddyw Posted September 10, 2009 Author Share Posted September 10, 2009 Thanks very much. I only have it set to 4 at the moment as i only have 4 questions in the database. so when i whave 100 questions it will be (0,100). Quote Link to comment https://forums.phpfreaks.com/topic/173835-solved-php-mysql-and-radio-values/#findComment-916324 Share on other sites More sharing options...
mikesta707 Posted September 10, 2009 Share Posted September 10, 2009 ahh ok, well then for ($i = 0; $i < 10; $i++){ //this will repeat 10 times, but we need to change it //so that you get a different random number each time. //what is the acceptable range of id numbers? $id = rand(0,10); $temp = $id*$i; $temp += (100 - $temp > 10) ? rand(0,10) : 0; $id=$temp; $sql = sprintf("SELECT * FROM test WHERE id='$id'"); $rs = mysql_query ($sql, $conn); while ($row = mysql_fetch_array ($rs)){ echo ("Question 1: " . $row["question"] ."<br><br>"); echo ("A: " . $row["answer1"] ."<br>"); echo ("B: " . $row["answer2"] ."<br>"); echo ("C: " . $row["answer3"] ."<br>"); echo ("D: " . $row["answer4"] ."<br>"); } } btw these lines $temp = $id*$i; $temp += (100 - $temp > 10) ? rand(1,9) : 0; but this will get questions in intervals. like if $i is 1, it will choose from 11-20, if $i is 2, 21-30 and so on. I'm sure there is a better way, but this is how I would do it off the top of my head. if you give me a little while I can probably come up with a better solution Quote Link to comment https://forums.phpfreaks.com/topic/173835-solved-php-mysql-and-radio-values/#findComment-916343 Share on other sites More sharing options...
ToonMariner Posted September 10, 2009 Share Posted September 10, 2009 <?php $sql = sprintf("SELECT * FROM test ORDER BY RAND() LIMIT 0,10"); $rs = mysql_query($sql); $i=1; while ($row = mysql_fetch_array ($rs)) { echo ("Question " . $i++ .": " . $row["question"] ."<br><br>"); echo ("A: " . $row["answer1"] ."<br>"); echo ("B: " . $row["answer2"] ."<br>"); echo ("C: " . $row["answer3"] ."<br>"); echo ("D: " . $row["answer4"] ."<br>"); } ?> querying in a loop is inefficient and you have to check that the random generated has not already been used - let the database do all that for you. Quote Link to comment https://forums.phpfreaks.com/topic/173835-solved-php-mysql-and-radio-values/#findComment-916349 Share on other sites More sharing options...
mikesta707 Posted September 10, 2009 Share Posted September 10, 2009 Ahh I knew there was a much better way. Yeah use that Quote Link to comment https://forums.phpfreaks.com/topic/173835-solved-php-mysql-and-radio-values/#findComment-916351 Share on other sites More sharing options...
freddyw Posted September 10, 2009 Author Share Posted September 10, 2009 Thanks alot you two. As I've mentioned before in different threads, I'm new to PHP, I get so far learning it, then the more I learn, it seems the amount to still learn keeps increasing. I appreciate both of you for helping with this. Do we have any idea how to change the letters to radio values? Quote Link to comment https://forums.phpfreaks.com/topic/173835-solved-php-mysql-and-radio-values/#findComment-916360 Share on other sites More sharing options...
mikesta707 Posted September 10, 2009 Share Posted September 10, 2009 oh yeah my bad forgot about that echo ("<input type='radio' name='radio' value='".$row['answer1']."'> A: " . $row["answer1"] ."<br>"); Quote Link to comment https://forums.phpfreaks.com/topic/173835-solved-php-mysql-and-radio-values/#findComment-916362 Share on other sites More sharing options...
freddyw Posted September 10, 2009 Author Share Posted September 10, 2009 genius thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/173835-solved-php-mysql-and-radio-values/#findComment-916366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.