Jump to content

Array funtion help please


Paul EC1

Recommended Posts

Hi all ,

i am trying to get this to work ???, but it just outputs the input string any help would be very much apprriechated

 

 

My code is :o:(

 

<?php 

$strCart= ":New Arrival Collection :10.00 :1 :10.00 :Anturium Five :10.00 :2 :20.00"; 		// input string


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

}


// add delivery to the total and the basket
$sngTotal=$sngTotal+1.50;

$strBasket = $strCart ;
$strBasket=$iBasketItems+1 . $strBasket . ":Delivery:1:5.00:---:5.00:5.00";



echo $strCart;
// currently outputs ":New Arrival Collection :10.00 :1 :10.00 :Anturium Five :10.00 :2 :20.00”


// explanation of desired results for 
//    2 items                      “no of rows    : first item                 : qty  : price – tax   : tax    :  Price  : item total	:  ect for second + delivery	
// should output should be  “ 3                :New Arrival Collection  :1     :8.25             : 1.75  :10.00   :10.00       : Anturium Five :10.00 :2 :20.00

  ?>

 

 

 

Many thanks for you time and help

 

 

Paul

 

(edited by kenrbnsn to add


tags)

Link to comment
Share on other sites

You're doing all the work in the while loop, then doing:

<?php
$strBasket = $strCart ;
$strBasket=$iBasketItems+1 . $strBasket . ":Delivery:1:5.00:---:5.00:5.00";
echo $strCart;
?>

 

which overwrites that work and just outputs the original string.

 

Ken

Link to comment
Share on other sites

Hi Ken,

you must have telepathy ;D as i noticed that just as my email poped up,

 

 

But i still seem to have a problem ??? as my output seems to be mixed up and or missing an item

 

Here is my current code,

 

<?php 
$strCart= ":New Arrival Collection :10.00 :2 :10.00 :Anturium Five :10.00 :2 :20.00"; 		

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

// Move to the next cart entry, if there is one
$pos=strpos($strThisEntry,":");
if ($pos==0) 
	$strThisEntry="";
else
	$strThisEntry=substr($strThisEntry,strpos($strThisEntry,":")+1);
}

// add delivery to the total and the basket
$sngTotal=$sngTotal+1.50;
  
$strBasket=$iBasketItems+1 . $strBasket . ":Delivery:1:5.00:---:5.00:5.00";

echo $strBasket ;	

?>

 

 

it outputs the following

 

1Item:

Quantity:New Arrival Collection

Price (-tax):8.51

Tax:0.15

Full Price:10.00

Row Total:0.00

Item:10.00

Quantity:2

Price (-tax):17.02

Tax:0.00

Full Price:0.00

Row Total:0.00

:Delivery:1:5.00:---:5.00:5.00

 

 

But it should be

 

Items in basket: 2

Item: New Arrival Collection

Quantity:1

Price (-tax):8.51

Tax:1.49

Full Price:10.00

Row Total:10.00

Item: Anturium Five

Quantity:2

Price (-tax):17.02

Tax:2.98

Full Price:10.00

Row Total:20.00

:Delivery:1:5.00:---:5.00:5.00

 

 

Any and all help is very much apprsiated

 

Paul

 

(edited by kenrbnsn to add


tags)

Link to comment
Share on other sites

hi there,

 

turn on error_reporting(E_ALL);

 

how does the start data ($strCart ) get into the script. is it from a text file, database, $_SESSION etc. Also, what does the data look like with more items in the basket that are all different in price etc.

 

Can you provide an example of more data. then it will be possible to help you with a solution.

 

you may want to look at

 

<?php
list($var, $var, ...) = explode(':', $strCart);
?>

 

this would make the data much easier to put together, but would  only work if the $strCart input is always identical.

Hence, seeing more data would be helpful.

Link to comment
Share on other sites

<?php 
$strCart= ":New Arrival Collection :10.00 :1 :10.00 :Anturium Five :10.00 :2 :20.00"; 		

$sngTotal=0.0;
$strThisEntry= $strCart;
$strBasket="";
$iBasketItems=0;		
$theItems = explode(":",$strCart);
$strBasket = '';
$i = 1;
while ($i < count($theItems)) {
$item = $theItems[$i++];
$full_price = $theItems[$i++];
$quantity = $theItems[$i++];
$total = $theItems[$i++];
$strBasket .= 'Item:'. $item.'<br />';
$strBasket .= 'Quantity:'. $quantity.'<br />';
$strBasket .= 'Price (-tax):'. number_format($total/1.175,2).'<br />';
$strBasket .= 'Tax:'. number_format($total*7/47,2).'<br />';
$strBasket .= 'Full Price:'. number_format($full_price,2).'<br />';
$strBasket .= 'Row Total:'. number_format($total,2).'<br />';
$iBasketItems++;
}

