drtzb06222 2014-05-28 15:46
浏览 29
已采纳

如果OR不工作

I have a statement set up to determine whether drupal blocks are present, and give a class depending on how many blocks are available.

<?php if($page['sidebar_first'] || $page['sidebar_second']) { 
      $contentwid= "eleven"; 
 } else { 
      $contentwid= "sixteen"; 
 } ?>

<?php if($page['sidebar_first'] && $page['sidebar_second']) { 
      $contentwid= "seventeen"; 
 } else { 
      $contentwid= "sixteen"; 
 } ?>

<div id="content" class="<?php print $contentwid; ?> columns">
</div>

The second statement works fine, If there is a block in the first sidebar AND a block in the second sidebar, then the main content has a class of seventeen.

The problem is with the first statement for OR ( || ). It doesn't matter if there is a block in either sidebar, the content is always given a class of sixteen, which is full width, and pushes the sidebar content to the bottom.

I know its detecting sidebar blocks correctly because the second statement works perfectly. I have set up everything else correctly, and have traced it back to this OR statement, which always seems to result in FALSE (sixteen).

I've tried || and OR, but as far as I know, they are no different, just user preference right?

I'm stumped.

  • 写回答

1条回答 默认 最新

  • dongwen6743 2014-05-28 15:50
    关注

    It's executing the first if (the or) and setting $contentwid correctly, but then it still executes the second if (the and) and failing, so executing the else for that, which resets $contentwid to "sixteen"

    <?php if($page['sidebar_first'] || $page['sidebar_second']) { 
          $contentwid= "eleven"; 
     } else { 
          $contentwid= "sixteen"; 
     } elseif($page['sidebar_first'] && $page['sidebar_second']) { 
          $contentwid= "seventeen"; 
     } else { 
          $contentwid= "sixteen"; 
     } ?>
    

    though you might want to reverse the "and" and "or" to prevent the and from failing

     <?php if($page['sidebar_first'] && $page['sidebar_second']) { 
          $contentwid= "seventeen"; 
     } elseif($page['sidebar_first'] || $page['sidebar_second']) { 
          $contentwid= "eleven"; 
     } else { 
          $contentwid= "sixteen"; 
     } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像