if statement to verify page id - php

I'm trying to add a class="active" statement to a dynamic li element based on if it's meets a requirement of a pageID being equal to a variable named thisPage but I have the syntax messed up. My problem is how do I add my if statement into the middle of a string? I think I have the spirit captured below....
Thanks
<? ...........
$menu .= "\t<li" . ' if $thisPage=={$pageID} echo class="active"' . ">{$pval['menuTitle']}</li>\n";
...........
?>

You can use a ternary
$menu .= '<li'.($thisPage==$pageID?' class="active"':'').'></li>';

Try this
$menu .= "\t<li".($thisPage==$pageID ? ' class="active"' : '').'>'.$pval['menuTitle'].'</li>'."\n";
Though I'd recommend using sprintf to make what's going on clearer
$menu .= sprintf('\t<li%s>%s</li>',
($thisPage==$pageID ? ' class="active"' : ''),
$pval['link'],
$pval['menuTitle']);

easiest way would be to use a ternary operator like this:
$menu .= "\t<li" . $thisPage=={$pageID} ? 'class="active"' : '' . ">{$pval['menuTitle']}</li>\n";

Related

inserting a variable within another variable string that contains an if statement

I have a string as follows and store it in the variable:
$div = '<div class="posts"> . $row['author'] .'<?php if('.$perm.'>1):?><br>  '.$role.'</br>  Edit<?php endif ?></div>
What I want is to use this variable in another page so I need to insert the if statement to regulate permission so the content only displays with those users with above 1. It comes up with no syntax errors but when I run it this happens
John1):?>
Moderator
Edit
so the 1):?> is being displayed when it shouldn't.
Any reasons why? or how I could do this better?
Thanks.
Can you give an try like this:
$div ='<div class="posts">'.$row['author'].''; ?> <?php if($perm>1) {'<br>  '.$role.'</br>  Edit';} ''
I think its badly formated with single quotes ' , please check and lets see if it is.
Try conditional operator like this. The div section is kept at both statements just to improve readability.
$div = if('.$perm.'>1) ? ('<div class="posts"> . $row['author'] . <br>  '.$role.'</br>  Edit<div>') : ('<div class="posts"> . $row['author'] .'<div>');
Or:
$div = '<div class="posts"> . $row['author'] .';
$div += if('.$perm.'>1) ? '<br>  '.$role.'</br>  Edit<div>' : '<div>';

How can I decide css class name dynamically in PHP ?

I want to decide a css class dynamically using ternary operator in PHP,I'm using following code but its some error
echo '<div '.count($photos) > 1 ? "class='slider'" : "class='image'".'>';
what's wrong in above code ?
You need parentheses for the expression.
echo '<div ' . (count($photos) > 1 ? "class='slider'" : "class='image'") . '>';
Or
echo '<div class="' . (count($photos) > 1 ? 'slider' : 'image') . '">'

Insert function name with php

I'm trying to do a simple task, insert a second function name under "onmouseover" but probably something is wrong with the syntax.
echo '<div onmouseover="changeText(); <?php if($title==""){echo changeViz();}; ?> ">';
I probably need to escape some quotes and add some, but i can't figure out the correct solution.
Thanks
Nothing it's working. Let me give you the complete code...:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); <?php if($title[$i]==""){echo changeViz();}; ?>">';
You are nesting <?php ?> inside existing php code, which is a syntax error. Instead, concatenate in the javascript function changeViz() as a quoted string.
This version uses a ternary operator to duplicate the if() statement you had originally.
echo '<div onmouseover="changeText(); ' . ($title == '' ? 'changeViz();' : '') . '">';
The ternary operation here will concatenate changeViz(); onto the echo string if $title == "", or otherwise just concatenate on an empty string.
Update after seeing full code:
You have the quote escaping correct in the first part.
echo '<div class="frame" onmouseover="changeText(\'' . $text[$i] . '\'); ' . ($title == '' ? 'changeViz();' : '') . '">';
You can make your code much more readable if you do not try to do everything in one line:
$onmouseover_action = "changeText(); ";
if($title==""){
$onmouseover_action .= "changeViz(); ";
}
echo '<div onmouseover="'.$onmouseover_action.'">';
It makes your code easier to maintain, and you have less need to comment it, because it describes itself quite much better.
Try this:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); '. ($title[$i]=="")?'changeViz();':'').'">';

what is the proper syntax for php if statement escaped inside a link

