Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to increment session variable on every page? php/mysql

i have 2 session variable. first one is an array to store inputs, second one is for checking index number.

if(isset($_POST['submit'])){

    $selected=$_POST['check_list'];
   foreach ($selected as $c){ 
    $checked[]=$c;
  } 

  $_SESSION['checkedsession']=$checked;
    $_SESSION['index']=$index;
    $index=0;

I have 13 pages belongs to items in $checked array. i call the session variables on other pages:

$index=$_SESSION['index'];
    $index=$index+1;
    $checked=$_SESSION['checkedsession'];

now our index value is 1. but after i call it on another page, my session variable starts from 0 again instead of 1. I mean i cannot increase it dynamically.I can edit post if there is something unclear. Any ideas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re not updating the value on the other pages:

$index=$_SESSION['index'];
$index=$index+1;

If you want the updated value to persist in the session, you have to store it in the session (just like you do on the initial page):

$_SESSION['index']=$index;

Basically, any time you want to update a session value, the steps are:

  1. Read the value from session
  2. Calculate the new value
  3. Write the new value to session
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading