samedi 16 février 2019

PHP random number no repeat

Seems to be duplicate title, but i accesed all links and none of the pages helped me.

I have the following code:

//Check if form was submitted
if($_POST){
        $number = $_POST['number'];
        $selected_choice = $_POST['choice'];
        $next=$number+1;
        $total=4;

        //Get total number of questions
        $query="SELECT * FROM `questions` LIMIT 4";
        $results = $mysqli->query($query) or die($mysqli->error.__LINE__);
        $total=$results->num_rows;

        //Get correct choice
        $q = "select * from `choices` where question_number = $number and is_correct=1";
        $result = $mysqli->query($q) or die($mysqli->error.__LINE__);
        $row = $result->fetch_assoc();
        $correct_choice=$row['id'];



        //compare answer with result
        if($correct_choice == $selected_choice){
                $_SESSION['score']++;
        }

        if($number == $total){
                header("Location: final.php");
                exit();
        } else {
                header("Location: formular1.php?n=".$next."&score=".$_SESSION['score']);
        }
}

What i need to do, is that to get random number when form is submited, but that number must be unique and dont repeat until session ends.

I have tried:

$number = range(1, 99);
shuffle($number);

..but i dont know how to integrate that :( Thank you!

Aucun commentaire:

Enregistrer un commentaire