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 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错