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

?>

Link to comment
Share on other sites

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
)


Link to comment
Share on other sites

 

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

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.