-3

I have no problem with creating php submenu class selected but when i try to create this submenu inside submenu: I can't get it work.

Problem: Defining echo inside echo returning syntax error because of quotes.

<ul class="sub_nav">
  <li <?php if ($page=='kurumsal-hakkimizda') {echo "class='selected'";} ?>>
    <a href="<?=BAZ_URL?>/tr/kurumsal-hakkimizda">Hakkımızda</a>
  </li>
  <li <?php if ($page=='kurumsal-ik') {echo "class='selected'";} ?>>
    <a href="<?=BAZ_URL?>/tr/kurumsal-ik">İnsan Kaynakları</a>
    <?php
      if ($page=='kurumsal-ik')
      { echo '
        <ul id="sub_sub_nav">

           <!-- !! PROBLEM STARTS HERE !! -->
           <li class="'if($page=='kurumsal-ik'){echo 'selected'}'">
           <!-- !! CANT USE ECHO INSIDE ECHO BEACUSE OF QUOTES !! -->

            <a href="'.BAZ_URL.'/tr/kurumsal-ik">İnsan Kaynakları Politikamız</a>
           </li>

           <li class="'if($page=='kurumsal-hedef'){echo 'selected'}'">
            <a href="'.BAZ_URL.'/tr/kurumsal-hedef">Kurumsal Hedef</a>
           </li>
        </ul>
     ';}
    ?>
  </li>
  <li <?php if ($page=='kurumsal-haberler') {echo "class='selected'";} ?>>
    <a href="<?=BAZ_URL?>/tr/kurumsal-haberler">Kurumsal Haberler</a>
  </li>
</ul>
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Barlas Apaydin
  • 6,794
  • 10
  • 50
  • 81

4 Answers4

2

Its already inside echo right? So do this:

<li class="', ($page=='kurumsal-ik') ? 'selected' : '', '">

It works for sure. It is called ternary operator! :)

Note: Dot concatenation operator cannot be used here, because, the ternary operator acts as a function returning a value. Only comma , can be used.

Praveen Kumar Purushothaman
  • 154,660
  • 22
  • 177
  • 226
0

You could just make the variable and echo it out at the very end:

<?php
  if ($page=='kurumsal-ik')
  { 
        $myVar= '
    <ul id="sub_sub_nav">
       <li class="';
    if($page=='kurumsal-ik')
    {
        $myVar .= 'selected';
    }
    $myVar.='">
        <a href="'.BAZ_URL.'/tr/kurumsal-ik">İnsan Kaynakları Politikamız</a>
       </li>

       <li class="';
    if($page=='kurumsal-hedef')
    {
        $myVar.= 'selected';
    }
    $myVar.='">
        <a href="'.BAZ_URL.'/tr/kurumsal-hedef">Kurumsal Hedef</a>
       </li>
    </ul>
    ';
    echo $myVar;
   }
?>
Fluffeh
  • 31,925
  • 16
  • 62
  • 77
0

As Praveen has said, it's already within an echo so there is no need to use another. I believe the syntax for a ternary operator within an echo is:

<?php echo '<li class="' . ( $page == 'page_name' ? 'selected' : '' ) . '"><a href="/">Link</a></li>'; ?>

Hope that helps.

James
  • 482
  • 2
  • 14
0

One the useful features of PHP is that it directly outputs everything that is outside the <?php and ?> tags, so you don't need long echo statements.

This is particularly practical when used in combination with the alternative syntax for control structures

Your code would be much more readable like this:

<?php if($page=='kurumsal-ik'): ?>
    <ul id="sub_sub_nav">

    <li class="<?php if($page=='kurumsal-ik') echo 'selected';?>">
        <a href="<?php echo BAZ_URL; ?>/tr/kurumsal-ik">İnsan Kaynakları Politikamız</a>
    </li>

    <li class="<?php if($page=='kurumsal-hedef') echo 'selected';?>">
        <a href="<?php echo BAZ_URL; ?>/tr/kurumsal-hedef">Kurumsal Hedef</a>
    </li>

    </ul>
<?php endif; ?>

P. S. You can shorten your code by using the shorthand <?=, which means <?php echo.

lortabac
  • 579
  • 4
  • 16