Page 1 of 1
Can you remove a variable from an array???
Posted: Sat Jan 11, 2003 9:23 pm
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
Posted: Sat Jan 11, 2003 11:13 pm
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.
Posted: Sun Jan 12, 2003 12:15 pm
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
Posted: Sun Jan 12, 2003 12:31 pm
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.
Posted: Sun Jan 12, 2003 7:59 pm
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
Posted: Sun Jan 12, 2003 8:13 pm
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.