Solved

php mysql if then statement syntax

Posted on 2016-10-21
4
25 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 51

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

23 Experts available now in Live!

Get 1:1 Help Now