How do I code an IF statment within a string? - php

I've tried every combination I could think of, but nothing is working. How do I integrate an IF statement within a string?
This is the line I need integrated;
if ($row['SL'] == 'Yes') {echo '<li class=\"sl\">Short Sale</li>' ;}
This is what I tried;
elseif ($row['X'] == 'sold') { echo "
<li id=\"price\"> $ {$row['SP$']}</li>
" .
{ if ($row['SL'] == 'Yes') {echo '<li class=\"sl\">Short Sale</li>' ;} }
. "
<li>Days on Market: {$row['DOM']}</li>;
}

Use a ternary operator:
elseif ($row['X'] == 'sold') { echo "
<li id=\"price\"> $ {$row['SP$']}</li>
" . ($row['SL'] == 'Yes') ? '<li class="sl">Short Sale</li>' : ''
. "
<li>Days on Market: {$row['DOM']}</li>";
}

You just need to break it up:
elseif ($row['X'] == 'sold') {
echo "<li id=\"price\"> $ {$row['SP$']}</li>";
if ($row['SL'] == 'Yes') {
echo '<li class=\"sl\">Short Sale</li>';
}
echo "<li>Days on Market: {$row['DOM']}</li>;
}

You don't. You output the first half the string, then fully stop that statement, then evaluate your if, then output the rest of the string.
...
elseif ($row['X'] == 'sold') {
echo "<li id=\"price\"> $ {$row['SP$']}</li>";
if ($row['SL'] == 'Yes') {
echo '<li class=\"sl\">Short Sale</li>'
}
echo "<li>Days on Market: {$row['DOM']}</li>";
}
You can't "embed" an if statement in a string, firstly because that's not syntactically valid, but even if it was, if statements don't resolve to a value in PHP.

What you are looking for is called Ternary Operator or Shorthand If/Else:
( condition ? true result : false result )
Example:
echo "<li id=\"price\">{$row['SP']}</li>" . ( $row['SL'] == 'Yes' ? '<li class="sl">Short Sale</li>' : '' ) . "<li>Days on Market: {$row['DOM']}</li>";
The above example is the same as:
echo "<li id=\"price\">{$row['SP']}</li>";
if ( $row['SL'] == 'Yes' ) echo '<li class="sl">Short Sale</li>';
else echo '';
echo "<li>Days on Market: {$row['DOM']}</li>";
You can "stack" ternary expressions so the complete example will look like:
echo
$row['X'] == 'sold'
? "<li id=\"price\">{$row['SP']}</li>" .
( $row['SL'] == 'Yes' ? '<li class="sl">Short Sale</li>' : '' ) .
"<li>Days on Market: {$row['DOM']}</li>"
: '';
important: you cannot build a ternary operator without else statement.

Related

If statement in foreach loop for printing an array [duplicate]

I want to concatenate in the middle of an echo to write an if statement, is this possible? Here is what I have.
echo "<li class='".if ($_GET["p"] == "home") { echo "active"; }."'><a href='#'>Home</a> </li>";
Like this, using the ternary operator:
echo "<li class='". (($_GET["p"] == "home") ? "active" : "") . "'><a href='#'>Home</a> </li>";
echo "<li class='".(($_GET["p"] == "home") ? "active" : "")."'><a href='#'>Home</a> </li>";
Do like this:
echo "<li class='".($_GET["p"] == "home" ? 'active' : '') ."'><a href='#'>Home</a> </li>";
Instead of messy inline concatenations, might I suggest getting cozy with printf()?
$format = '<li class="%s">Home </li>';
printf($format, ($_GET['p'] == 'home') ? 'active' : '');

How can i shorten this PHP code? (if - elseif)

I have this simple if else function and i wonder how i could shorten this?
<?php
if( $count_bananas == '1' )
{ echo '<div class="stage-columns-1">'; }
elseif ($count_bananas == '2')
{ echo '<div class="stage-columns-2">'; }
elseif ($count_bananas == '3')
{ echo '<div class="stage-columns-3">'; }
elseif ($count_bananas == '4')
{ echo '<div class="stage-columns-4">'; }
?>
The code works but i wonder if there is a way to code this shorter and more elegant?
You really didn't see the pattern?
echo '<div class="stage-columns-' . $count_bananas . '">';
It can be done without condition checks. For the code snippet you are posted, if the values are same then -
echo '<div class="stage-columns-' . $count_bananas . '">';

Active Page for CSS using PHP and MYSQL

