/    Sign up×
Community /Pin to ProfileBookmark

PHP Quizz Total Session Score

Hi

When user takes multiple tests in single session, iwant to display total session score for thar user in the labe as Your Total Score is : —-

Code 😀

<?php
session_start();
?>

<?php
require ‘config.php’;
if(!empty($_SESSION[‘name’])){

$right_answer=0;
$wrong_answer=0;
$unanswered=0;

$keys=array_keys($_POST);
$order=join(“,”,$keys);

//$query=”select * from questions id IN($order) ORDER BY FIELD(id,$order)”;
// echo $query;exit;

$response=mysql_query(“select id,answer from questions where id IN($order) ORDER BY FIELD(id,$order)”) or die(mysql_error());

while($result=mysql_fetch_array($response)){
if($result[‘answer’]==$_POST[$result[‘id’]]){
$right_answer++;
}else if($_
POST[$result[‘id’]]==10){
$unanswered++;
}
else{
$wrong_answer++;
}
}
$name=$_SESSION[‘name’];

mysql_query(“update users set score=’$right_answer’ where user_name=’$name'”);

}
?>
<!DOCTYPE html>
<html>
<head>
<title>ABCD</title>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<!– Bootstrap –>
<link href=”css/bootstrap.min.css” rel=”stylesheet” media=”screen”>
<link href=”css/style.css” rel=”stylesheet” media=”screen”>
<!– HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries –>
<!–[if lt IE 9]>
<script src=”../../assets/js/html5shiv.js”></script>
<script src=”../../assets/js/respond.min.js”></script>
<![endif]–>

</head>
<body>
<header>
<p class=”text-center”>
Welcome <?php
if(!empty($_SESSION[‘name’])){
echo $_SESSION[‘name’];
}?>

</p>
</header>
<div class=”container result”>
<div class=”row”>
<div class=’result-logo’>
<img src=”image/Quiz_result.png” class=”img-responsive”/>
</div>
</div>
<hr>
<div class=”row”>
<div class=”col-xs-18 col-sm-9 col-lg-9″>
<div class=’result-logo1′>
<img src=”image/cat.GIF” class=”img-responsive”/>
</div>
</div>

<div class=”col-xs-6 col-sm-3 col-lg-3″>
<a href=”<?php echo BASE_PATH.’questions.php’;?>” class=’btn btn-success’>Start new Quiz!!!</a>

<a href=”<?php echo BASE_PATH.’logout.php’;?>” class=’btn btn-success’>Logout</a>

<div style=”margin-top: 30%”>
<p>Total no. of right answers : <span class=”answer”><?php echo $right_answer;?></span></p>
<p>Total no. of wrong answers : <span class=”answer”><?php echo $wrong_answer;?></span></p>
<p>Total no. of Unanswered Questions : <span class=”answer”><?php echo $unanswered;?></span></p>
</div>

</div>

</div>
<div class=”row”>

</div>
</div>
<footer>
<p class=”text-center” id=”foot”>
Email : <a href=”mailto:[email protected]” target=”_blank”>[email protected]</a>
</p>
</footer>
<!– jQuery (necessary for Bootstrap’s JavaScript plugins) –>
<script src=”js/jquery-1.10.2.min.js”></script>
<script src=”js/bootstrap.min.js”></script>

<!– Include all compiled plugins (below), or include individual files as needed –>
<script src=”js/jquery.validate.min.js”></script>

</body>

</html>
<?php }else{

header( ‘Location: /questions.php’ ) ;

}?>

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmApr 26.2019 — I'm going to assume that this code is where you are outputting the totals:
<i>
</i> &lt;div style="margin-top: 30%"&gt;
&lt;p&gt;Total no. of right answers : &lt;span class="answer"&gt;&lt;?php echo $right_answer;?&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Total no. of wrong answers : &lt;span class="answer"&gt;&lt;?php echo $wrong_answer;?&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Total no. of Unanswered Questions : &lt;span class="answer"&gt;&lt;?php echo $unanswered;?&gt;&lt;/span&gt;&lt;/p&gt; <br/>
&lt;/div&gt;

So - What is the problem? You didn't tell us that!

Note: you don't need to keep switching in and out of php mode.

And - please read the manual about the mysql functions. You will learn that they are long-deprecated and that you should be learning to use PDO or, if necessary, the MySqlI functions in place of MySql.
Copy linkTweet thisAlerts:
@NogDogApr 26.2019 — If you want to track totals over a session, then you need to add them to the $_SESSION array, maybe...
<i>
</i>function addTotal($key, $value)
{
if(empty($_SESSION[$key]) {
$_SESSION[$key] = $value;
} else {
$_SESSION[$key] += $value;
}
}
// then you can use it as:
addTotal('right_answer', $right_answer);
addTotal('wrong_answer', $wrong_answer);

echo "Total right: {$_SESSION['right_answer']}, total wrong: {$_SESSION['wrong_answer']}";

Note, this works since $_SESSION is a "super-global" and available in any context, including within a function definition.
Copy linkTweet thisAlerts:
@sreekanth236authorApr 27.2019 — @ginerjm#1603107

Hi

Thanks for the reply

I want to display like this

<div style="margin-top: 30%">

<p>Total no. of right answers : <span class="answer"><?php echo $right_answer;?></span></p>

<p>Total no. of wrong answers : <span class="answer"><?php echo $wrong_answer;?></span></p>

<p>Total no. of Unanswered Questions : <span class="answer"><?php echo $unanswered;?></span></p>

<p>Your total score is : ------ </p>

</div>

If user takes multiple tests in single session, i want to display full score of the user session
Copy linkTweet thisAlerts:
@ginerjmApr 27.2019 — While NogDog attempted to provide some help to you that made perfect sense, you have not yet replied in kind to his effort nor mine. This is a two-way street - we give you something and you give us something in reply directly to that.

How about it?
×

Success!

Help @sreekanth236 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...