[PHP] array, trouble updating

2005-05-17 Thread mayo
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.
 
I'm making a little shopping basket, writing the results to a file,
ftping the file to a distributor and sending the CC data to merchant
services company.
 
Write to file - easy
FTP file to distributor - easy
Updating the array in the shopping basket -- not so easy !!??!!
 
A shopping basket should be able to add and delete items (done)
You should be able to delete the whole basket (done)
Now I need to be able to update quantity - I've spent at least 5 or 6
hours on this and have gotten nowhere!!! This learning exercise is
getting to be a major time drag.
 
Any hints would be good. 
 
The code is below:
 
 
?php
 
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--; 
} 
} 
 
if ($action2 == deleteItem) 
{ 
array_splice ($ses_basket_name, $position, 1); 
array_splice ($ses_basket_amount, $position, 1);

array_splice ($ses_basket_price, $position, 1); 
array_splice ($ses_basket_id, $position, 1); 
$ses_basket_items--;
} 
 
?
?php 
 
if ($_GET['basket']!=){ 
if (session_is_registered('ses_basket_items')){ 
$basket_position_counter=0; 
$double=0; 
if ($_SESSION['ses_basket_items']0){ 
foreach ($_SESSION['ses_basket_name'] as $basket_item){ 
if ($basket_item==$_GET['basket']){ 
$double=1; 
$subtract=1;
$basket_position=$basket_position_counter; 
 
} 
$basket_position_counter++; 
} 
} 
 
if ($double==1){ 

$oldamount=$_SESSION['ses_basket_amount'][$basket_position];

$_SESSION['ses_basket_amount'][$basket_position]++; 
$amount=$_SESSION['ses_basket_amount'][$basket_position]; 
$oldprice=$_SESSION['ses_basket_price'][$basket_position]; 
$newprice=($oldprice/$oldamount)*$amount; 
$_SESSION['ses_basket_price'][$basket_position]=$newprice; 
 
}else{ 
$_SESSION['ses_basket_name'][]=$_GET['basket']; 
$_SESSION['ses_basket_amount'][]=1; 
$_SESSION['ses_basket_price'][]=$_GET['price']; 
$_SESSION['ses_basket_id'][]=$_GET['id']; 
$_SESSION['ses_basket_items']++; 
} 
}else{ 
 
$_SESSION['ses_basket_items']=1; 
$_SESSION['ses_basket_name'][0]=$_GET['basket']; 
$_SESSION['ses_basket_amount'][0]=1; 
$_SESSION['ses_basket_price'][0]=$_GET['price']; 
$_SESSION['ses_basket_id'][0]=$_GET['id']; 
session_register(ses_basket_items); 
session_register(ses_basket_name); 
session_register(ses_basket_amount); 
session_register(ses_basket_price); 
session_register(ses_basket_id); 
} 
} 
 

if ($_SESSION['ses_basket_items']0){ 
 
for
($basket_counter=0;$basket_counter$_SESSION['ses_basket_items'];$basket
_counter++){ 
// basket output 
$price=sprintf(%01.2f,$_SESSION['ses_basket_price'][$basket_counter]);

$amount=$_SESSION['ses_basket_amount'][$basket_counter]; 
$name=$_SESSION['ses_basket_name'][$basket_counter]; 
$aaa=$basket_counter;
 
echo $amount $name $price;
echo a href=\2.php?action2=deleteItemposition= . $aaa .
\DEL/a;
echo a
href=\2.php?id=1001price=25basket=mousesubtract=yes\-sub/a;
echo $aaa;
 
echo BR\n; 
} 
} else { 
 
$_SESSION['ses_basket_items']=0; 
unset($_SESSION['ses_basket_name']); 
unset($_SESSION['ses_basket_amount']); 
unset($_SESSION['ses_basket_price']); 
unset($_SESSION['ses_basket_id']); 
} 
 
?
 


Re: [PHP] array, trouble updating

2005-05-17 Thread Jason Wong
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



RE: [PHP] array, trouble updating

2005-05-17 Thread mayo

Jason,

Thx. Even though I have what I need for the client (I finally got things
to work as desired), I'll rework it and update the client version.

I knew I was doing things poorly and it degenerated into simply get the
damn thing to work.

Thx, Mayo



