如果用户选择了正确答案,我想打印,如果正确则打印"你选择正确:你选择的答案是:...."如果用户选择不正确答案,则打印他选择的答案和正确的答案。所有问题都在数据库中。我的函数检查答案,但扫描它没有空格,当选项中有空格,用户选择正确答案时,它说错了。我的答案代码:
<?php
require_once 'templates/header.php';
$servername2 = "localhost";
$username2 = "root";
$password2 = "";
$db2 = "user_login";
$conn2 = new mysqli($servername2, $username2, $password2, $db2);
$kat = $_SESSION['kategorija'];
$sql2 = "SELECT * FROM questions where category_id=$kat";
$results = $conn2->query($sql2);
$s=0;
$count = $_SESSION['count'];
$kategorija = $_SESSION['kategorijaName'];
?>
<body>
<div class="container" style="margin-top:100px;">
<div class="row" style=" margin-bottom:30px;">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="" style="text-align: -webkit-center;
font-size: 2em;
letter-spacing: 2px;
text-transform: uppercase;
font-family: Open Sans, sans-serif;
font-weight: 700;
color: #4B4B4C;">
<?php echo $kategorija; ?>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<?php
$teisingi = 0;
foreach($results as $result){
$s++;
$cAnswer = $result['correctAnswer'];
if (isset($_POST['answers'.$s.''])){
$answers2 = $_POST['answers'.$s.''];
}else $answers2 = NULL;
echo '<div class="borderis"> '.$result['question'].'</div>';
if ($answers2 != NULL ){
if($answers2 == $result['answer'.$cAnswer.''] ){
echo '<div style="margin-bottom:20px; margin-top:10px; color: #40a240;
letter-spacing: 2px;"> Jūsų pasirinktas atsakymas yra teisingas: ';
echo ''.$answers2.'</div>';
$teisingi++;
}else{
echo '<div style="margin-top:10px; margin-bottom:10px; color:red; letter-spacing: 2px;"> Jūsų pasirinktas atsakymas yra neteisingas: ';
echo ''.$answers2.'</div>';
echo '<div style="margin-top:10px; margin-bottom:20px; color: #40a240;
letter-spacing: 2px; "> Teisingas atsakymas yra: '.$result['answer'.$cAnswer.''].'</div>';
}
}else {
echo '<div style="margin-top:10px; margin-bottom:20px; letter-spacing: 2px;"> Neatsakytas </div>';
}
}
?>
<div class="row" style=" padding-top: 20px; margin-bottom:40px;">
<div class="col-md-3 col-sm-3 col-xs-12">
<a href="start-quiz.php" class="btn btn-success btn-block" id="start_btn"> Grįžti į testus </a>
</div>
<div class="col-md-3 col-sm-3 col-xs-12">
</div>
<div class="col-md-6 col-sm-6 col-xs-12" style="text-transform: uppercase;
letter-spacing: 3px !important;
font-size:1.3em !important;
font-weight: 600 !important;
text-align: -webkit-center;
border: 3px solid #4B4B4C;
"> Jūs atsakėte teisingai
<?php echo $teisingi.'/'.$count ?> </div>
</div>
</div>
</div>
</div>
</body>
<script>
</script>
<?php
$conn2->close();?>
&#13;
我的问题代码在哪里打印问题:
<?php
require_once 'templates/header.php';
?> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <?php
$servername2 = "localhost";
$username2 = "root";
$password2 = "";
$db2 = "user_login";
$kat = $_POST['category'];
$_SESSION['kategorija'] = $kat;
$conn2 = new mysqli($servername2, $username2, $password2, $db2);
$query2 = "SET NAMES utf8";
$stmt2 = $conn2->prepare($query2);
$stmt2->execute();
$sql2 = "SELECT * FROM questions where category_id= $kat";
$sql3 = "SELECT category_name FROM categories where id= $kat";
$result2 = $conn2->query($sql2);
$result3 = $conn2->query($sql3);
while($row2 = $result3->fetch_assoc()) {
$kategorija = $row2['category_name'];
}
$i=0;
?>
<div class="container">
<?php
if ($result2->num_rows > 0) {
?>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12" style="margin-top: 100px;">
<div class="" style="text-align: -webkit-center;
font-size: 2em;
letter-spacing: 2px;
text-transform: uppercase;
font-family: Open Sans, sans-serif;
font-weight: 700;
color: #4B4B4C;"> <?php echo $kategorija; ?>
</div></div></div>
<form action="atsakymai.php" method="post" id="quiz" style="margin-top:100px;" >
<?php
while($row = $result2->fetch_assoc()) {
$label1 = $row['answer1'];
$label2 = $row['answer2'];
$label3 = $row['answer3'];
$label4 = $row['answer4'];
$z = 1;
while ( $z<=4 ){
$row['answer'.$z.''] = preg_replace('/\s+/', '', $row['answer'.$z.'']);
$z++;
}
echo '<div class="row">';
echo '<div class="col-md-12 col-sm-12 col-xs-12">';
echo '<div class="borderis">'.$row['question'].'</div><br>';
$i++;
echo '<fieldset id="group" >';
echo '<label for="'.$row['answer1'].'"><input type="radio" id="'.$row['answer1'].'" name="answers'.$i.'" value="'.$row['answer1'].'" class="mygtukai"> <bled></bled>
<span>'.$label1.'</span></label>'.'<br>';
echo '<label for="'.$row['answer2'].'"><input type="radio" id="'.$row['answer2'].'"name="answers'.$i.'" value="'.$row['answer2'].'"> <bled></bled>
<span>'.$label2.'</span></label>'.'<br>';
echo '<label for="'.$row['answer3'].'"><input type="radio" id="'.$row['answer3'].'"name="answers'.$i.'" value="'.$row['answer3'].'"> <bled></bled>
<span>'.$label3.'</span></label>'.'<br>';
echo '<label style="margin-bottom:15px;" for="'.$row['answer4'].'"><input type="radio" id="'.$row['answer4'].'"name="answers'.$i.'" value="'.$row['answer4'].'"> <bled></bled>
<span>'.$label4.'</span></label>'.'<br>';
echo '</fieldset>';
echo '</div></div>';
}
} else {
echo "0 results";
}
?>
<div id="progressbar"></div>
<div id="percentage"></div>
<input type="submit" value="Pateikti atsakymus" name="result" class="qSubmit" id="qSubmit" />
</form>
<script>
$(document).ready(function(){
var names = {};
$(':radio').each(function() {
names[$(this).attr('name')] = true;
});
var count = 0;
$.each(names, function() {
count++;
});
$("#qSubmit").click(function(){
if ($(':radio:checked').length !== count) {
event.preventDefault();
alert("Atsakykite į visus klausimus!");
}
});
});
</script>
<script>
$(document).ready(function() {
$('#progressbar').progressbar({
value: 0
});
$('[name*=answers]').click(function() {
var totalChecked = 0;
var totalRadioGroups = 0;
var previousName = '';
$.each($('[name*=answers]'), function(index, value) {
var radioName = $(this).attr('name');
var radioId = this.id;
if (previousName != radioName) {
totalRadioGroups++;
}
if ($('#' + radioId).is(':checked')) {
totalChecked++;
}
previousName = radioName;
});
var percentage = (totalChecked / totalRadioGroups) * 100;
$('#progressbar').progressbar('value', percentage);
$('#percentage').html(percentage.toFixed(2) + "%");
});
});
</script>
<?php
$_SESSION['count'] = $i;
$_SESSION['kategorijaName'] = $kategorija;
$conn2->close();
$stmt2->close();
?>
&#13;