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.

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

报告相同问题?

悬赏问题

  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库