I'm still learning this stuff, so please bear with me...
I have a shopping cart which returns a variable on checkout completion containing the cart contents. The variable looks like this:
product1 : quantity1 : price1 ~ product2 : quantity2 : price2
ie: parameters for each item in the cart delimited by : and each cart item delimited by ~
What I want to do is run a fairly complex routine on the cartcontents variable, which will involve breaking it down in to each product and then breaking down the different parameters of each product in order to update the product database and the shoppers account.
My plan is to make a function for the routine:
function processcart($product){
<<function contents>>}
split the variable into an array (using preg_split) thus:
$values = preg_split("/~/", "$cart");
and then run the function on each value in the array.
$product1 = $values[0];
function processcart($product1);
$product2 = $values[1];
function processcart($product2);
etc....(not sure if this is the simplest way to do it!)
The problem is, I don't know how many items (and therefore how many variables in the split cartcontents) there will be in the cart each time.
I realise that I could use count():
$noofproducts = count($values);
to work out the number of products, but I'm not sure how I can then use this number to get my routine to run the necessary number of times (and also increment the variable / array number - ie "$product1 = $values[0]" becomes "$product2 = $values[1]" each time it runs)
Thanks in advance...
I have a shopping cart which returns a variable on checkout completion containing the cart contents. The variable looks like this:
product1 : quantity1 : price1 ~ product2 : quantity2 : price2
ie: parameters for each item in the cart delimited by : and each cart item delimited by ~
What I want to do is run a fairly complex routine on the cartcontents variable, which will involve breaking it down in to each product and then breaking down the different parameters of each product in order to update the product database and the shoppers account.
My plan is to make a function for the routine:
function processcart($product){
<<function contents>>}
split the variable into an array (using preg_split) thus:
$values = preg_split("/~/", "$cart");
and then run the function on each value in the array.
$product1 = $values[0];
function processcart($product1);
$product2 = $values[1];
function processcart($product2);
etc....(not sure if this is the simplest way to do it!)
The problem is, I don't know how many items (and therefore how many variables in the split cartcontents) there will be in the cart each time.
I realise that I could use count():
$noofproducts = count($values);
to work out the number of products, but I'm not sure how I can then use this number to get my routine to run the necessary number of times (and also increment the variable / array number - ie "$product1 = $values[0]" becomes "$product2 = $values[1]" each time it runs)
Thanks in advance...