breaking down a variable into two...

A

Anonymous

Guest
I guess that's the best way to describe what I am trying to do. I have a form that uses drop-down menus. They have name & price so that the user can choose what they want & when the submit is clicked, the php that will handle the info is to break up the data from the d-d menu and make one variable for item name and another for price. The item description will ALWAYS be from the beginning of the string to 3 spaces before the dollar sign and price will ALWAYS have the '$' right before the amount (duh...lol).

An example of the form output & what I need to handle would be like:

$product="Item Description + $123.00"

whereas it'll be broken down into the following:

$Item="Item Description"
and
$Price="123.00"

thus dropping the ' + $' from the original variable.

I want to break down the prices so that I can make the php also total everything together.

I was also thinking that IF (there's a good word, isn't it...IF) I could pass 2 variables through the d-d menu in the form, I could call them individually where needed. Try as I might, though. It will not work that way... :( (That'd have been an EASY solution to my problem if it'd have worked... :) ).

Many thanks for your help on this one.

If you have any further questions, please email me william@baldeagleelectronics.com

Thanks,

Bill
 
Firstly, I have no idea why you'd want a form to work like this. It takes a lot longer to find + and $ on the keyboard than it does to find the Tab key, it only seems natural that you'd have two different form fields and you'll have a lot less trouble with user error. I think what you see as a time-saving measure is only going to cause frustration and wasted time for your users.

Anyway, there's two ways to do this. First of all, to try to avoid a little trouble with user error, you can just user strpos() to find the + and $ and then grab what's before and after, respectively, using substr():
PHP:
<?php
$product = 'Item Description + $123.00';

$plus_pos = strpos($product, '+');
$dollar_pos = strpos($product, '$');

$item = trim(substr($product, 0, $plus_pos));
$price = trim(substr($product, $dollar_pos + 1));

echo $item . "\n" . $price;

/* output:
      Item Description
      123.00
*/
?>
The other way to do this is to use explode(), where we have to assume that there will be little to no user error (which is a huge assumption, but anyway..):
PHP:
<?php
$product = 'Item Description + $123.00';

list($item, $price) = explode(' + $', $product);

echo $item . "\n" . $price;

/* output:
      Item Description
      123.00
*/
?>
You could use trim() again to account for a little whitespace. Both of these methods are hugely susceptible to user error, however, and I implore you to consider using multiple form fields instead.
 
I am able to do it with this one command, too..

list( $color1, $price ) = explode( '$', $color, 3 );

Now, I'm trying to trying to make an if statement that will search the variable, look for a '*' and then do one thing if the variable has a '*' and another if it does not. Any suggestions?
 
Back
Top