RE: Silly varible question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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: m.ford@xxxxxxxxxxxxxx
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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux