简体   繁体   English

如何在每一页上增加 session 变量? php/mysql

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

i have 2 session variable.我有 2 个 session 变量。 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.我有 13 页属于 $checked 数组中的项目。 i call the session variables on other pages:我在其他页面上调用 session 变量:

$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.现在我们的索引值为 1。但是在我在另一个页面上调用它之后,我的 session 变量再次从 0 而不是 1 开始。我的意思是我不能动态增加它。如果有不清楚的地方,我可以编辑帖子。 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 中,则必须将其存储在 session 中(就像您在初始页面上所做的那样):

$_SESSION['index']=$index;

Basically, any time you want to update a session value, the steps are:基本上,只要您想更新 session 值,步骤如下:

  1. Read the value from session从 session 读取值
  2. Calculate the new value计算新值
  3. Write the new value to session将新值写入 session

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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