0

I migrate my old app. On old server (php 5.2) I'm using nested alternative if statement to mix html and php code in view, like this:

<?php if($pg_id==1): ?>
    <li <?php if ($nav2=='news'): echo 'class="active"'; endif; ?>><a href="<?php echo base_url();?>cms/news/add">News</a></li>
<? endif; ?>

Why on the new server (php 5.5) the above example doesn't works? I'm getting error:

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\testapp\system\libraries\Loader.php(673) : eval()'d code on line 372

If I want this statement works, I need to rewrite it like this:

<?php if($pg_id==1):
    echo '<li';
    if ($nav2=='news')
        echo 'class="active"';
    echo '><a href="' . base_url() .'cms/news/add">News</a></li>';
endif; ?>

The app is written using Codeigniter 1.7.2.

davor
  • 939
  • 2
  • 14
  • 31
  • 2
    I suspect that: ` endif; ?>` should be: ``. i.e. php.ini has short open tags (` `) disabled. Note: short echo tags (`= `) will work fine in php 5.4+ – Ryan Vincent Dec 10 '16 at 12:24
  • Yes, you're right. What a newbie question. Shame on me. – davor Dec 10 '16 at 13:14

1 Answers1

0

Try it it will works... If not comment please

<?php if($pg_id==1):
    if ($nav2=='news'):?>
   <li class="active"><a href="<?php echo base_url('cms/news/add');?>">News</a> </li>
   <?php else: ?>
   <li class=""><a href="<?php echo base_url('your path'); ?>">Title</a></li>
 <?php endif;
  endif;
 ?>

Rather than using above code use the Ternary Operator. It will be much easy and understandable.

<?php  
$class= ($nav2=='news')?'active':'';
  if($pg_id==1): ?>
  <li class="<?php echo $class;?>"><a href="<?php echo base_url('cms/news/add');?>">News</a></li>
<?php endif; ?>

Try it.. will be 100% works.

Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
  • This code works, but this isn't properly rewritten. As you can see in my snippet,
  • is allways printed, and class="active" is printed when $nav2='news'. In your example, you print
  • only when $nav2='news'.
  • – davor Dec 10 '16 at 11:24
  • yes i understand problem clearly now. I am going to edit the code then try. – Hikmat Sijapati Dec 10 '16 at 11:27
  • if you not need any class just remove it. – Hikmat Sijapati Dec 10 '16 at 11:31
  • Ok, thanks, it works. But, my question is why my code (first block in my post) doesn't work on new server, while on the old it works. It would be greate if I can resolve this without rewritting my code. – davor Dec 10 '16 at 11:55
  • Yes this will work but if you want to reduce code then use ternary operator to define the value of class is whether active or not.Code is edited above just copy it and enjoy. Waiting for your response and up vote thank you... – Hikmat Sijapati Dec 11 '16 at 05:00