Can you remove a variable from an array???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
g7ibby
Forum Newbie
Posts: 2
Joined: Sat Jan 11, 2003 9:23 pm
Location: Tampa

Can you remove a variable from an array???

Post by g7ibby »

Ive been working with php for about 3 weeks now and im loving it. I'm creating a database driven shopping cart on my RAQ4 using mysql and php. I've gotten the cart to do do every function in managing a online store but one thing.

I'm using arrays to store all of the items in their cart. Each variable in the array contains the item number and quantity desired. I need the client to be able to remove a varable or adjust the quantity value of a variable within the array. How can i do this?

Can anybody shed some light on this or am I barking up the wrong tree?

Or even better does anybody know of a better way of storing the cart info?

Thank you
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

i dont understand why you are storing there info as a variable, why dont you just store what they want into a DB table then remove with a SQL query. Then you can amend, delete, insert new, anything you want. fast.
g7ibby
Forum Newbie
Posts: 2
Joined: Sat Jan 11, 2003 9:23 pm
Location: Tampa

Post by g7ibby »

Its my understanding that this could start to strain the system if the site or multiple sites running this were to become very busy making transactions to the database creating new rows as everybody were shopping. If you dont really think this would be a problem and I'm being over cautious let me know and I'll go that route because it was a early consideration.

Also if i did go this way should the persons cart be marked with a sesion ID of some type to keep track of them before they enter a name at checkout time and get a cusyomer ID? And what would be the most effective way to do this?

Thanks
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

It would be nearly as big of a strain on the server to use a DB compared to what keeping the purchase info of several hundred or thousand users in memory would be.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

http://www.php.net/unset

example:

Code: Select all

$arrayї'0'] = "foo";
$arrayї'1'] = "bar";
echo $arrayї'1']; // PRINTS "bar"
unset($arrayї'1']);
echo $arrayї'1']; // PRINTS ""
you might want to look into objects though; http://www.php.net/oop
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

just be aware that unset will leave a hole in your array.
That's not too bad but you should keep it in mind. e.g.

Code: Select all

<?php
$a = array('a', 'b', 'c');
for($i=0; $i!=count($a); $i++)
	echo $a[$i], ' ';
echo "<br/>\n";	

unset($a[1]);

for($i=0; $i!=count($a); $i++)
	echo $a[$i], ' ';
?>
the second loop will only output one element, since after unset
  • $a is equal to array(0=>'a', 2=>'c')
  • count($a) will return 2
  • and the loop therefor tries to access $a[0] and $a[1], the latter is no more.
foreach will be uneffected.
Post Reply