Home > front end >  how to increment session variable on every page? php/mysql
how to increment session variable on every page? php/mysql

Time:08-10

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?

CodePudding user response:

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
  • Related