Jump to content

[SOLVED] adding up values with $_SESSION's in a array


redarrow

Recommended Posts

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>"

?>

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
)


 

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>"

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.