I have a program that is to count the number of times a button is pressesd and then do differnet things eg $k1 = 0; $k2 = 0; if ($_SESSION['init'] == 1) { $k1 ++; echo $k1; } elseif ($_SESSION['init'] == 2) { $k2 ++; echo $k2; } This updates the values but what I need to do is for the increment to update the value of k1 and k2 so that they can be summed when ($_SESSION['init'] == 10). Any ideas?
uhh, from what you are asking it looks like all you want is something like this? elseif($_SESSION['init'] == 10) { $k3 = $k2+$k1; echo $k3; } PHP:
That is what I am trying to do but the problem is that the values of k1 and k2 do not hold when the session changes so all that is output is 0 (there initial values)
yeah I have a session start but cant get the new variables to remain permanently whatever the session. Any ideas?? <?php session_start(); $k1 = 0; $k2 = 0; if(isset($_GET['rst'])) { session_destroy(); header("Location: $_SERVER[PHP_SELF]"); } function add() { if (!isset($_SESSION['init'])) { $_SESSION['init'] = 1; } else { $_SESSION['init']++; } return $_SESSION['init']; } if ($_SESSION['init'] == 1) { $k1 ++; echo $k1; } elseif ($_SESSION['init'] == 2) { $k2 ++; echo $k2; } elseif ($_SESSION['init'] == 3) { $k3 = $k1+$k2; echo $k3; } ?>
The functions are writtten in javascript <html> <head> <title>Hello!</title> <script language="javascript"> <!-- function refresh() { window.location.reload( false ); } //--> </script> </head> <body> <div align="center"> <?=add()?> <p> <input type="button" onclick="refresh()" value="Refresh" name="button1"> <input type="button" value="Reset" onClick="document.location='<?=$_SERVER['PHP_SELF'].'?rst'? >';"> </body> </html>
Just because you're using a session doesn't mean that all variables are registered with that session. Hence the reason you need to use $_SESSION['init'] instead of just $init! If you want to remember the values of k1 and k2 across a session, you have to have them registered with the session. Instead of $k1 you have to use $_SESSION['k1'] (and the same goes for k2) just as you did with the 'init' value.