Вопрос

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><a href="?select=trending"' . ($_GET['select'] == "trending") ? ' class="filter_selected">Trending</a></li>' : '>Trending</a></li>';
    echo '<li><a href="?select=most_picked"' . ($_GET['select'] == "most_picked") ? ' class="filter_selected">Most Picked</a></li>' : '>Most Picked</a></li>';
    echo '<li><a href="?select=newest"' . ($_GET['select'] == "newest" || empty($_GET['select'])) ? ' class="filter_selected">Newest</a></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 '.'

Это было полезно?

Решение

Maybe you can make your life a bit easier:

echo '<div id="filter_bar"><ul>',
    '<li><a href="?select=trending"', $_GET['select'] == "trending" ? ' class="filter_selected">' : '>', 'Trending</a></li>',
    '<li><a href="?select=most_picked"', $_GET['select'] == "most_picked" ? ' class="filter_selected">' : '>', 'Most Picked</a></li>',
    '<li><a href="?select=newest"', ($_GET['select'] == "newest") || empty($_GET['select']) ? ' class="filter_selected">' : '>', 'Newest</a></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>';

Другие советы

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');

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><a href="?select=trending"' . ($_GET['select'] == "trending" ? ' class="filter_selected">Trending</a></li>' : '>Trending</a></li>');
    echo '<li><a href="?select=most_picked"' . ($_GET['select'] == "most_picked" ? ' class="filter_selected">Most Picked</a></li>' : '>Most Picked</a></li>');
    echo '<li><a href="?select=newest"' . (($_GET['select'] == "newest" || empty($_GET['select'])) ? ' class="filter_selected">Newest</a></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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top