Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 06-20-2010, 10:14 AM Syntax help needed
borzoid's Avatar
Webmaster Talker

Posts: 526
Name: Connie Taylor
Trades: 13
Hi, I have an application that I need to make changes to but have not been able to find the code who wrote it for a long time. It is a small change but I don't know the correct syntax.

the application has a form for adding animals to a database. On of the elements on the form is "Gender" and has radio buttons for male and female and enters the values into the db as "m" or "f" with male being selected by default. I have edited the form to give a third option "w" (for wether - a neutered male).

The save function has the following line of code in it for the gender:

PHP Code:
$gender = ( $gender == "male" ) ? "m" "f"
How do I change that to include the "w" choice?

Thanks in advance for any help you can give.
borzoid is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-20-2010, 05:43 PM Re: Syntax help needed
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I like the switch statement in those cases. Much more readable than a if/else series.
PHP Code:
switch($gender){
  case 
'male':
    
$gender='m';
    break;
  case 
'female':
    
$gender='f';
    break;
  case 
'wether':
    
$gender='w';
    break;

But you should rather use the "value" of the option rather than it's displayed value
HTML Code:
<select name'sel'>
  <option value='m'>Male</option>
  <option value='f'>female</option>
  <option value='w'>Wteher</option>
</select>
and you will get back directly 'm','f', or 'w' without needing to check for the text returned.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 06-20-2010 at 05:44 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-20-2010, 08:58 PM Re: Syntax help needed
borzoid's Avatar
Webmaster Talker

Posts: 526
Name: Connie Taylor
Trades: 13
Thank you very much.
borzoid is offline
Reply With Quote
View Public Profile
 
Old 06-22-2010, 01:40 PM Re: Syntax help needed
Super Talker

Posts: 144
Name: Nick Mallare
Location: Olathe, KS
Trades: 0
Quote:
Originally Posted by borzoid View Post
Hi, I have an application that I need to make changes to but have not been able to find the code who wrote it for a long time. It is a small change but I don't know the correct syntax.

the application has a form for adding animals to a database. On of the elements on the form is "Gender" and has radio buttons for male and female and enters the values into the db as "m" or "f" with male being selected by default. I have edited the form to give a third option "w" (for wether - a neutered male).

The save function has the following line of code in it for the gender:

PHP Code:
$gender = ( $gender == "male" ) ? "m" "f"
How do I change that to include the "w" choice?

Thanks in advance for any help you can give.
I am going to take a moment to teach.

The statement that you gave is called a ternary operator. It is a shortened form of an if-else statement and is used to reduce the number of lines from four to one. As with any if-else statement you only have two options, hence with a three-option statement you cannot use the ternary operator.

The switch-statement is a very good solution; however, an alternate solution is an if-elseif-else statement.

PHP Code:
if( $a == )
    echo 
'$a is equal to 1.';
elseif( 
$a == )
    echo 
'$a is equal to 2.';
else
    echo 
'$a is not equal to 1 or two.'
Nick
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
nmallare is offline
Reply With Quote
View Public Profile Visit nmallare's homepage!
 
Reply     « Reply to Syntax help needed
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33593 seconds with 12 queries