Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Display Random Non repeatable Array

    Hi all,

    Can someone help me with this problem of mine..
    It seems I kind of reach a stuck point..

    So, basically, I tried to create a quiz, the questions are saved inside the database. Now, I managed to get the necessary data from the database, the problem is in how to load them with this characteristic:
    - random
    - unrepeatable

    I managed to load them in random, but the data still repeatable..
    I tried to use shuffle(), rand(), array_rand() in PHP file,
    and also using order by rand() in sql query..

    But they are still repeatable..

    For information, the data will be display through flash file..

    Can somebody help me with this?

    Thanks before..
    Appreciate any help..

    Here's the code of mine

    PHP Code:
    $sqlText="SELECT * FROM soal WHERE Difficulty='"$levelStr ."' AND Topic='"$topic ."'";
        
    $result=mysql_query($sqlText);
        
        
    $count=0;
        
    $arr=null;
        while(
    $row mysql_fetch_array($result)) {
           
    $arr[$count]=$row["SoalID"]."#".$row["Question"]."#".$row["Answer1"]."#".$row["Answer2"]."#".
                      
    $row["Answer3"]."#".$row["Answer4"]."#".$row["CorrectAnswer"]."#".$row["Picture"];
           
    $count++;
        }
        if (
    $count>0) {
          
    $idx=shuffle($arr);
          echo 
    $arr[$idx];
        } 

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    The questions repeat because each page request is completely separate from all other page requests and php has no idea that any question has already been used.

    To prevent repeats, you need to remember which of the questions have already been used. You can either use a $_SESSION variable or a database table to remember which questions have been output for any visitor.

    Someone posted a solution using $_SESSION variables in one of the other php help forums you posted on.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •