?
Solved

php mysql if then statement syntax

Posted on 2016-10-21
4
Medium Priority
?
55 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 58

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 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