redarrow Posted December 10, 2008 Share Posted December 10, 2008 Can any off you give me an example, to get the total of price better then it shown please........ <?php session_start(); $x=array("jumper"=>"10.00","jacket"=>"20.00","jeans"=>"30.00","coat"=>"40.00","shoes"=>"50.00"); foreach($x as $key=>$val){ $_SESSION['item'][]=$key; $_SESSION['price'][]=$val; } for($i=0; $i<5; $i++){ $total=(($_SESSION['price'][0])+($_SESSION['price'][1])+($_SESSION['price'][2]) +($_SESSION['price'][3])+($_SESSION['price'][4])+($_SESSION['price'][5])); echo " <table> <tr> <td>Item</td> <td>".$_SESSION['item'][$i]."</td> <tr><td>Price</tr></td> <td>£".$_SESSION['price'][$i]."</td></tr></table><br>"; } echo" <table><tr><td>Total price: $total</td></tr></table>" ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted December 10, 2008 Share Posted December 10, 2008 Err, are you looking for the array_sum function? Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 10, 2008 Author Share Posted December 10, 2008 When i explode to get a new array all the arrays are [0] i need a way to sum the arrays up and use hopefully array_sum() function currently only sees the £50. so what do i do to get the sum off, all off the arrays of [0] might not make scence sorry.. example below $value=explode(' ',$_SESSION['price'][$i]); print_r($value); result Array ( [0] => 10 ) <table> <tr> <td>Item</td> <td>jumper</td> <tr><td>Price</tr></td> <td>£10</td></tr></table><br>Array ( [0] => 200 ) <table> <tr> <td>Item</td> <td>jacket</td> <tr><td>Price</tr></td> <td>£200</td></tr></table><br>Array ( [0] => 30.00 ) <table> <tr> <td>Item</td> <td>jeans</td> <tr><td>Price</tr></td> <td>£30.00</td></tr></table><br>Array ( [0] => 40.00 ) <table> <tr> <td>Item</td> <td>coat</td> <tr><td>Price</tr></td> <td>£40.00</td></tr></table><br>Array ( [0] => 50.00 ) Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 10, 2008 Author Share Posted December 10, 2008 worked yee haa thank you...... <?php session_start(); $x=array("jumper"=>"10","jacket"=>"200","jeans"=>"30.00","coat"=>"40.00","shoes"=>"50.00"); foreach($x as $key=>$val){ if(($key && $val)!=''){ $_SESSION['item'][]=$key; $_SESSION['price'][]=$val; }else{ $key=''; $val=''; $_SESSION['item'][]=$key; $_SESSION['price'][]=$val; } } for($i=0; $i<5; $i++){ $total=array_sum($_SESSION['price']); //$total=(($_SESSION['price'][0])+($_SESSION['price'][1])+($_SESSION['price'][2]) // +($_SESSION['price'][3])+($_SESSION['price'][4])+($_SESSION['price'][5])); echo " <table> <tr> <td>Item</td> <td>".$_SESSION['item'][$i]."</td> <tr><td>Price</tr></td> <td>£".$_SESSION['price'][$i]."</td></tr></table><br>"; } echo" <table><tr><td>Total price: $total</td></tr></table>" Quote Link to comment 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.