0

Hi, i am having trouble in understanding how session works. I am trying to write a program that will repeatedly request a number to be entered and each time the number is entered the program is to print out:

  1. that number
  2. the sum of all the numbers entered
  3. the count in how many times I submitted

This is my code so far:

<?php
session_start();
if(isset($_session['count']))
{
 $_session['count'] = $count;
}else{
 $_session['count'] = 0; 
}
?>
<html>
 <head>
 <title>number</Title>
 <style>
 </style>
 <body>
 <form action = "numbers.php" method = "post">
 Numbers: <input type "text" name = "number" size = "6"/>
 <input type = "submit" value = "submit" name = "submit"/>
 <p>
 </form>
 </body> 
 </head>
</Html>
<?php 
 if(isset($_POST["submit"]))
 {
 $number = $_POST['number'];
 If (is_numeric($number))
 { 
 $count = $_session['count'] + $number;
 print "Last number entered: ".$number;
 print "<br>Total internal numbers: ".$count;
 }
 } 
?>

Im trying to store my $count variable so each time i submit it outputs the total sum of the numbers entered

asked Aug 30, 2013 at 12:17
1
  • FYI: $cnt =0; then isset($cnt) would be false! Commented Aug 30, 2013 at 12:21

4 Answers 4

2

You set $_session['count'] = $count; before you have a variable called $count. You have to update the session value after you've set $count like:

$count = $_session['count'] + $number;
$_session['count'] = $count;

You can then replace the top of your script with:

if(!isset($_session['count']))
{
 $_session['count'] = 0; 
}
answered Aug 30, 2013 at 12:20
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the explanation of WHY the code doesn't work as the question asker expects.
0

On the start you are using undefined variable $count. Edit it to:

if(!isset($_session['count']))
 $_session['count'] = 0; 

Then add a new line:

If (is_numeric($number))
{ 
 $count = $_SESSION['count'] + $number;
 $_SESSION['count'] = $count;
 print "Last number entered: ".$number;
 print "<br>Total internal numbers: ".$count;
}

OR second part edit to:

If (is_numeric($number))
{ 
 $_SESSION['count'] += $number;
 print "Last number entered: ".$number;
 print "<br>Total internal numbers: ".$_SESSION['count'];
}
answered Aug 30, 2013 at 12:22

3 Comments

the first if can be safely submitted to thedailywtf.com ;) seriously, can't you think of a better implementation? HINT: reverse the isset check, from true to false
what if i wanted to have my have my output to get the sum of all numbers inputted for example: i inputted 1 the outcome is 1 then 1 is inputted again the outcome is 2
nvm it turns out it was a syntax problem $_session should have been $_SESSION
0
<?php 
 if(isset($_POST["submit"]))
 {
 $number = $_POST['number'];
 If (is_numeric($number))
 { 
 $count = $_session['count'] + $number;
 $_session['count'] = $count;
 print "Last number entered: ".$number;
 print "<br>Total internal numbers: ".$count;
 }
 } 
?>
answered Aug 30, 2013 at 12:19

Comments

0

Here's the working example. [TESTED]

You have to assign $_SESSION['count']=$count;

<?php
session_start();
//Code commented as not required.
/*if(isset($_session['count']))
{
 $_session['count'] = $count;
}else{
 $_session['count'] = 0;
}
*/?>
<html>
<head>
 <title>number</Title>
 <style>
 </style>
 <body>
 <form action = "" method = "post">
 Numbers: <input type "text" name = "number" size = "6"/>
 <input type = "submit" value = "submit" name = "submit"/>
 <p>
 </form>
 </body>
</head>
</Html>
<?php
if(isset($_POST["submit"]))
{
 $number = $_POST['number'];
 if (is_numeric($number))
 {
 $count = $_SESSION['count'] + $number;
 $_SESSION['count']=$count;
 print "Last number entered: ".$number;
 print "<br>Total internal numbers: ".$count;
 }
}
?>
answered Aug 30, 2013 at 12:25

1 Comment

Don't say that the code in the beginning is not required. $count = $_SESSION['count'] + $number; will through an undefined index notice the first time the script is called.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.