duanluo5096 2013-11-24 16:32
浏览 41

使用href链接对表进行排序,并将值存储在cookie中。 只能使用PHP

I have a table, like this enter image description here

The titles are to create a cookie, and store a value like "description", when someone clicks the description title.

This is what I have so far, but it doesn't work. When I check the cookie, it sets to the last setcookie which is "deleted". Nothing happens when I click on other links (except page refresh).

<table>
       <th><a href=<?php setcookie("orderCookie", "id"); $PHP_SELF?>>ID</th>
       <th><a href=<?php setcookie("orderCookie", "itemName"); $PHP_SELF?>>Item Name</th>
       <th><a href=<?php setcookie("orderCookie", "description"); $PHP_SELF?>>Description</th>
       <th><a href=<?php setcookie("orderCookie", "supplierCode"); $PHP_SELF?>>Supplier Code</th>
       <th><a href=<?php setcookie("orderCookie", "cost"); $PHP_SELF?>>Cost</th>
       <th><a href=<?php setcookie("orderCookie", "price"); $PHP_SELF?>>Selling Price</th>
       <th><a href=<?php setcookie("orderCookie", "onHand"); $PHP_SELF?>>Number On Hand</th>
       <th><a href=<?php setcookie("orderCookie", "reorderPoint"); $PHP_SELF?>>Reorder Point</th>
       <th><a href=<?php setcookie("orderCookie", "backOrder"); $PHP_SELF?>>Back Order</th>
       <th><a href=<?php setcookie("orderCookie", "deleted"); $PHP_SELF?>>Delete/Restore</th>

requirement:

Make each column header a link. When the user clicks on the link, it redisplays the records sorted in ascending order on that column value. Store the name of the column last sorted on in a cookie, and use this as the default sorting the next time the user calls view.php. That is, this cookie should be used to "preserve" the view, even between sessions.

Any suggestions? Thanks!

EDIT:

   <th><a href="?orderBy=id">ID</th>
   <th><a href="?orderBy=itemName">Item Name</th>
   <th><a href="?orderBy=description">Description</th>
   <th><a href="?orderBy=supplierCode">Supplier Code</th>


if(isset($_GET['orderBy'])){
   $order = $_GET['orderBy'];
   setcookie("orderCookie", $order); 
   header("Location:view.php");
}
  • 写回答

1条回答 默认 最新

  • drjtua5953 2013-11-24 16:37
    关注

    Headers must be sent by PHP before any HTML content is sent. You cannot set cookies, once the first character of html is sent.

    You can use javascript/jQuery for this, instead.

    EDIT: Help to use javascript to set cookies: https://stackoverflow.com/a/1460174/1001641

    EDIT 2: In your code, in the href attribute, use this instead:

    <?php echo $_SERVER['PHP_SELF'] . "?r=id"; ?>
    <?php echo $_SERVER['PHP_SELF'] . "?r=itemName"; ?>
    

    and so on.

    Now, at the top of your file, use this code:

    <?php 
    if(isset($_GET['r']))
    {
      $r = $_GET['r'];
    }
    else
    {
      $r = "";
    }
    setcookie("orderCookie", $r);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。