Hello,

I have a page containing 5 links, each link has a different ID number at the end. (Shown in Bold)

http://localhost/Memory.php?action=add&id=1
http://localhost/Memory.php?action=add&id=2
http://localhost/Memory.php?action=add&id=3
http://localhost/Memory.php?action=add&id=4
http://localhost/Memory.php?action=add&id=5

Each time a link is clicked, how do I add the id at the end of each link, to an array? But, if array already contains that particular ID, how do I display a message notifying the user e.g. "Already Added!"

Once in the array, how do I output the items in the array to the screen, along with a remove button.

Upon clicking the remove button, how do I delete the appropriate id from the array. The remove link will be in the form of the following:

http://localhost/Memory.php?action=delete&id=1
http://localhost/Memory.php?action=delete&id=2
http://localhost/Memory.php?action=delete&id=3
http://localhost/Memory.php?action=delete&id=4
http://localhost/Memory.php?action=delete&id=5

    When you're clicking the link you are posting back to the server with the GET data "action" and "id". On the Memory.php page you load up the array for the user from stored data. It may be like (0 => 1, 1 => 3, 2 => 5), so don't mix up your key-value pairs.

    To add to the array just tack on a blank index:
    $arr[] = $_GET['id'];
    sort($arr);

    To delete it, you must determine the numeric index of the id:

    $x = array_search($_GET['id'],$arr);
    if ($x !== false) { unset($arr[$x]); }
    sort($arr);

    use the sort() function to re-index your keys after you make changes to the array.

      And depending upon how you want the user-data to persist, you could use a session or a cookie (or even a database).

        Write a Reply...