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' : '');
Related
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.
I'm using include for the header
and it's have this
<li><span>Arrangements</span></li>
<li><span>Build-a-Bouquet</span></li>
<li><span>Special Events</span></li>
and I want only any current opened page get the class="current"
like this
<li class="current"><span>Build-a-Bouquet</span></li>
You can do it like this:
<?php
$menus = array();
$menus['Arrangements'] = 'arrangements.php';
$menus['Build-a-Bouquet'] = 'bouquet.php';
$menus['Special Events'] = 'events.php';
?>
<ul>
<?php
$currnet_page = $_SERVER['PHP_SELF'];
if (! empty($menus)) {
foreach ($menus as $menu => $menu_link) {
$isCurrent = ($currnet_page == $menu_link) ? 'current' : '';
?>
<li class="<?php echo $isCurrent;?>"><span><?php echo $menu;?</span></li>
<?php
}
}
?>
</ul>
Try something like this:
$active = "bouquet";
include("navigation.php");
Navigation:
<li<?php if( isset($active) && $active == "arrangements") echo " class='active'";?>><span>Arrangements</span></li>
... And so on.
This is just a quick example, $_SERVER['PHP_SELF'] can do the job.
<li><span>Arrangements</span></li>
<?php
if($_SERVER['PHP_SELF'] == '/bouquet.php'){
echo '<li class="current"><span>Build-a-Bouquet</span></li>';
}else{
echo '<li><span>Build-a-Bouquet</span></li>';
}
?>
<li><span>Special Events</span></li>
There are a few ways you can do it. Choose the one you like:
1)
Set $current_ var you want and use:
<li><a<? echo isset($current_arrangements) ? 'class="current"' : ''; ?>href="arrangements.php"><span>Arrangements</span></a></li>
<li><a<? echo isset($current_bouquet) ? 'class="current"' : ''; ?>href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a<? echo isset($current_special_events) ? 'class="current"' : ''; ?>href="events.php"><span>Special Events</span></a></li>
2) same as above but use one variable and check for its value (exactly the way Niet the Dark Absol wrote)
3) wrap it to some function
<?
// Echo Class If Current
function ecic($page) {
echo eregi($page, $_SERVER['PHP_SELF']) ? 'class="current"' : '';
}
?>
<li><a <? ecic('arrangements'); ?>href="arrangements.php"><span>Arrangements</span></a></li>
<li><a <? ecic('bouquet'); ?>href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a <? ecic('events'); ?>href="events.php"><span>Special Events</span></a></li>
BTW: In most common PHP configurations you can use just <? instead of <?php.
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.
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>";
I've setup a menu for a fairly simple site based on icant.co.uk. It's fairly simple with maybe 5 pages. The small site is mainly a mysql browser for a few tables using MATE. Theres a common.php file that contains the header & footer HTML so thats where I put the code below.
The code below highlights the current page on the menu. Its ugly and I'm sure there has to be a better way to do it.
Any help is appreciated, thank you!
heres my code
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
if ($currentFile == "orders.php"){
echo '<li id="active">Orders</li>';
}
else{
echo '<li>Orders</li>';
}
if ($currentFile == "customers.php"){
echo '<li id="active">Customer List</li>';
}
else{
echo '<li>Customer List</li>';
}
if ($currentFile == "order_details.php"){
echo '<li id="active">Order Details</li>';
}
else{
echo '<li>Order Details</li>';
}
?>
UPDATE For those curious, below is the working code!
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
// easier to manage in case you want more pages later
$pages = array(
array("file" => "orders.php", "title" => "Orders"),
array("file" => "order_details.php", "title" => "Order Details"),
array("file" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$currentAppend = ($page['file'] == $currentFile) ? ' id="current' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '' . $page['title'] .''
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
?>
What I normally do is something like (for all elements...):
<li class="<?php if (condition) echo 'selected'; ?>">content part, links, etc.</li>
Not sure if that's what you meant, but this way you'll get rid of this ugly if-else:
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
// easier to manage in case you want more pages later
$pages = array(
array("file" => "orders.php", "title" => "Orders"),
array("file" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '' . $page['title'] .''
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
A more concise way of doing it (if you have short tags enabled) would be:
<li class="<?= $test=="your_page_name" ? 'selected' : 'not_selected'?>">Link Name</li>
It performs the same function as the first answer, just more concisely.
Here's a snippet from a project of mine. It it old ugly code, and uses tables, but you can just as easily use the idea for divs and cleaner markup. The trick is to make the navigation use a different class if the current page matches it's url.
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_home.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_home.php'>Billing Home</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_schedules.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_schedules.php'>Billing Schedules</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_outstanding.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_outstanding.php'>Outstanding</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_list.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_list.php'>List All</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_history.php'>Billing History</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_statement_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_statement_history.php'>Statement History</a></td></tr>