timmah1 Posted December 14, 2008 Share Posted December 14, 2008 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 Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 Ignore my post - read the $ as an & :-\ But you might as well change these lines: $raw = "$val"; $price = explode(",", $raw); To this: $price = explode(",", $val); Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 um, not sure what that is suppose to mean Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 Sorry - I edited my post as I made a silly mistake. What do the variables contain before they come across that code? Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 ok, got that. pretty redundant what I had Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 The values are pulled from this <input name="package_[<?=$a[name]; ?>]" type="radio" value="<?=$price;?>,<?=$expDate;?>"><?=$a[name];?> Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 OK I've not used list() before but it seems that you're making a loop but where exactly are you accessing the "package_" variables inside the loop? I mean, where is $val getting its value from? Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 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];?> Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 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. Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 $raw value is the value of the form field package_name Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 Yes you've already told me that but there is NOWHERE I can see you're assigning that value after while() Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 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])) Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 hahaha oops I've mis-read list() - I must have been thinking of some different command, sincere apologies! How about using echo to display the contents of the variables you're trying to add so that you can see you're getting something? Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 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 Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 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! Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 Not a waste of time, I'm willing to do what ever it takes. I put that on the code, and it didn't add the values, but it echoed out the $tmp for each product. You can see the results if you want http://cheezyfries.net/vegasD/secure/index.php After click on Order, the next page is where I'm getting things screwed up Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 I can see what you mean but something else strange happened. Let me grab a screenshot as I don't quite know how to explain this one off... Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 I put this $price = explode(",", $val); $tmp = $price[0]; $total[] += $tmp; And now it displays Array Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 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. I can select nearly all the radio buttons and some in one section cancel out those in another section! Quote Link to comment Share on other sites More sharing options...
Mad Mick Posted December 14, 2008 Share Posted December 14, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 I saw that, I fixed that with all the sports, not a real big issue though I still don't understand why the fields won't add up, makes no sense to me Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 Yes. $_POST['package_'] is correct. Like I said, it worked fine until I put the second value on the field and trying to explode the values Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 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" Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 btw the reason some buttons cancel out others is because the product names are the same in other boxes which gives your radio buttons the same conflicting names. Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2008 Author Share Posted December 14, 2008 They can select as many from each group that they want, there is no limit, except to how many products there are. That's not the issue at all, that works perfectly fine. 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.