dougu4448 2016-12-10 10:54
浏览 30

嵌套的替代if语句在视图中混合php和html时不起作用[重复]

This question already has an answer here:

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.

</div>
  • 写回答

1条回答 默认 最新

  • douchang8758 2016-12-10 11:04
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件