petbar Posted December 10, 2011 Share Posted December 10, 2011 Need some help with something I have not done before. I have a dynamic checkbox set up to accept userinput then post $optionsname,$optionsprice. echo "<li><input type=\"checkbox\" name=\"optionslist[]\" value=\"$optionsname,$optionsprice\" /> $optionsname - $ $optionsprice - $optionsdisc</li>"; So far no problem $showoptions=$_POST['optionslist']; if(empty($showoptions)) { echo("You didn't select any options."); } else { $N = count($showoptions); echo("<strong>You selected $N options:</strong> "); for($i=0; $i < $N; $i++) { echo("<li>".$showoptions[$i] . "</li>"); } } Can someone tell me how to explode $showoptions into $optionsname,$optionsprice again. Thanks for any help in advanced. Quote Link to comment Share on other sites More sharing options...
freelance84 Posted December 10, 2011 Share Posted December 10, 2011 what are you exploding at? explode $showoptionsParts = explode('atsomething',$showoptions ); $optionsname = $showoptionsParts[0]; $optionsprice = $showoptionsParts[1]; Quote Link to comment Share on other sites More sharing options...
petbar Posted December 10, 2011 Author Share Posted December 10, 2011 $showoptionsParts = explode(',',$showoptions ); $optionsname = $showoptionsParts[0]; $optionsprice = $showoptionsParts[1]; echo "here".$optionsprice; Still nothing returned for $optionsprice. Would it be an issue if there are spaces in $optionsname ? example ( Lock and Unlock ). I have to be doing something really dumb that I'm just not catching Thanks for the reply Quote Link to comment Share on other sites More sharing options...
freelance84 Posted December 10, 2011 Share Posted December 10, 2011 what do you get when you print out $showoptions? print_r($showoptions); Quote Link to comment Share on other sites More sharing options...
petbar Posted December 10, 2011 Author Share Posted December 10, 2011 Array ( [0] => Lock and Unlock,29.00 [1] => Trunk Pop,19.00 ) so I have been trying $showoptionsParts = explode(',',$showoptions ); $optionsname = $showoptionsParts[0]; $optionsprice = $showoptionsParts[1]; echo "here".$optionsprice; even before you suggested. That is what is driving me nuts. again thanks for you time Quote Link to comment Share on other sites More sharing options...
xyph Posted December 10, 2011 Share Posted December 10, 2011 Explode expects a string, not an array. You have to loop through $showoptions, and explode each individual value. You'll probably want to use foreach Quote Link to comment Share on other sites More sharing options...
petbar Posted December 10, 2011 Author Share Posted December 10, 2011 That make a ton of sense Thank you will give it a try Quote Link to comment Share on other sites More sharing options...
petbar Posted December 11, 2011 Author Share Posted December 11, 2011 May I expand my question ? $showoptions=$_POST['optionslist']; $N = count($showoptions); for($i=0; $i < $N; $i++) { $showoptionsParts = explode(',',$showoptions[$i] ); $optionsname = $showoptionsParts[0]; $optionsprice = $showoptionsParts[1]; echo $optionsprice."<br />"; } Works great and returns The selected option prices. What is the best way for me to SUM the items to use further down the page. I was under the impression $showoptionsParts[1] was an array but I guess I'm incorrect AGAIN Quote Link to comment Share on other sites More sharing options...
xyph Posted December 11, 2011 Share Posted December 11, 2011 Here's an example. I've used a foreach instead of the combination of count() and for() that you've used. <?php $array = array( 23,45,17,26 ); $sum = 0; foreach( $array as $value ) { echo 'Sum now equals '.$sum.'.'; $sum += $value; echo ' We added '.$value.' to it, so now, sum is '.$sum.'<br>'; } ?> In my example, you could use array_sum instead. Since you need to explode your values, you have to keep a running total. Quote Link to comment Share on other sites More sharing options...
petbar Posted December 12, 2011 Author Share Posted December 12, 2011 $showoptions=$_POST['optionslist']; foreach( $showoptions as $key => $value){ $justprice =explode (',', $value); foreach( $justprice as $value ) { $sum += $value; } } $optionsprice=$sum; Thanks guys for all the help I got it Quote Link to comment Share on other sites More sharing options...
xyph Posted December 12, 2011 Share Posted December 12, 2011 You don't need the 2nd foreach. Use $justprice[1]. Quote Link to comment Share on other sites More sharing options...
petbar Posted December 12, 2011 Author Share Posted December 12, 2011 thank you again starting to clean up now. 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.