i want to change the link address depending on whether the value matches a certain value. but i am returning a syntax error. what do i need to change? the error that i am getting is syntax error, unexpected T_IF
echo "<a href='".if($type=='group'){echo'link_a';}else{echo 'link_b';}."'>".$usaname."</a>
try this
$link = ($type=='group') ? 'link_a' : 'link_b';
echo "<a href='".$link."'>".$usaname."</a>
You can't use an if statement inside a hyperlink or any text that you are printing.
Instead you should have your code like this, keeping conditional (and other) functions outside where html is printed.
if ($type == "group"){ $link = "link_a"; } else { $link = "link_b"; }
echo "<a href='$link'>$usaname</a>
echo "<a href='". $type=='group' ? 'link_a' : 'link_b';."'>".$usaname."</a>
Ternary operators are handy in this case.
echo "<a href='" . ($type == 'group' ? 'link_a' : 'link_b') . "'>" . $usaname . "</a>";
you should do something like this:
echo "
That is probably the shortest and cleanest way i can find.
Use the ternary operator
echo "<a href='". ($type == 'group' ? 'link_a' : 'link_b') ."'>" . $username ."</a>";
Try a ternary...
echo '', htmlspecialchars($usaname), '';
That should simplify things a bit. Also, it's good to use htmlspecialchars() so that you have valid HTML.

Echo with Shorthand If not behaving properly

I'm programming in PHP, I'm using a 'shorthand if' to echo some HTML code on to the page, but it is behaving in odd ways.
echo '<div id="filter_bar">
<ul>';
echo '<li>Trending</li>' : '>Trending</a></li>';
echo '<li>Most Picked</li>' : '>Most Picked</a></li>';
echo '<li>Newest</li>' : '>Newest</a></li>';
echo '</ul></div>';
The resulting code that I get as a result is this
class="filter_selected">Trending</a></li> class="filter_selected">Most Picked</a></li> class="filter_selected">Newest</a></li>
As you can see, the opening list tags are not showing... but they do if I replace the first period '.' on each line with a ',' comma.
So this works, with the commas
Should I be using a comma here? Everywhere online sheems to show a period '.'
The reason is:
echo '<li>' . true ? 'aaa' : 'bbb'; will give you aaa,
because it is same with '<li>1' ? 'aaa' : 'bbb'
And you should do like this:
echo '<li>' . (true ? 'aaa' : 'bbb');
Maybe you can make your life a bit easier:
echo '<div id="filter_bar"><ul>',
'<li>' : '>', 'Trending</li>',
'<li>' : '>', 'Most Picked</li>',
'<li>' : '>', 'Newest</li>',
'</ul></div>';
Which will use commas (no need to repeat echo that much) and you don't need to repeat strings that much if you only want to insert the class attribute.
Then you made use of parenthesis where they were not needed (see Operator Precedence Docs) but at the place where those are needed, you didn't use them (the last case).
Additionally it's better to fill those values to variables upfront, so you can debug stuff easier (and you don't mix $_GET with output, so to prevent mixing output logic with input variables).
That done you could have learned that your issue is not within the echo but just the expression you've formulated with the ternary operator.
$class_trending = $_GET['select'] == "trending" ? ' class="filter_selected"' : '';
$class_most_picked = $_GET['select'] == "most_picked" ? ' class="filter_selected"' : '';
$class_newest = ($_GET['select'] == "newest") || empty($_GET['select']) ? ' class="filter_selected"' : '';
echo '<div id="filter_bar"><ul>',
'<li><a href="?select=trending"', $class_trending, '>Trending</a></li>',
'<li><a href="?select=most_picked"', $class_most_picked, '>Most Picked</a></li>',
'<li><a href="?select=newest"',$class_newest , '>Newest</a></li>',
'</ul></div>';
dunno your problem but this code looks an ugly mess for me.
I'd make it this way:
in the PHP code I'd prepare variables first.
$sections = array(
'newest' => 'Newest',
'trending' => 'Trending',
'most_picked' => 'Most Picked',
);
if (empty($_GET['select']) OR !$choice = array_search($sections,$_GET['select'])) {
$choice = 'newest';
}
and then in the template run smooth and short loop:
<div id="filter_bar">
<ul>
<? foreach ($sections as $sect => $name): ?>
<li>
<a href="?select=<?=$sect?><? if ($choice == $sect) ?>" class="filter_selected"<? endif ?>><?=$name?></a>
</li>
<? endforeach ?>
</ul>
</div>
One possible solution is
echo "<li><a href='?select=newest'";
echo ($_GET['select'] == "newest" || empty($_GET['select'])) ? ' class="filter_selected">Newest</a></li>' : '>Newest</a></li>';
Change your parentheses to the following:
echo '<div id="filter_bar">
<ul>';
echo '<li>Trending</li>' : '>Trending</a></li>');
echo '<li>Most Picked</li>' : '>Most Picked</a></li>');
echo '<li>Newest</li>' : '>Newest</a></li>');
echo '</ul></div>';
If you do not do this, PHP does not know what exactly your condition is. Also have a look at operator precendence as this explains why it works using commas.
By the way: The ?: is called ternary operator.

Categories