Jump to content

Val addition


timmah1

Recommended Posts

How can I get a total of the values with this

 

while (list ($name,$val) = @each ($_POST['package_'])) {  	

	$raw = "$val"; 
	$price = explode(",", $raw);
	$total += $price[0];

 

Can anybody tell me why the value is 0 when the 2 values are 89.99 and 30.00?

 

Thanks in advance

 

Link to comment
Share on other sites

From this code

<input name="package_[<?=$a[name]; ?>]" type="radio" value="<?=$price;?>,<?=$expDate;?>"><?=$a[name];?>

$name = packag_name;
$val = price, expDate;
$total += $price[0];

 

Then I'm exploding the $val, so that the expDate carries with every package checked.

 

Then, I need to add to add all the $price[0] (since it's the first variable) to give me a total price.

Even if there is only one package selected, $total should still echo the total price

 

So you can see where that field is coming from

$sql = "SELECT * FROM nba WHERE active = '1' ORDER BY id";
$q = mysql_query($sql);			
while($a = mysql_fetch_assoc($q)){ 
if($a['price'] == "n/a") {
	$price = "N/A";
}
else {
	$price1 = number_format ($a['price'], 2); 
	$price = "$".$price1;
}
$packID = $a['id'];
?>
<tr>
<td align="left" valign="top">
<?php		


		if($packID === "1"){ 
		$expDate = date("Y-m-d 23:59:59");
		   }
		   
		   elseif($packID === "2"){ 
			$expDate = date("Y-m-d 23:59:59", strtotime('+1 week'));
		   }
		   
		   elseif($packID === "3"){
			$expDate = date("Y-m-d 23:59:59", strtotime('+30 days'));
		   }	   
		   
		   elseif($packID === "4"){ 
			$expDate = date("Y-m-d 23:59:59", strtotime($stopDate4));
		   }
		   
		   elseif($packID === "5"){ 			   
			$expDate = date("Y-m-d 23:59:59", strtotime($stopDate5));
		   }
		   
		   elseif($packID === "6"){ 
		   $expDate = date("Y-m-d 23:59:59", strtotime($stopDate6));

		   }
?>
<input name="package_[<?=$a[name]; ?>]" type="radio" value="<?=$price;?>,<?=$expDate;?>"><?=$a[name];?>

Link to comment
Share on other sites

I don't think you see what I'm seeing, let's look at your original code you posted...

while (list ($name,$val) = @each ($_POST['package_'])) {     

      $raw = "$val";
      $price = explode(",", $raw);
      $total += $price[0];

 

You've set a while() loop starting there then second line down you're accessing $raw (exploding it) but there's nowhere I can see inside that loop that you're assigning $raw a value.

Link to comment
Share on other sites

I guess I'm confused on what your asking.

 

If I have a form field name "test" and a value of "testing"

On the next page, after clicking on submit

 

while (list ($name,$val) = @each ($_POST['package_'])) {  
echo $name $val;
}

Would be the same as this

while (list (test,testing) = @each ($_POST['test])) {  
echo $test $testing;
}

 

Does that make sense?

 

 

while (list (name_of_field,value_of_field) = @each ($_POST['field]))

Link to comment
Share on other sites

Everything echos fine, except for trying to add the values, that's where I'm stumped.

 

Everything worked for before I put the explode on there.

Now that I exploded the values because I put 2 values on the field, it don't add, but when I echo

$price[0];

 

It shows with no problem

Link to comment
Share on other sites

How about this?

while (list ($name,$val) = @each ($_POST['package_'])) {     

      $price = explode(",", $val);
      $tmp=$price[0];
      $total += $tmp;

 

Might seem a waste of time but seeing as you're echo'ing the value you want but it won't add - it's worth a shot!

Link to comment
Share on other sites

I think you've not grouped your radio buttons properly - this might have something to do with your options not coming up properly but as for adding them together still doesn't really explain it.

 

filevs0.th.png

 

I can select nearly all the radio buttons and some in one section cancel out those in another section!

Link to comment
Share on other sites

The logic seems OK - i did this as a test and the total works fine.  Are you sure $_POST['package_'] is right?

 

<?php
$package=array("bread"=>89.99,"cheese"=>30.00);
$total=0;

while (list ($name,$val) = @each ($package)) {     
      $raw = "$val";
      $price = explode(",", $raw);
      $total += $price[0];
  }
echo $total;
?>

Link to comment
Share on other sites

I think you're using the radio buttons wrong - each group (in your case tables, 4 of them) should have the same name and you've got them having different names.

 

Now I'm presuming you want the user to be able to select only one option from each box? If not then checkboxes might be more appropriate.

 

If you do want only one option selectable from each box then you need to name each box differently but each option within each box named the same.

 

Eg.

BOX 1

name="box1"

name="box1"

name="box1"

name="box1"

 

BOX 2

name="box2"

name="box2"

name="box2"

name="box2"

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.