-Original Message-
From: Jason Wong [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, May 17, 2005 5:56 PM
To: php-general@lists.php.net
Subject: Re: [PHP] array, trouble updating

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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] array, trouble updating - SOLVED

2005-05-17 Thread mayo
Yikes,

Who would have thought something so easy would be such a pain in the
butt.

I solved it with embedded if-else clauses.

Thx all



-Original Message-
From: mayo [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, May 17, 2005 9:19 AM
To: 'php'
Subject: [PHP] array, trouble updating

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.
 
I'm making a little shopping basket, writing the results to a file,
ftping the file to a distributor and sending the CC data to merchant
services company.
 
Write to file - easy
FTP file to distributor - easy
Updating the array in the shopping basket -- not so easy !!??!!
 
A shopping basket should be able to add and delete items (done)
You should be able to delete the whole basket (done)
Now I need to be able to update quantity - I've spent at least 5 or 6
hours on this and have gotten nowhere!!! This learning exercise is
getting to be a major time drag.
 
Any hints would be good. 
 
The code is below:
 
 
?php
 
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--; 
} 
} 
 
if ($action2 == deleteItem) 
{ 
array_splice ($ses_basket_name, $position, 1); 
array_splice ($ses_basket_amount, $position, 1);

array_splice ($ses_basket_price, $position, 1); 
array_splice ($ses_basket_id, $position, 1); 
$ses_basket_items--;
} 
 
?
?php 
 
if ($_GET['basket']!=){ 
if (session_is_registered('ses_basket_items')){ 
$basket_position_counter=0; 
$double=0; 
if ($_SESSION['ses_basket_items']0){ 
foreach ($_SESSION['ses_basket_name'] as $basket_item){ 
if ($basket_item==$_GET['basket']){ 
$double=1; 
$subtract=1;
$basket_position=$basket_position_counter; 
 
} 
$basket_position_counter++; 
} 
} 
 
if ($double==1){ 

$oldamount=$_SESSION['ses_basket_amount'][$basket_position];

$_SESSION['ses_basket_amount'][$basket_position]++; 
$amount=$_SESSION['ses_basket_amount'][$basket_position]; 
$oldprice=$_SESSION['ses_basket_price'][$basket_position]; 
$newprice=($oldprice/$oldamount)*$amount; 
$_SESSION['ses_basket_price'][$basket_position]=$newprice; 
 
}else{ 
$_SESSION['ses_basket_name'][]=$_GET['basket']; 
$_SESSION['ses_basket_amount'][]=1; 
$_SESSION['ses_basket_price'][]=$_GET['price']; 
$_SESSION['ses_basket_id'][]=$_GET['id']; 
$_SESSION['ses_basket_items']++; 
} 
}else{ 
 
$_SESSION['ses_basket_items']=1; 
$_SESSION['ses_basket_name'][0]=$_GET['basket']; 
$_SESSION['ses_basket_amount'][0]=1; 
$_SESSION['ses_basket_price'][0]=$_GET['price']; 
$_SESSION['ses_basket_id'][0]=$_GET['id']; 
session_register(ses_basket_items); 
session_register(ses_basket_name); 
session_register(ses_basket_amount); 
session_register(ses_basket_price); 
session_register(ses_basket_id); 
} 
} 
 

if ($_SESSION['ses_basket_items']0){ 
 
for
($basket_counter=0;$basket_counter$_SESSION['ses_basket_items'];$basket
_counter++){ 
// basket output 
$price=sprintf(%01.2f,$_SESSION['ses_basket_price'][$basket_counter]);

$amount=$_SESSION['ses_basket_amount'][$basket_counter]; 
$name=$_SESSION['ses_basket_name'][$basket_counter]; 
$aaa=$basket_counter;
 
echo $amount $name $price;
echo a href=\2.php?action2=deleteItemposition= . $aaa .
\DEL/a;
echo a
href=\2.php?id=1001price=25basket=mousesubtract=yes\-sub/a;
echo $aaa;
 
echo BR\n; 
} 
} else { 
 
$_SESSION['ses_basket_items']=0; 
unset($_SESSION['ses_basket_name']); 
unset($_SESSION['ses_basket_amount']); 
unset($_SESSION['ses_basket_price']); 
unset($_SESSION['ses_basket_id']); 
} 
 
?
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php