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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分