I am trying to build a menu with an active page element for CSS using PHP and MySQL.
This example PHP/HTML hybrid works, and is what I'm trying to mimic.
<nav><ul>
<li<?php if ($thisPage == "1") echo " class=\"active\""; ?>>page 1</li>
<li<?php if ($thisPage == "2") echo " class=\"active\""; ?>>page 2</li>
<li<?php if ($thisPage == "3") echo " class=\"active\""; ?>>page 3</li>
</nav></ul>
I want to blend this statement to set the active class:
<?php if ($thisPage == $menuID) echo " class=\"active\""; ?>
Into this unordered list statement
<?php
echo "\n<nav>\n";
echo "<ul>\n";
while($row_rsMenu = mysql_fetch_assoc($rsMenu))
{
echo "<li>" . "" . $row_rsMenu['menuName'] . "</li>\n";
}
echo "</ul>\n";
echo "</nav>\n";
?>
This is where I am but I can't seem to get the syntax to work correctly.
echo "<li" . "if(" . $thisPage==$menuID . ")". echo ' class=\"active\"';" . ">" . "" . $row_rsMenu['menuName'] . "</li>\n";
If someone could help me to understand where this went wrong I'd appreciate it.
You have your if statement encapsulated in quotes. That means that PHP won't interpret it. Instead, it will print it out along with your HTML.
Something along the lines of this will work better:
echo "\n<nav>\n";
echo "<ul>\n";
while($row_rsMenu = mysql_fetch_assoc($rsMenu))
{
$active = ""; //not active by default
if($thisPage==$menuID){
$active = ' class="active" ';
}
echo "<li>" . "" . $row_rsMenu['menuName'] . "</li>\n";
}
echo "</ul>\n";
echo "</nav>\n";
By the way, where are you getting $menuID from?
Your error is wrapping the if block within quotes, among a few other issues with quotes.
Try this instead:
echo '<li', (($thisPage==$menuId) ? ' class="active"' : ''), '>', '', $row_rsMenu['menuName'], '</li>';
This uses a ternary operator to shorten things up a bit, in addition to fixing your syntax error.

CSS style if/else statement not responding

I am trying to have set the position of my li element according to the value of the variable that I will be displaying. For some reason this doesn't seem to be working. Does anyone see anything wrong with my code? Thanks
echo "<li class=\"compensation_post posts_values\" if($compensation == \"Free\") {style=\"left: 540px;\"}>$compensation</li>";
PHP can parse variables in a double-quoted string, but not conditions. So you're going to need to break this out in one of two ways:
Separated:
$style = '';
if ($compensation == "Free")
$style = " style=\"left:540px;\"";
echo "<li class=\"compensation_post posts_values\"$style>$compensation</li>";
Or in-line:
echo "<li class=\"compensation_post posts_values\" "
. ($compensation == "Free" ? "style=\"left:540px;" : "")
. ">$compensation</li>";
I suppose you can also go in and out of php code like:
?><li class=\"compensation_post posts_values\" <?= ($compensation == "Free" ? "style=\"left:540px;\"" : ""); ?>>$compensation</li>
You cannot use an if statement like that. Instead, you could use a shorthand if. Something like this should work:
echo "<li class=\"compensation_post posts_values\"" . (($compensation == 'Free') ? 'style=\"left: 540px;\" : '') . ">$compensation</li>";
Or, you could do like this:
if ($compensation == 'Free')
$style = 'style="left: 540px;"';
echo "<li class=\"compensation_post posts_values\" $style>$compensation</li>";

PHP conditional loop help

Hi there in my database I have 3 columns, is_contract, is_permenant and is_temporary. Within these columns there is either a Y or N value.
I am using these columns to echo onto the page what kind of work someone is looking for, my problem is that the user can be looking for more than one type of work, I am currently running 3 if statements to determine what to echo to the page, however I am struggling to add a comma if more than one of the statemnts returns as true, below is my code so far,
<?php
if($rslt['is_contract'] == 'Y') {
echo "Contract ";
}
if($rslt['is_permanent'] == 'Y') {
echo "Permanent ";
}
if($rslt['is_temporary'] == 'Y') {
echo "Temporary";
}
?>
<?php
$out=array();
if($rslt['is_contract'] == 'Y') $out[]="Contract";
if($rslt['is_permanent'] == 'Y') $out[]="Permanent";
if($rslt['is_temporary'] == 'Y') $out[]="Temporary";
echo implode(", ",$out);
?>
Either you can use like this in simple way
if($rslt['is_contract'] == 'Y') {
echo "Contract ";
}
if($rslt['is_permanent'] == 'Y') {
if($rslt['is_contract'] == 'Y') {
echo ", ";
}
echo "Permanent ";
if($rslt['is_temporary'] == 'Y') {
echo ", ";
}
}
if($rslt['is_temporary'] == 'Y') {
if($rslt['is_contract'] == 'Y' && $rslt['is_permanent'] != 'Y') {
echo ", ";
}
echo "Temporary";
}

Categories