duandou2763 2017-03-17 00:23
浏览 37
已采纳

单击链接后如何保持变量值

I searched for a whole day but couldn't find solution to this. I'd like to build very simple 'prev' 'next' pagination, for that I'd like to keep value in a $counter variable so that after clicking a link 'next', system would via GET load next 10 rows from MySQL database and variable would be incremented, something like

$counter += 10;

I first needed to declare $counter variable and set it to 0, but after I click 'next' link, page refreshes and counter variable is reset back to 0, actually not moving with database listing. But I cannot go on without assigning initial value to this variable. It looks like a catch 22 to me.

I tried to solve this with SESSION but had same problem. I have spent 12 hrs trying to decypher this, analysed many pagination samples on net so pls help me if you can. Thanks. If that is silly q, sorry.

  • 写回答

1条回答 默认 最新

  • drwu24647 2017-03-17 01:22
    关注

    The basic technique that I have used to preserve the value of a variable such as $counter is to pass it as variable and reload the form.

    When the form is initially opened, $counter would have no value.

    The initial code when the form is first opened is:

        if (!isset($_REQUEST['page']))
            {$counter = 1;
                    } else {
            $counter = $_REQUEST['page'];}
    

    You can then set $counter to whatever value you like. You then pass the value that you set for $counter by reloading your form. The example below, to reload your form, would have to be modified for your situation.

        echo "<tr><td> <a href='index.php?content=edit_company_form&page=$counter'>$company</td></tr>"
    

    Moving to the next and previous pages is handled in the same manner, add another variable to be passed to the reloaded form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元