Solved

php mysql if then statement syntax

Posted on 2016-10-21
4
44 Views
Last Modified: 2016-10-21
How do I check in an echo statement if $data_type = NEW then $onemonthahead = NEWCONTRACT

echo "<td colspan='3' valign='top' align='center' class='paramText'><input class='rateTextbox' type='text' id='cont_strt' name='cont_strt' maxlength='10' size='10' 

autocomplete='off' required value=$onemonthahead'> (MMDDYY)</td>";

Open in new window

0
Comment
Question by:bschwarting
  • 2
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41853744
You don't usually do this in an echo statement.  The best way would be to write the conditional statements separately.  I'll see if I can give you an example...
1
 
LVL 1

Author Comment

by:bschwarting
ID: 41853745
true, i guess i can do before
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41853748
I haven't tested this, but it's the right way to write the code.  HTH, ~Ray
<?php // demo/temp_bschwarting.php
/**
 * https://www.experts-exchange.com/questions/28977968/php-mysql-if-then-statement-syntax.html
 */
ini_set('display_errors', TRUE);
error_reporting(E_ALL);


// SET THE DEFAULT VALUE
$onemonthahead = NULL;

// TEST A VARIABLE AND RESET THE DEFAULT AS NEEDED
if ($data_type == 'NEW')
{
    $onemonthahead = 'NEWCONTRACT';
}

echo "<td colspan='3' valign='top' align='center' class='paramText'><input class='rateTextbox' type='text' id='cont_strt' name='cont_strt' maxlength='10' size='10' 

autocomplete='off' required value='$onemonthahead'> (MMDDYY)</td>";

Open in new window

0
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 41853822
This is a good case for using a Ternary expression

$onemonthahead = $date_type == 'NEW' ? 'NEWCONTRACT' : '';

Open in new window


This is equivalent to
if ($data_type == 'NEW') {
  $onemonthahead='NEWCONTRACT';
}
else {
  $onemonthahead= '';
}

Open in new window

1

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question