Paul EC1 Posted July 11, 2008 Share Posted July 11, 2008 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 <?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) Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/ Share on other sites More sharing options...
kenrbnsn Posted July 11, 2008 Share Posted July 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587515 Share on other sites More sharing options...
Paul EC1 Posted July 11, 2008 Author Share Posted July 11, 2008 Hi Ken, you must have telepathy 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) Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587554 Share on other sites More sharing options...
the shig Posted July 11, 2008 Share Posted July 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587654 Share on other sites More sharing options...
sasa Posted July 11, 2008 Share Posted July 11, 2008 <?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 ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587693 Share on other sites More sharing options...
Paul EC1 Posted July 11, 2008 Author Share Posted July 11, 2008 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 ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587704 Share on other sites More sharing options...
Paul EC1 Posted July 11, 2008 Author Share Posted July 11, 2008 sasa give yourself a gold star as i have been at this fo hours now 10/10 Many many thanks Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587723 Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587728 Share on other sites More sharing options...
Paul EC1 Posted July 11, 2008 Author Share Posted July 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587750 Share on other sites More sharing options...
the shig Posted July 11, 2008 Share Posted July 11, 2008 i've got some code if you still need it. Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587755 Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 Honestly, rewriting it would make life much easier in the long run. A week (if that) of work will save you tons of time later on. Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587766 Share on other sites More sharing options...
Paul EC1 Posted July 11, 2008 Author Share Posted July 11, 2008 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 .. Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587771 Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 OSCommerce is pretty solid. You may need to toy with some things to get it just how you want it, but it's a solid backend and frontend and it really simplifies things. I've never used it, but I've heard good things about it. Quote Link to comment https://forums.phpfreaks.com/topic/114241-array-funtion-help-please/#findComment-587775 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.