Actually , I want to display some random questions from database (mysql) in web page where user can answer that question by using radio buttons
here is my code to display data in web page using php
public function loadQuestions() {
try {
$stmt = $this->conn->prepare('SELECT * FROM questions');
$stmt->execute();
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
And i called that function from my displaying page (quiz.php)
$stmt = $auth_user->loadQuestions();
<form action="#">
<!-- this backend logic -->
<?php
foreach($stmt as $row) {
?>
<!-- backeng logic close -->
<div class="card-panel light-blue darken-1"><?php echo $row['Question_id'];
echo ' ';
echo $row['question'];
echo '?'
?></div>
<p>
<label>
<input name="group3" type="radio" value="<?php $row['option1'] ?>" />
<span><?php echo $row['option1'] ?></span>
</label>
</p>
<p>
<label>
<input name="group3" type="radio" value="<?php $row['option2'] ?>" />
<span><?php echo $row['option2'] ?></span>
</label>
</p>
<p>
<label>
<input name="group3" type="radio" value="<?php $row['option3'] ?>" />
<span><?php echo $row['option3'] ?></span>
</label>
</p>
<p>
<label>
<input name="group3" type="radio" value="<?php $row['option4'] ?>" />
<span><?php echo $row['option4'] ?></span>
</label>
</p>
<?php
}
?>
<!-- submit button -->
<div class="card">
<div class="card-content">
<p>
<label>
<input type="checkbox" onchange="document.getElementById('submitValue').disabled = !this.checked;"/>
<span>I agree that, And I'm sure to submit my answers.</span>
</label>
</p>
<div class="col s6 offset-s10">
<button class="btn waves-effect waves-light" id="submitValue" disabled type="submit">Submit
<i class="material-icons right">send</i>
</button>
</div>
</div>
</div>
<!-- submit button end -->
</form>
But, the problem is, when i'm selecting the radio button, it will not consider as separate question for every row that returns by foreach() loop..
what is the best way to do that?
thanks in advance...
Change your query which fetches questions from the database to:
SELECT * FROM questions ORDER BY RAND();
EDIT: Based on OP's comments, to limit the query results to just 15:
SELECT * FROM questions ORDER BY RAND() LIMIT 15;
You can use Rand() for random and limt for limit the rows result
SELECT *
FROM questions
ORDER BY RAND()
LIMIT 15
The RAND() function returns a random number or a random number within a range.
The RAND() function will return a value between 0 (inclusive) and 1 (exclusive).
The RAND() function will return a completely random number if no seed is provided, and a repeatable sequence of random numbers if a seed value is used.
public function loadQuestions() {
try {
$stmt = $this->conn->prepare('SELECT * FROM questions ORDER BY RAND() LIMIT 15');
$stmt->execute();
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
Return a random decimal number (no seed value - so it returns a completely random number >= 0 and <1):
SELECT RAND();
Return a random decimal number >= 5 and <10:
SELECT RAND()*(10-5)+5;
You may also uses the php mt_rand() function with query:-
The mt_rand() function generates a random integer using the Mersenne Twister algorithm.
Tip: This function produces a better random value, and is 4 times faster than rand().
Tip: If you want a random integer between 10 and 100 (inclusive), use mt_rand (10,100).
<form action="#">
<!-- this backend logic -->
<?php
foreach($stmt as $row) {
echo '
<!-- backeng logic close -->
<div class="card-panel light-blue darken-1"> '.$row['Question_id'].'
'.$row['question'].'
?
</div>
<p>
<label>
<input name="group3" type="radio" value=" '.$row['option1'].' " />
<span> '.$row['option1'].' </span>
</label>
</p>
<p>
<label>
<input name="group3" type="radio" value=" '.$row['option2'].'" />
<span>'.$row['option2'].' </span>
</label>
</p>
<p>
<label>
<input name="group3" type="radio" value="'.$row['option3'].'" />
<span>'.$row['option3'].' </span>
</label>
</p>
<p>
<label>
<input name="group3" type="radio" value=" '.$row['option4'].' " />
<span>'.$row['option4'].' </span>
</label>
</p>';
}
?>
<!-- submit button -->
<div class="card">
<div class="card-content">
<p>
<label>
<input type="checkbox" onchange="document.getElementById('submitValue').disabled = !this.checked;"/>
<span>I agree that, And I'm sure to submit my answers.</span>
</label>
</p>
<div class="col s6 offset-s10">
<button class="btn waves-effect waves-light" id="submitValue" disabled type="submit">Submit
<i class="material-icons right">send</i>
</button>
</div>
</div>
</div>
<!-- submit button end -->
</form>