I'm working on a quiz and I'm stuck at coloring the right answer which is entering from the database. I want to make the entire row with green color, after the user choose an answer and click it. Also the wrong one with red. The second problem is that I'm struggling to randomize it with no-repeat question, because now it does repeat. Working for days to find an answer...
I'm going to post the hole code, for better understanding.
Process.php
<?php include 'database.php'; ?>
<?php session_start(); ?>
<?php
//Check to see if the score is set
if(!isset($_SESSION['score'])){
$_SESSION['score'] = 0;
}
if($_POST){
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next = $number+1;
/*
* Get total questions
*/
$query = "SELECT * FROM questions";
//Get results
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;
/*
* Get correct choice
*/
$query = "SELECT * FROM choices
WHERE question_number = $number AND is_correct = 1";
//Get results
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
//Get row
$row = $result->fetch_assoc();
//Set correct choice
$correct_choice = $row['id'];
/***
* TRIED TO DO SOME CODE FOR COLORING THE RIGHT ANSWER *
$query = "SELECT is_correct FROM choices";
$result_color = $mysqli->query($query) or die($mysqli->error.__LINE__);
$row_color = $result_color->fetch_assoc();
function text_color($row_color) {
if ($row_color == 0 ) return 'background-color:#0f0';
if ($row_color == 1 ) return 'background-color:#ffab0a';
return '';
}
$text_color = $row_color['text'];
***/
$query = "SELECT DISTINCT * FROM questions ORDER BY rand() LIMIT 1";
//Get results
$result = $mysqli->query($query) or die($msqli->error.__LINE__);
$question = $result->fetch_assoc();
/***
* TRIED TO RANDOMIZE IT WITHOUT REPEATING *
if(!isset($_SESSION['questions'])){
$_SESSION['questions'] = array();
}
$_SESSION['questions'][] = $row_question['question_number'];
$session = implode(", ", $_SESSION['questions']);
$query = "select * from questions WHRE `question_number` NOT IN $session order by rand() LIMIT 1";
***/
//Compare
if($correct_choice == $selected_choice){
//Answer is correct
$_SESSION['score']++;
}
//Check if last question (SINCE IT WASN'T RANDOM)
if($number == $total){
header('Location:final.php');
exit();
}
else {
header("Location:question.php?n=$question[question_number]");
}
}
Question.php
<?php include 'database.php'; ?>
<?php session_start(); ?>
<?php
//Set Question number
$number=(int)$_GET['n'];
/*
* Get total questions
*/
$query = "SELECT * FROM `questions`";
//Get results
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;
/*
* Get Question
*/
$query = "SELECT * FROM `questions` WHERE question_number = $number ORDER BY rand() LIMIT 1";
//Get results
$result = $mysqli->query($query) or die($msqli->error.__LINE__);
$question = $result->fetch_assoc();
/*
* Get Choices
*/
$query = "SELECT * FROM `choices` WHERE question_number = $number ORDER BY rand() LIMIT 4";
//Get results
$choices = $mysqli->query($query) or die($msqli->error.__LINE__);
Aucun commentaire:
Enregistrer un commentaire