0

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");
}
hank99
  • 175
  • 1
  • 1
  • 10

1 Answers1

0

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);
?>
Community
  • 1
  • 1
Naveed Hasan
  • 633
  • 7
  • 18