简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM