user1889007
user1889007

Reputation: 319

php if else check inside a string

Im trying to squeeze few html codes and php codes into a string. (And then pass the string back to ajax callback and ajax prints the string).

This is my code.

$sql = "SELECT * FROM pfilter WHERE id = '$getId'";
$result = mysql_query($sql);

while ($myrow = mysql_fetch_array($result)){

    $output_string =    '<table>';
    $output_string .=   '<tr><td>Make</td><td>'.$myrow['make'].'</td></tr>
                        <tr><td>Model</td><td>'.$myrow['model'].'</td></tr>
                        <tr><td>Series</td><td>'.$myrow['series'].'</td></tr>
                        <tr><td>Years</td><td>'.$myrow['years'].'</td></tr></table>';

But I want to check whether $myrow['make'] has any value, and if it does then print that whole tr. Otherwise it should skip that whole tr. Whats the best way to check this? I tried putting if else inside that string using '. .' but didnt suceed. Do I have to use $output_string .= again and again between ifelse? Im new to PHP, Im glad if you guys can help me out here.

Upvotes: 0

Views: 106

Answers (4)

Kevin
Kevin

Reputation: 6701

Yes! You can use the ternary operator inside a string. So, here it goes:

$output_string .=   (!empty($myrow['make'])?'<tr><td>Make</td><td>'.$myrow['make'].'</td></tr>
                       : '' )
                    <tr><td>Model</td><td>'.$myrow['model'].'</td></tr>
                    <tr><td>Series</td><td>'.$myrow['series'].'</td></tr>
                    <tr><td>Years</td><td>'.$myrow['years'].'</td></tr></table>';

Enclosing the condition by parentheses will do the trick.

Upvotes: 0

GJK
GJK

Reputation: 37389

Use the ternary operator:

'text' . (strpos($myrow['make'], 'foo') !== FALSE ? '<tr>...</tr>' : '') . 'more text'

Upvotes: 0

dkamins
dkamins

Reputation: 21918

The ".=" operator concatenates strings together.

This:

$output_string =    '<table>';
$output_string .=   '<tr><td>Make</td><td>'.$myrow['make'].'</td></tr>
                     <tr><td>Model</td><td>'.$myrow['model'].'</td></tr>
                     <tr><td>Series</td><td>'.$myrow['series'].'</td></tr>
                     <tr><td>Years</td><td>'.$myrow['years'].'</td></tr></table>';

is equivalent to this:

$output_string =    '<table>';
$output_string .=   '<tr><td>Make</td><td>'.$myrow['make'].'</td></tr>';
$output_string .=   '<tr><td>Model</td><td>'.$myrow['model'].'</td></tr>';
$output_string .=   '<tr><td>Series</td><td>'.$myrow['series'].'</td></tr>';
$output_string .=   '<tr><td>Years</td><td>'.$myrow['years'].'</td></tr></table>';

So put your logic like this:

$output_string =    '<table>';
if (@$myrow['make']) {
    $output_string .=   '<tr><td>Make</td><td>'.$myrow['make'].'</td></tr>';
}
$output_string .=   '<tr><td>Model</td><td>'.$myrow['model'].'</td></tr>';
$output_string .=   '<tr><td>Series</td><td>'.$myrow['series'].'</td></tr>';
$output_string .=   '<tr><td>Years</td><td>'.$myrow['years'].'</td></tr></table>';

Upvotes: 0

NebuSoft
NebuSoft

Reputation: 4000

You can combine length operations with the ternary operator.

$output_string .= (strlen($myrow['make']) > 0 ? '<tr><td>Make</td><td>'.$myrow['make'].'</td></tr>' : '');

You are simply checking if there is any length to the string, and if so returning the full line with the values. If not, returning an empty string. You can read more about the ternary operator here: http://php.net/manual/en/language.operators.comparison.php

Upvotes: 2

Related Questions