Solved

php mysql if then statement syntax

Posted on 2016-10-21
4
35 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 108

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 108

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 52

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 dynamically set the form action using jQuery.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now