• 0

PHP: Array (Delete Row)?


Question

Hi there,

I can store data into a session, seperating the data by a comma. So here's an example using 'Pens'.

$_SESSION['Pens'][$Count] = $Pen_ID.",".$Pen_Qty;

I then extract the information and link the $Pen_ID into MySQL (using the explode(); function) and echo'ing into a table.

foreach ($a as $b)
{
	$array = explode(',', $b);
	$ID = $array[0];
	$Qty = $array[1];
}

Deleting A Row

Now, here's my problem - how can I remove a row from the array (let's say it's a shopping basket). I've tried using unset($_SESSION['Pens'][0]) for example - but all it does is clears the entries in that row, and the shopping cart displays a blank line.

Update Quantity

Also, if the user inputs the same entry twice (unique ID), then how does one 'Update' that row with the new quantity ($NewQty = $NewQty + $OldQty). As opposed to two array entries displayed in the shopping cart, when I can just update the quantity in the session.

Thanks in advance.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Ick, lets see how rusty my PHP is after 6 months of Ruby -

Unsetting should work.

If it doesn't, I would probably just add $_SESSION['Pens'][0] = false, and have your inserter check it first... if ( $_SESSION['Pens'][0] { do something }

As for your updating, your best bet would be to retrieve the last stored one (mysql_select), add the value, and update the row.

Link to comment
Share on other sites

  • 0

I tried the 'false' method, but again - it just clears the entries in the row (same as unset), and the view_cart.php is displaying a blank line in place of data. Also, for updating a Qty field - what if it's in the middle of the array? (I mean, the last entry may not necessarily be the one I want to update, and this array is in the session - not the database itself).

:(

I've done something like that, and I just ignored array "rows" that were blank. It is possible to re-build the array if you want to...

Rebuilding the array - how would one do this?

Link to comment
Share on other sites

  • 0

I have an example I use in a webshop (a basket) if you would like to see how I solved it?

As described above, I loop through the array rebulding it leaving out the one I deleted.

Included is allso a function for adding +1 to an entry if it is identical to a allready existing one :)

PM me if you want to have a look

Link to comment
Share on other sites

  • 0

I've tried reading other examples - I have actually done this in the past, but cannot remember how it's done. Rebuilding the array isn't what I did before - when the contents are read from the array, I want an IF statement saying, IF (ID = Blank) then don't show the row.

Or something to that effect.

Link to comment
Share on other sites

  • 0

I see what you are saying. Unset the index you want to remove first, then when you loop through it afterwards using

if(isset($array[$i])) echo $array[$i];

/k

Link to comment
Share on other sites

  • 0

For some reason - the unset command is working now??? that's wierd.

Just gotta' figure out how to update the quantity now ;) (and actually, I think I've found a way)

Thanks guys for the help, I'll have to use some of these ideas later on.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.