Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting from an array!

Status
Not open for further replies.

ifx

Technical User
Feb 26, 2002
57
GB
I'm writing a shopping basket which is working fine, except when I want to delete an item from the basket.

It's using an array to store three pieces of information for each position in the array (3 columns and variable rows). This is pulled out to a session then read back to a session when stored. It increases in size length as a product is added to the basket. When the product is added, a remove button is displayed allowing the user to click on it to remove that item (identified by PID). This works OK, but only if the products are deleted from the last item to the first. If I try it in any other order I get odd results
- sometimes it'll delete all the items but leave a remaining blank row, other times it'll delete only the ones with that PID but also in between them in the array.

How can I delete from the array at any position where the PID matches that inputted without affecting the other rows?

Here's my current remove function:

Code:
Function DeleteFromBasket(PID)
	
	basketArray = Session("basketArray")
	basketMaxUsed = Session("basketMaxUsed")
	
	Dim a 'Loop var
		For a = 0 to UBound(basketArray,2)
			If basketArray(0,a) = PID Then
				basketMaxUsed = basketMaxUsed - 1 'Change max size
				basketArray(0,a) = null
				basketArray(1,a) = null
				basketArray(2,a) = null
				End If 
		Next
		
 			Redim Preserve basketArray(BASKET_COLUMNS,basketMaxUsed) 'Reduce array size
			
End Function

Any ideas woudl be great! Thanks!
 
When I had a similar problem, I just set quantity to 0 and left the array alone. Then when diplaying or inserting, I ignored members where quantity was 0.

In your example, you need to shift the array elements down. So if the second item was removed from an array with 5 items, the third becomes the second, the fouth becomes the third, the fifth becomes the fourth and then set the fifth to null...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Hi everybody,

I don't how you script works when you add a product to the basket. But here is something that could be good to think about. I have noticed that almost all the shopping cart just add a new item in the cart when the client choose to do so. Mine work like this : if you add an item that wasn't in the cart, it is added. But if the item was already in the cart, the quantity is updated (increased by the new quantity). This way, we can be pretty sure that the item identifier (PID, etc.) is really unique. So when it is deleted (even with ifx's code) it should work just fine.
I am suspecting that your PID is not just a "unique" primary key.

Another thing you should try is the dictionnary object that does exist in ASP. It is some kind of shopping cart by it self. You just need to store the right information into it. The Dictionary object is used to store information in name/value pairs (referred to as key and item). You can read about it at
Even better, it has built-in methods for finding keys, removing 1 or all keys, adding key, etc...

Keep me posted

Asiby aka WebMaestro
Live Long And Prosper

asiby@hotmail.com
 
Thanks for your replies!

I've worked around the problem slightly using mwolf00's suggestion of just replacing the values with blanks. This seems to work very well so I might stick with that.

I did look at the Scripting.Dictionary Object which I've used before in a shopping system with great success. Unfortunately it only provides an associative array that can hold two values (if manipulated effectively) which in that case were PID and quantity.

I need to hold three values per item, maybe four, so this wasn't an option. The PID alone isn't unique when added to the basket but the other two varibles (phone number and phone make in this case) combine with it to make it unique. No one can add another item with the same PID, phone make and number as is already stored there.

For the moment I've done a work around and given up on the ReDim idea and it seems to be performing OK.

Thank you for your thoughts - most appreciated! :)
 
Glad for you.

Ok about an associative array that can hold two values . But did you try to store an array in the dictionnary object (embedded array) ?

I have to admit that the shopping cart I did back then was pretty simple. Cause the only I had to store in the cart was the ID and the amount. I could pull out the price from a database usin the ID and all the user information was accessible after he or she has logged in (phone, fax, payment options, ...).

Take care.

Asiby aka WebMaestro
Live Long And Prosper

asiby@hotmail.com

 
Yeh I used the scripting.dictionary object to store the data as key/item pairs. The Key was the product ID with the value at each key being the quantity. Worked a treat, but I can't hold more than one value in it (especially as the product ID alone won't necessarily be unique within the shopping basket), which is why I have to use the array (unfortunatly). Ahh well, it seems to work I suppose..!
 
Hello Ifx,

Sorry to bother you again. But I am really curious to know why you have to have duplicate values in the product ID. It's just because I myself have been developping shopping carts for a while and I am starting to think that I am probably doing something wrong.

Thanks


Asiby aka WebMaestro
Live Long And Prosper

asiby@hotmail.com
 
Usually I wouldn't duplicate product ID's in most situations because obviously it causes problem when they are not unique. The reason for having to duplicate them here is simply the nature of the products being sold. The products - though essentially the same - differ slightly depending upon which phone they are for. The phone number is also required to send the product.

An analogy could be that the products are shoes, available in different sizes and with different colour laces. Each shoe 'model' has a unique PID. However there are different sizes available, and these don't have different PIDs. The different colour laces available add to the selection but the shoe model being purchased still has the same PID, with the size and lace colour simply being 'options'.

The combination of the PID, phone model and phone number makes the product unique within the shopping basket. I don't have to worry about quantities becuase none can order the same product for the same phone and number. This shop is also being added to a sort of 'legacy' system which has grown in ways that have thrown up unforseen problems. Some products are available in two formats but have the same PID. This makes another problem for developing this system which I need to work around.

So if you can have a unique PID for EVERY product that's certainly going to make your life easier when manipulating information, and ideally be the way to go as often as possible. In many situations a single PID can be used to identify the product - the difficulty only seems to arise when there are 'options' which are added on, requiring you to handle more variables.
 
I clearely understand Ifx. You couldn't have been more clear.

But, :D cause I still have a "but", what about creating some kind of "fake autonumber field/column" in you array ? It's just an idea. But as you said, it's working for now and that's all that matters for the moment.

Thanks for you time.

Asiby aka WebMaestro
Live Long And Prosper

asiby@hotmail.com
 
You could create a dictionary of dictionaries. The key in the first dictionary would be the main product id and the value would be the dictionary which contains all items with that product id. Then the key to that dictionary would be the secondary product id and the value could be the amount or whatever else you wanted. It could even be another dictionary if you had additional keys.

One unique key would be preferable, but this should do what you want.
 
Yes. And I have been trying to think of an easy way to do it and I have realized that Javascript does offer a lot more possibilities when it comes to array handling. So my idea will be to try too use a Class built with ASP-Javascript while the rest of the pages will be in ASP-VBScript.



Asiby aka WebMaestro
Live Long And Prosper

asiby@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top