// add delivery to the total and the basket
//$sngTotal=$sngTotal+1.50;
  
$strBasket= 'Items in basket:'.$iBasketItems . '<br />' . $strBasket . ":Delivery:1:5.00:---:5.00:5.00";

echo $strBasket ;	

?>

Link to comment
Share on other sites

Hi all, sorry but it’s worth noting i am very new to php.

 

$strCart  come from a shopping cart but, for the purpose of getting a clean exit string from the array, i have been feeding the array manually, just to make sure of data consistency

 

Her are some examples of the input string ($strCart )

 

For one item

New Arrival Collection :50.00 :2 :100.00 :

 

For two items

New Arrival Collection :50.00 :2 :100.00 :Anturium Five :40.00 :2 :80.00

 

For three items

New Arrival Collection :50.00 :2:100.00 :Anturium Five :40.00 :2 :80.00 :White Dream :34.00 :5 :34.00";

 

 

It’s all in the same format “item ,price , qty ,total” note that total in only a total for the item in that line and doeas not need to be recalculated.

 

As you can see from the code below

 

 

The first example i item

Input New Arrival Collection :50.00 :2 :100.00

 

Output

2Item:New Arrival Collection

Quantity:

Price (-tax):42.55

Tax:0.30

Full Price:100.00

Row Total:0.00

:Delivery:1:5.00:---:5.00:5.00

 

The secound example 2 items

Input New Arrival Collection :50.00 :2 :100.00 :Anturium Five :40.00 :2 :80.00

 

Output

3Item:New Arrival Collection

Quantity:

Price (-tax):42.55

Tax:0.30

Full Price:100.00

Row Total:0.00

Item:40.00

Quantity:40.00

Price (-tax):1.70

Tax:11.91

Full Price:0.00

Row Total:0.00

:Delivery:1:5.00:---:5.00:5.00

 

 

 

The third example 3 items

Input New Arrival Collection :50.00 :2 :100.00 :Anturium Five :40.00 :2 :80.00

 

Output

4Item:New Arrival Collection

Quantity:

Price (-tax):42.55

Tax:0.30

Full Price:100.00

Row Total:0.00

Item:40.00

Quantity:40.00

Price (-tax):1.70

Tax:11.91

Full Price:0.00

Row Total:68.00

Item:5

Quantity:5

Price (-tax):28.94

Tax:0.00

Full Price:0.00

Row Total:0.00

:Delivery:1:5.00:---:5.00:5.00

 

 

 

<?php

 

$strCart= "New Arrival Collection :50.00 :1 :50.00 :Anturium Five :40.00 :2 :80.00 :White Dream :34.00 :5 :34.00";

 

 

 

$sngTotal=0.0;

$strThisEntry= $strCart;

$strBasket="";

$iBasketItems=0;

 

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

 

$strBasket = '';

 

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

 

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

 

$iBasketItems=$iBasketItems+1;

 

 

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

 

 

 

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

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

if ($pos==0)

$strThisEntry="";

else

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

}

 

// add delivery to the total and the basket

$sngTotal=$sngTotal+1.50;

 

$strBasket= $iBasketItems+1  . $strBasket . ":Delivery:1:5.00:---:5.00:5.00";

 

 

 

echo $strBasket ;

 

?>

 

Link to comment
Share on other sites

I've noticed that you've made a lot of topics and posts about this cart issue...how come you're making life so difficult and make crazy strings and all sorts of stuff instead of just making an array in the sessions or using a pre-made e-commerce solution like OSCommerce?  First of all, your cart would break if a product had a : in it....second of all it's just clumsy.

Link to comment
Share on other sites

Hi,

it's a hand me down cart for a cheap skate friend of mine and it 's based on a flat file system, plus all of the $vars are dropt instantly and output into content html >.tpl

 

i had to extract the data from the html then re-jig it for the backend, if only it had a database and some vb my life would have a lot easier!!! How well..

 

 

Again Many thanks for everyone’s help

 

Paul

 

 

Link to comment
Share on other sites

Yes please for the code, as i am on a php learning cerv.

 

 

As for thr rejig of the code, that is somthing i am and have been looking into, i also do not like this sloppy approach.

I have had to rewrite a lot of the code already!!

 

i am just having a look at OSCommerce very interesting ..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.