[PHP] Silly varible question
I know this is probably simple as all get out but it's early and I can't find an answer anywhere after searching for a while I need to assign a number to a variable and then use that variable in a session to store an array. It's for a shopping cart system I'm building. What I've got is: $count = 1; //First item in the cart passed from form to form $item = array(); $item[] = $_POST['phone']; $item[] = $_POST['category']; $item[] = $_POST['copy']; $item[] = $_POST['pic_style']; $item[] = $cost; $item[] = $image; session_register('item'); In the session the item is known as 'item'. What I need for it to be is 'item1' so when the customer chooses another product I can increase $count by 1 and have the next array with values stored as 'item2'. Thanks for any help, Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Silly varible question
Why not just have another array... $bag = array(); $item[] array_push($bag, $item); then store the bag in the session. so, you would have count($bag) items in your shopping cart, and you would be able to easily access them. Just a thought, instead of munging variable names. -B Ed Curtis wrote: I know this is probably simple as all get out but it's early and I can't find an answer anywhere after searching for a while I need to assign a number to a variable and then use that variable in a session to store an array. It's for a shopping cart system I'm building. What I've got is: $count = 1; //First item in the cart passed from form to form $item = array(); $item[] = $_POST['phone']; $item[] = $_POST['category']; $item[] = $_POST['copy']; $item[] = $_POST['pic_style']; $item[] = $cost; $item[] = $image; session_register('item'); In the session the item is known as 'item'. What I need for it to be is 'item1' so when the customer chooses another product I can increase $count by 1 and have the next array with values stored as 'item2'. Thanks for any help, Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Silly varible question
Brad Bonkoski wrote: Why not just have another array... $bag = array(); $item[] array_push($bag, $item); then store the bag in the session. so, you would have count($bag) items in your shopping cart, and you would be able to easily access them. Just a thought, instead of munging variable names. -B use the $_SESSION super global instead of using session_register() (read the manual for the fine print regarding $_SESSION and why session_register() is no longer recommended) e.g.: function addItemToBag($item) { if (!isset($_SESSION['bag'])) $_SESSION['bag'] = array(); $_SESSION['bag'][] = $item; } session_register('item'); In the session the item is known as 'item'. What I need for it to be is 'item1' so when the customer chooses another product I can increase $count by 1 and have the next array with values stored as 'item2'. Thanks for any help, Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Silly varible question
On 13/07/06, Ed Curtis [EMAIL PROTECTED] wrote: I know this is probably simple as all get out but it's early and I can't find an answer anywhere after searching for a while I need to assign a number to a variable and then use that variable in a session to store an array. It's for a shopping cart system I'm building. What I've got is: $count = 1; //First item in the cart passed from form to form $item = array(); $item[] = $_POST['phone']; $item[] = $_POST['category']; $item[] = $_POST['copy']; $item[] = $_POST['pic_style']; $item[] = $cost; $item[] = $image; session_register('item'); In the session the item is known as 'item'. What I need for it to be is 'item1' so when the customer chooses another product I can increase $count by 1 and have the next array with values stored as 'item2'. Thanks for any help, Ed First off, don't use session_register, use the $_SESSION superglobal array, that way you can create: $_SESSION['count'] = 1 and increment like so: $_SESSION['count']++ then you can create the cart like so: $item{$_SESSION['count']} = array(); does this make sense. Maybe not, but then again I've had far too much coffee. Make sense to the php gurus out there? -- http://www.web-buddha.co.uk http://www.projectkarma.co.uk
RE: [PHP] Silly varible question
On 13 July 2006 13:53, Ed Curtis wrote: I know this is probably simple as all get out but it's early and I can't find an answer anywhere after searching for a while I need to assign a number to a variable and then use that variable in a session to store an array. It's for a shopping cart system I'm building. What I've got is: $count = 1; //First item in the cart passed from form to form $item = array(); $item[] = $_POST['phone']; $item[] = $_POST['category']; $item[] = $_POST['copy']; $item[] = $_POST['pic_style']; $item[] = $cost; $item[] = $image; session_register('item'); In the session the item is known as 'item'. What I need for it to be is 'item1' so when the customer chooses another product I can increase $count by 1 and have the next array with values stored as 'item2'. Just make $item a 2-dimensional array, with the first diemnsion addressed by $count; so: $item[$count][] = $_POST['phone']; $item[$count][] = $_POST['category']; $item[$count][] = $_POST['copy']; $item[$count][] = $_POST['pic_style']; $item[$count][] = $cost; $item[$count][] = $image; $count += 1; $item[$count][] = ...; $item[$count][] = ...; // etc. Incorporating the (good) advice to use $_SESSION, this becomes: $_SESSION['item'][$count][] = $_POST['phone']; $_SESSION['item'][$count][] = $_POST['category']; $_SESSION['item'][$count][] = $_POST['copy']; $_SESSION['item'][$count][] = $_POST['pic_style']; $_SESSION['item'][$count][] = $cost; $_SESSION['item'][$count][] = $image; $count += 1; $_SESSION['item'][$count][] = ...; $_SESSION['item'][$count][] = ...; // etc. Personally, I'd be inclined to use string indexes that indicate what's in each element, to avoid having to remember somehow which numeric index corresponds to which attribute; so: $_SESSION['item'][$count]['phone'] = $_POST['phone']; $_SESSION['item'][$count]['category'] = $_POST['category']; $_SESSION['item'][$count]['copy'] = $_POST['copy']; $_SESSION['item'][$count]['pic_style'] = $_POST['pic_style']; $_SESSION['item'][$count]['cost'] = $cost; $_SESSION['item'][$count]['image'] = $image; $count += 1; $_SESSION['item'][$count]['phone'] = ...; $_SESSION['item'][$count]['category'] = ...; // etc. HTH Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Silly varible question
Just make $item a 2-dimensional array, with the first diemnsion addressed by $count; so: $item[$count][] = $_POST['phone']; $item[$count][] = $_POST['category']; $item[$count][] = $_POST['copy']; $item[$count][] = $_POST['pic_style']; $item[$count][] = $cost; $item[$count][] = $image; $count += 1; $item[$count][] = ...; $item[$count][] = ...; // etc. Incorporating the (good) advice to use $_SESSION, this becomes: $_SESSION['item'][$count][] = $_POST['phone']; $_SESSION['item'][$count][] = $_POST['category']; $_SESSION['item'][$count][] = $_POST['copy']; $_SESSION['item'][$count][] = $_POST['pic_style']; $_SESSION['item'][$count][] = $cost; $_SESSION['item'][$count][] = $image; $count += 1; $_SESSION['item'][$count][] = ...; $_SESSION['item'][$count][] = ...; // etc. Personally, I'd be inclined to use string indexes that indicate what's in each element, to avoid having to remember somehow which numeric index corresponds to which attribute; so: $_SESSION['item'][$count]['phone'] = $_POST['phone']; $_SESSION['item'][$count]['category'] = $_POST['category']; $_SESSION['item'][$count]['copy'] = $_POST['copy']; $_SESSION['item'][$count]['pic_style'] = $_POST['pic_style']; $_SESSION['item'][$count]['cost'] = $cost; $_SESSION['item'][$count]['image'] = $image; $count += 1; $_SESSION['item'][$count]['phone'] = ...; $_SESSION['item'][$count]['category'] = ...; // etc. Thanks for all the help guys. I now have a better understanding of multi dimensional arrays and can use it for what I'm trying to accomplish. This list is the greatest!! Thanks, Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Silly varible question
Ed Curtis wrote: ... Thanks for all the help guys. I now have a better understanding of multi dimensional arrays and can use it for what I'm trying to accomplish. This as Mary Poppins said 'php arrays are scrumptious'. list is the greatest!! yeah we rule (the land of goblins, fairies and helpless little leprecauns) ;-) PS - ok, I admit it, Mary Poppins didn't say that. Thanks, Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php