douquanqiao6788 2013-09-04 20:18 采纳率: 100%
浏览 41
已采纳

跨多个表单文件的php计数器变量

I have multiple pages. On each page I have a list of items. So on Page 1, I have Items 1-10. On page 2, I have items 11-20. It is NOT ten items per page but the point is that page 3 should start off with the number immediately following the last number on page 2.

The pages have a simple numeric variable so the counting of items works fine per page but I want it to be set in one place.

I am brand new to php so my question is mostly about how and where to store a variable that would keep track of this item count over multiple files?

Desired behavior:

Let's say Page 1 has 5 items and my counter variable on page 1 sets them to be correctly numbered to:

1.apple
2.grape
3.pear
4.banana
5.orange

But let's say I need to remove banana. When I do so, the current counter on page 1 works fine and sets to:

1.apple
2.grape
3.pear
4.orange

But on page 2 the counter variable is set to 6 and has:

6.mango
7.peach
8.watermelon

I want to set the persistent variable so that when I remove a fruit, it will update not only the current page the removed fruit is on but every page after that. So it would adjust page 2 to:

5.mango
6.peach
7.watermelon

Would sessions make that change permanent or would it disappear with the user's cookie?

  • 写回答

3条回答 默认 最新

  • dqfkd82886 2013-09-04 20:24
    关注
    <?php
    //On the first line of your PHP code
    session_start();
    
    //then store your variable in the SESSION array.
    $_SESSION['yourCountersNameHere'] = 56;
    
    //You should now have acces to its value from page to page like this
    
    echo $_SESSION['yourCountersNameHere'];
    //Should display 56.
    ?>
    

    you could also store it in a DB. and then check in the DB on every page.

    //to connecte
    $m_DB = new PDO("mysql:host=".$Host.";dbname=".$DBName."; charset=utf8", $Login, $Password);
    
    //check value in DB
    $query = "SELECT * FROM table";
    $stmt = $m_DB->prepare($query);
    //$param here is empty but normaly it would be a array for all your params like this:
    //$param = array('rowname'=>$value, 'rowName2'=>$value2, .........and so on for all the $params you use.);
    
    $getCounter = $stmt->execute($param);
    while($Counter = $getCounter->fetch())
    {
        //Display the value in your MySQLDB.
        echo $Counter['table.rowName'];
    }
    

    If you use another DB type than MYSQL the code should be fairly the same exepte for a couple of changes you'll have to make.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式