1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mysql based multiple choice test where the answer must be included at a random order

Discussion in 'PHP' started by ingilizdili, Aug 12, 2011.

  1. #1
    Hello;
    I have devised a multiple choice vocabulary test where visitors find a sentence with one word missing. The sentence is depicted by a picture. The visitors are supposed to find the correct word. I have already made a gap filling version of it using input element. However, it is very difficult to make a guess in this version. With the one I am developing and asking help for I want to offer them a variety of choices which will -of course- include the correct answer. All the choices and the correct answer are in the database, word column. I have tried the following code and succeded! But the correct answer is always the first choice :( I want it to appear among the other choices at a random order, which is what is expected of multiple choice questions nowadays. Can you help, please. Here is the code;
    $result = mysql_query("SELECT * FROM dic ORDER BY RAND() LIMIT 5");
    
    $row = mysql_fetch_array( $result ); {
    
    $word = $row['word'];
    $id = $row['id'];
    
    echo "<table style='border-collapse: collapse' border='0' bordercolor='#c3bbbb' cellpadding='3' cellspacing='0'><tr><td colspan='3'><table border='0' height='", $row['height'], "' width='", $row['width'], "' style='table-layout:fixed; background:url(http://sozluk.ingilizdili.net/yeni-sozluk/resim/", $row['picture_1'], ") no-repeat;'><tr><td valign='top'><p align='center'>", $row['definition_1'], "<select name='kelime'><option>??</option>";
    }
    $result1 = mysql_query("SELECT * FROM dic WHERE id = '$id' ORDER BY RAND() LIMIT 5");
    while($row1 = mysql_fetch_array( $result1 )) {
    echo  "<option>", $row1['word'], "</option>";
    
    }
    
    
    $result2 = mysql_query("SELECT * FROM dic WHERE id >= '$id' ORDER BY RAND() LIMIT 5");
    while($row1 = mysql_fetch_array( $result1 )) {
    echo  "<option>", $row1['word'], "</option>";
    
    }
    
    echo "</select><input type='hidden' name='answer' value='", $word, "' />", $row['definition_2'], "</p></td></tr></table></td></tr></table>";
    PHP:

     
    Solved! View solution.
    ingilizdili, Aug 12, 2011 IP
  2. #2
    First, load data into an array instead of direct displaying then you use php.net/array_shuffle and display it to the current user :)
     
    EricBruggema, Aug 24, 2011 IP
  3. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I made it though not in a very direct way! Thank you very much. Below is the coding which other people with the same question may find useful.
    $result = mysql_query("SELECT * FROM db WHERE width != '' ORDER BY RAND() LIMIT 1");
    
    $row = mysql_fetch_array( $result ); {
    
    //answer
    $word = $row['word'];
    
    $ws = substr($word, 0, 1);
    }
    
    
    
    
    
    echo "<table border='0' width='", $row['width'], "' style='table-layout:fixed'><tr><td valign='top'><p align='center'>", $row['definition_1'], " ", "<select name='kelime' onchange='submit();'><option>", $ws, "__________</option>";
    
    //option1
    $result1 = mysql_query("SELECT * FROM db WHERE ilk_harf = '$ws' AND word != '$word' ORDER BY RAND() LIMIT 4");  //sorulan kelimeyle aynı türden kelimeleri listele
    while($row1 = mysql_fetch_array( $result1 )) {
    $kelime1 = $row1['word'];
    }
    
    //option2
    $result2 = mysql_query("SELECT * FROM db WHERE ilk_harf = '$ws' AND word != '$word' AND word != '$kelime1' ORDER BY RAND() LIMIT 1");  
    while($row2 = mysql_fetch_array( $result2 )) {
    $kelime2 = $row2['word'];
    }
    
    //option3
    $result3 = mysql_query("SELECT * FROM db WHERE ilk_harf = '$ws' AND word != '$word' AND word != '$kelime1'  AND word != '$kelime2'  ORDER BY RAND() LIMIT 1");  
    while($row3 = mysql_fetch_array( $result3 )) {
    $kelime3 = $row3['word'];
    }
    
    //option4
    $result4 = mysql_query("SELECT * FROM db WHERE ilk_harf = '$ws' AND word != '$word' AND word != '$kelime1'  AND word != '$kelime2'  AND word != '$kelime3' ORDER BY RAND() LIMIT 1");  
    while($row4 = mysql_fetch_array( $result4 )) {
    $kelime4 = $row4['word'];
    }
    $cars = array("$kelime1", "$kelime2", "$kelime3", "$kelime4", "$word");
    shuffle($cars);
    
    echo  "<option>", $cars[0], "</option><option>", $cars[1], "</option><option>", $cars[2], "</option><option>", $cars[3], "</option><option>", $cars[4], "</option>";
    
    echo "</select><input type='hidden' name='cevap' value='", $word, "' />", $row['definition_2'], "<br><img src='http://sozluk.ingilizdili.net/yeni-sozluk/resim/", $row['picture_1'], "' height='", $row['height'], "' width='", $row['width'], "'></p></td></tr></form>";
    PHP:
     
    ingilizdili, Aug 24, 2011 IP