Jump to content

array - string function help


Paul EC1

Recommended Posts

Hi all,

 

 

I have an array that has been formed from a string, the array is basically the contents of a shopping cart.

What i need is the array code to handle more than one product and a count the products to be given as $item_count and place at the front of a exit string

 

 

The basic input array / string looks like this before the code is run. Basically 4 vars (item : price : qty : RowTotal)

e.g. for one item

:White Spray :80.00 :1 :80.00

 

e.g for two items

:White Spray :80.00 :1 :80.00:Blue Spray :70.00 :1 :70.00

 

 

 

 

My output current code thats only good for one item

*Note $item_count is a place holder at the moment.

**Note $parts[0]; has been previously dropped 

 

$oldstring =$strCart ;

$parts = explode(":",$oldstring);

$a = $parts[2];  // item

$b = $parts[3];   // item price

$c  =$parts[4];  //qty

$d  =$parts[5];  //row items total

 

$b1 = ($b/1.175); /// Price ex-Vat

$b2 =  ($b*7/47); /// VAT component

 

 

$newstring = ":$a:$c:$b1:$b2:$b:$d" ;

//$parts = "

 

$strCart = $newstring;

 

///////////Result for one item ( item count : item : qty : Price -tax : tax : Full Price : Row Total)

   $item_count :White Spray :1 :68.08 :11.91 :80.00 :80.00

 

 

/// Ideal result for two items!!!

 

2 :White Spray :1 :68.08 :11.91 :80.00 :80.00 :Blue Spray :1 :68.08 :11.91 :80.00 :80.00

 

 

I have this code from a template, but it does not work with my string

 

 

$sngTotal=0.0;

$strThisEntry=$strCart;

$strBasket="";

$iBasketItems=0;

 

 

while (strlen($strThisEntry)>0) {

// Extract the Quantity and Product from the list of "x of y," entries in the cart

$iQuantity=cleanInput(substr($strThisEntry,0,1),"Number");

$iProductId=substr($strThisEntry,strpos($strThisEntry,",")-1,1);

// Add another item to our

$iBasketItems=$iBasketItems+1;

 

$sngTotal=$sngTotal + $iQuantity * $arrProducts[$iProductId-1][1];

$strBasket=$strBasket . ":" . $arrProducts[$iProductId-1][0] . ":" . $iQuantity;

$strBasket=$strBasket . ":" . number_format($arrProducts[$iProductId-1][1]/1.175,2); /** Price ex-Vat **/

$strBasket=$strBasket . ":" . number_format($arrProducts[$iProductId-1][1]*7/47,2); /** VAT component **/

$strBasket=$strBasket . ":" . number_format($arrProducts[$iProductId-1][1],2); /** Item price **/

$strBasket=$strBasket . ":" . number_format($arrProducts[$iProductId-1][1]*$iQuantity,2); /** Line total **/

 

// Move to the next cart entry, if there is one

$pos=strpos($strThisEntry,",");

if ($pos==0)

$strThisEntry="";

else

$strThisEntry=substr($strThisEntry,strpos($strThisEntry,",")+1);

}

 

 

 

Any help emulating this code wold be very much appreciated

(as you can guess i am no php expert :o)

 

 

Many thanks

Paul

 

 

P.s. it's worth noting that the source for 3 item looks like this

 

:White Spray

:80.00

:1

:80.00

 

:Pink Quattro

:45.00

:3

:135.00

 

:Freesia Mix

:35.00

:1

:35.00

 

 

Link to comment
https://forums.phpfreaks.com/topic/114164-array-string-function-help/
Share on other sites

$theItems = explode(":",$newstring);
$strBasket = '';
while ($i < count($theItems)) {


$strBasket .= 'Item:'. $theItems[$i].'<br />';   ++$i;
$strBasket .= 'Quantity:'. $theItems[$i].'<br />';   ++$i; $iQuantity = $theItems[$i];
$strBasket .= 'Price (-tax):'. number_format($theItems[$i]/1.175,2).'<br />';   ++$i;
$strBasket .= 'Tax:'. number_format($theItems[$i]*7/47,2).'<br />';   ++$i;
$strBasket .= 'Full Price:'. number_format($theItems[$i],2).'<br />';   ++$i; $fPrice = $theItems[$i];
$strBasket .= 'Row Total:'. number_format($fPrice*$iQuantity,2).'<br />';   ++$i;

}

 

Assuming $newstring is the string of items :)

 

 

Sorry it’s not working i think i am missing something, it’s late in the uk, well past my bed time !! perhaps that explains a thing or two

 

 

The code now is

 

*note the input string for two items is $strCart  that =  : :Baby Basket Blue :35.00 :2 :70.00 :Pink & Purple Bouquet :32.00 :1 :32.00

Also note that i used my old code to stripped the first : (the one i wanted replacing with the row count)

 

The output for this code is

Basket=1: :Baby Basket Blue :35.00 :2 :70.00 :Pink & Purple Bouquet :32.00 :1 :32.00 :Delivery:1:5.00:---:5.00:5.00

And should be

Basket=2: Baby Basket Blue :2  bla bla bla...

 

 

$sngTotal=0.0;

$strThisEntry=$strCart;

$strBasket="";

$iBasketItems=0;

 

 

$theItems = explode(":",$strCart);

$strBasket = '';

while ($i < count($theItems)) {

 

 

$strBasket .= 'Item:'. $theItems[$i].'<br />';  ++$i;

$strBasket .= 'Quantity:'. $theItems[$i].'<br />';  ++$i; $iQuantity = $theItems[$i];

$strBasket .= 'Price (-tax):'. number_format($theItems[$i]/1.175,2).'<br />';  ++$i;

$strBasket .= 'Tax:'. number_format($theItems[$i]*7/47,2).'<br />';  ++$i;

$strBasket .= 'Full Price:'. number_format($theItems[$i],2).'<br />';  ++$i; $fPrice = $theItems[$i];

$strBasket .= 'Row Total:'. number_format($fPrice*$iQuantity,2).'<br />';  ++$i;

 

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.