Jump to content

explode question


petbar

Recommended Posts

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.

 

 

Link to comment
Share on other sites

$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

 

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.