Solved

php mysql if then statement syntax

Posted on 2016-10-21
4
51 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 57

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

688 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