Jump to content

[SOLVED] PHP MySql and Radio Values


freddyw

Recommended Posts

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?

Link to comment
Share on other sites

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>");

 

}

}

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

<?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.

 

Link to comment
Share on other sites

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?

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.