Re: array, trouble updating

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

 



On Tuesday 17 May 2005 21:18, mayo wrote:
> I usually work with cold fusion and took on a little project to get my
> feet wet in php and am spinning my wheels.  What I thought might be
> difficult was easy and what I thought would be a piece of cake has
> caused me much grief over the last few days.

It appears that your overall problem is a misunderstanding of how arrays 
work in PHP and how easy it is to manipulate them.

First:

> if (session_is_registered('ses_basket_items')){

In general, for practical purposes (if PHP is installed using the 
recommended default setup, ie register_globals disabled), 
session_is_registered() is deprecated and should not be used. Use:

  if (isset($_SESSION['ses_basket_items'])) { ... };


Second:

>             if ($action == "empty")
>             {
>                         while ($ses_basket_items > -1)
>                         {
>                                     array_splice ($ses_basket_name,
> $ses_basket_items, 1);
>                                     array_splice ($ses_basket_amount,
> $ses_basket_items, 1);
>                                     array_splice ($ses_basket_price,
> $ses_basket_items, 1);
>                                     array_splice ($ses_basket_id,
> $ses_basket_items, 1);
>                                     $ses_basket_items--;
>                         }
>             }

You seem to be using multiple single dimension arrays to store your basket 
details. That is not the optimal way of doing things. You should have a 
*single* multi dimension array, there are many ways to do this, here's a 
couple:

1)
  $basket[1] = array('name' => 'name of product',
                     'id' => 'product id',
                     'price' => 'price of product',
                     'amount' => 'quantity required');
  $basket[2] = array('name' => 'name of product',
                     'id' => 'product id',
                     'price' => 'price of product',
                     'amount' => 'quantity required');

2)
  $basket['a_product_id'] = array('name' => 'name of product',
                                  'price' => 'price of product',
                                  'amount' => 'quantity required');

  $basket['another_product_id'] = array('name' => 'name of product',
                                        'price' => 'price of product',
                                        'amount' => 'quantity required');


OK, so how do you operate on them? In the examples below 2 forms will be 
given corresponding to how you defined the arrays as per above.

To remove an item:

1) unset($basket[n]); // where n is an integer 
2) unset($basket['product_id']; // if product_id is an integer
                                // then you don't need the single-quotes
                                // note that also applies when first
                                // define the array, ie:
                                // $basket[product_id] = array(...);

To change an attribute, eg the amount:

1) $basket[n]['amount'] = 5;
2) $basket['product_id'] = 10;


To display an attribute, eg price:

1) echo $basket[n]['price'];
2) echo $basket['product_id']['price'];


When playing around with arrays, print_r() is your friend, use it 
liberally.

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
------------------------------------------
New Year Resolution: Ignore top posted posts

-- 
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