Jump to content

How do you unset a value in a POST variable when you don't know the key ?


vincej

Recommended Posts

Hi - I have a POST variable which contains a bunch of products with their codes and prices etc.  However if the customer wants to delete one of those products  it has to be removed from the POST before being processed. I have read the manual on unset  which appears to work well when you know the key relating to the value, but if you take a look at the array ( below ) you can see that it is a multi- dimensional array where the number of potential entries not knowable in advance. My HTML checkbox for delete has a value of prodid.

 

The answer is probably obvious but so far this student of PHP hasn't found it - MANY thanks for your advice !

 


Array
(
    [quantity] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
        )

    [prodid] => Array
        (
            [0] => 17
            [1] => 28
            [2] => 27
            [3] => 25
        )

    [name] => Array
        (
            [0] =>  Ribs Large pack 20 pieces
            [1] => 25 Piece Bag 
            [2] => Sirloin Steak 
            [3] => 50 piece bag of Scalops
        )

    [orderid] => Array
        (
            [0] => JAC0398
            [1] => JAC2920
            [2] => JAC2920
            [3] => JAC2920
        )

    [pricelb] => Array
        (
            [0] => 5.00
            [1] => 
            [2] => 
            [3] => 
        )

    [submit] => Update and Submit to Picking >> 
)



 

 

 

 

 

 

Link to comment
Share on other sites

You should find some way to handle the POST data, create a new array before using that new array instead. Don't pass along stuff that shouldn't be there. Kinda hard to understand what you meant, but at least I tried to help you! :)

Link to comment
Share on other sites

I probably should have been more clear. The values are in a form - these are $_POSTED for processing. However one of the available form values is a check box which will allow the customer to delete a product ( prodid ) this too will form part of the  POST variable.

 

I have been trying so far to do exactly what MMDE has suggested - ie create a new SESSION variable  but  I haven't had alot of success so far.

 

the challenge I have is exactly as requinix states - I have no obvious ( to me ) way of identifying the values which need deleting.

 

 

Link to comment
Share on other sites

Looking at that print_r() output, how do you know which products are supposed to be removed?

 

I agree. What is your exact process for editing the cart. Typically the cart will be stored in a cookie or in the session. So, if the user is editing the cart you only need to pass the changes. It looks like you are passing the entire cart when the user wants to delete an item AND you are not passing any identifying information for the item being changed.

 

But, there is definitely one thing you should consider changing - the format of your array. You have sub-arrays which contain different information about the products. A better format, IMHO, is to have one sub-array for each product where the ID of the product is the key for the sub-array. This is a much more logical approach. The problem with the current approach is it assumes that the first element in the 'name' subarray necessarily matches with the first element in every other array. This would likely be true, but that kind of loose logic can cause problems.

Link to comment
Share on other sites

I probably should have been more clear. The values are in a form - these are $_POSTED for processing. However one of the available form values is a check box which will allow the customer to delete a product ( prodid ) this too will form part of the  POST variable.

 

I have been trying so far to do exactly what MMDE has suggested - ie create a new SESSION variable  but  I haven't had alot of success so far.

 

the challenge I have is exactly as requinix states - I have no obvious ( to me ) way of identifying the values which need deleting.

 

Well, with your current format, which I don't link, all you need to do is identify the key of the selected ID for deletion then delete each record with that key in the subarrays. Note that this will not re-index the keys. So, if you have keys: 0, 1, 2, 3, 4 and then delete key #3 you will be left with 0, 1, 2, 4. However, you can reindex the keys if needed.

 

I will post some sample code shortly.

 

 

 

Link to comment
Share on other sites

$deleteID; //The id to be deleted passed from POST
$cartArray; //The array of products passed from POST

//Get the key by searching for the ID in the array
$deleteKey = array_search($deleteID, $cartArray['prodid']);

if($deleteKey !== false)
{
    //Remove the element from each subarray
    foreach($cartArray as $index => &$values)
    {
        unset($values[$deleteKey]);
        //Uncomment this line if you need to reindex the values
        //$cartArray[$index] = array_values($values);
    }
}

 

Note, if the array was more logically structured you could delete an item with a single line of code instead of all that

Link to comment
Share on other sites

Oh, so it's not POST data? It's session data? That changes everything, and as everyone have said, use another hierarchy.

Do you need to remember anything but the order id, product id and quantity? I guess it's nice not being so terrible against the database.

 

At least keep the array to be something like this:

$array['orderid']['prodid']['quantity']
$array['orderid']['prodid']['name']
$array['orderid']['prodid']['pricelb']

 

I would still recommend:

$array['orderid']['prodid'] = $quantity

Link to comment
Share on other sites

to clarify - it is POST data. However, my strategy up till now has been to take the POST  data and create a new SESSION variable with a new name for use in the next part of the app.

 

Essentially, my challenge is , if I have a html checkbox which is intended to delete a product ID then, I need to remove that key => value from the POST array before processing.  I think Psycho has got something very interesting - I'll check it out.

 

Many Many Thanks for all your advice !

Link to comment
Share on other sites

Psycho's right--If I understand what you're trying to do with this array you're tackling it from the completely wrong angle.

 

You're trying to do too much here with one array--it product desc, etc should be in another array, but if you want to pass it along in this array for easier parsing later on, that's fine.

 

Think in terms of each item as the head element, and what you have currently as top-level elements should be sub elements of the rest.

[0]->array
     [itemID]->ID of the item being ordered
     [desc]->Description of the item
     [price]->Price of this item
      ...etc for each item
[1]->array
     [itemID]->ID of the item being ordered
     [desc]->Description of the item
     [price]->Price of this item
      ...etc for each item
[2]->array {...}

and so on.  A user wants the 2nd item deleted?  No problem 

unset($Myitems[1])     


 

Link to comment
Share on other sites

Jcanker - thanks for your help and ideas. Clearly I am still a student of PHP.  I'm not sure how I would use different arrays. The issue is that I capture my web form data using submit and POST. It grabs all the data in the form.  The checkbox which indicates that an item is to be deleted is part of the Submit / POST.

 

If I need to approach this from a wholly different angle I am an eager student wanting to learn how that could be done.

 

Many Thanks ! 

Link to comment
Share on other sites

It's been a while since I've actually done this in PHP.  (It's actually easier to do with AJAX, but if you're just learning PHP then AJAX will make your head spin :o)

 

I'm assuing you're having php build the web form dynamically based on what is in the shopping cart and that the form elements aren't hard coded?  You can always pass the Itemnum as the VALUE of the checkbox.  If the box is checked, remove that item from their list.

 

It still goes back to rethinking how your arrays work.  Don't forget that $_GET and $_POST are arrays themselves, so assuming you've taken the proper precautions such as sanitizing your data (or even better, used prepared statements!), you can work directly with POST or GET data without recasting into your own new array. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.