2

Possible Duplicate:
PHP: How to remove specific element from an array?

I've got an array for holding shopping basket information:

session_start();

$items = $_SESSION['items'];
$values = $_SESSION['values'];


if (isset($_POST['addtobasket']))
{


       $items[] = $_POST['item'];
       $values[] = $_POST['value'];
       $_SESSION['items'] = $items;
       $_SESSION['values'] = $values;
}

        print("Added " . $_POST['item'] . " with value of " . $_POST['value'] . "to basket");

?>

At the checkout screen I'd like to be able the user to be able to remove items:

<?                                  
                            echo "<table class='basketdisplay'>";
                            echo "<tr><td>Item</td><td>Price</td></tr>";
                            foreach($items as $key => $item)
                            {                                  
                                   echo "<tr>"; 
                                   echo "<td>" . $item . "</td>";
                                   echo "<td>£" . $values[$key] . "</td>"; 
                                   echo "<td>" . $key . "</td>";
                                   echo "</tr>";      
                            }



                            echo "</table>";
?>

So instead of the last column showing the key, some form of button to remove the item at that key?

Does anyone know how I'd go about this, my php is not strong. TIA

Community
  • 1
  • 1
James MV
  • 8,569
  • 17
  • 65
  • 96
  • Answer is also on SO [here](http://stackoverflow.com/questions/2448964/php-how-to-remove-specific-element-from-an-array) – afuzzyllama Aug 19 '11 at 14:48

7 Answers7

2

You could use http://php.net/manual/en/function.unset.php to remove items from the array.

You'd have to figure the code though:)

Quamis
  • 10,924
  • 12
  • 50
  • 66
2
unset($array[$key]);

manual

RiaD
  • 46,822
  • 11
  • 79
  • 123
0
if(isset($_REQUEST['removeitem']))
{
    unset($items[$_REQUEST['removeitem']]);
}

Where $_REQUEST['removeitem'] is the key of the item you want to remove. Then have your href do something like <a href="?removeitem=itemkey">remove item</a>.

Ryre
  • 6,135
  • 5
  • 30
  • 47
0

Regarding your removal feature, you could just use a checkbox (or even a submit button) like:

echo "<td><input type=checkbox name='remove[$key]' value=1>remove</td>";

(Take care that all variables ($key) in your output also need htmlspecialchars() handling.)

Then if the basket form is submitted again, you can simply probe for elements to be removed with:

foreach ($_REQUEST["remove"] as $key) {
    unset($_SESSION["items"][$key]);
}
mario
  • 144,265
  • 20
  • 237
  • 291
0

Firstly, change this line:

echo "<td>" . $key . "</td>";

to this:

echo "<td><a href='new_php_page.php?key=$key'>Remove Item</td>";

new_php_page.php:

<?php

session_start();

$items = $_SESSION['items'];

// validate session key here - that it is an integer etc.

if (array_key_exists($_POST['key'], $_SESSION['items'])) {
  unset($_SESSION['items']);
}

// redirect to cart page again

?>

An improvement would be to replace the link to a button. You could then consider using AJAX to have the item removed 'behind-the-scenes'.

pb149
  • 2,298
  • 1
  • 22
  • 30
0

First, you must use prepared statement to prevent SQL Injection and for filtering purpose. Use PDO for this.

but i don't sure, because you don't make any link for user to delete their item.

first you must make an unique id for all item, so easy for you to remove, example :

echo '<a href="delate.php?$item_id">Remove</a>'
Ahmad
  • 4,224
  • 8
  • 29
  • 40
0

Make the table a form and the final element of the row an HTML input field:

<FORM target="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<?php
echo "<table class='basketdisplay'>";
echo "<tr><td>Item</td><td>Price</td></tr>";
foreach($items as $key => $item)
{                                  
    echo "<tr>"; 
    echo "<td>" . $item . "</td>";
    echo "<td>£" . $values[$key] . "</td>"; 
?>

<TD>
    <INPUT type="submit" name="DELETE_<?php echo $key; ?>" value="Delete"/>
</TD>
<?php
    echo "</tr>";      
}
echo "</table>";
?>
</FORM>

And then at the start of the script, use something like:

<?php
foreach ($_POST as $name=>$value)
{
    if (preg_match('/^DELETE_(\d+)$/',$name,$matches)) //Assumes that $key is numeric
    {
        $keyToDelete=$matches[1];
        // Put your delete code in here
    }
}
?>

or something like that.

DaveyBoy
  • 2,928
  • 2
  • 17
  • 27