• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

PHP: Array (Delete Row)?

Question

DjmUK    0

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.

Share this post


Link to post
Share on other sites

8 answers to this question

Recommended Posts

  • 0
linked    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.

Share this post


Link to post
Share on other sites
  • 0
jimbo11883    1

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...

Share this post


Link to post
Share on other sites
  • 0
DjmUK    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?

Share this post


Link to post
Share on other sites
  • 0
Kudos    51

Rebuild the array by using a loop to reassign the array variable to a new array while skipping the blank index.

/k

Share this post


Link to post
Share on other sites
  • 0
Hentiki    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

Share this post


Link to post
Share on other sites
  • 0
DjmUK    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.

Share this post


Link to post
Share on other sites
  • 0
Kudos    51

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

Share this post


Link to post
Share on other sites
  • 0
DjmUK    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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.