【问题标题】:If elseif elseif else logic with html & php code the elseif conditon are not executingIf elseif elseif else logic with html & php code elseif条件没有执行
【发布时间】:2023-04-09 04:43:02
【问题描述】:

我有一些情况下的代码要先检查,然后代码应该根据它执行。我的代码有

<?php if($this->Session->read('Auth.User.id')==''){ ?>
  <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Start now" onclick="javascript:register_popup();">
  <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Start now" onclick="javascript:submitPlan(2);">
  <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Start now" onclick="javascript:submitPlan(3);">
<?php } elseif($this->Session->read('Auth.User.id')!='' && $this->Session->read('Auth.User.role_id')=='3' && isset($this->data['User']['plan_type']) && $this->data['User']['plan_type']=='A'){ ?>
   <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Your plan" onclick="javascript:register_popup();">
   <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Start now" onclick="javascript:submitPlan(2);">
   <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Start now" onclick="javascript:submitPlan(3);">
<?php } elseif($this->Session->read('Auth.User.id')!='' && $this->Session->read('Auth.User.role_id')=='3' && $this->data['User']['plan_type']=='B'){ ?>
   <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Start now" onclick="javascript:register_popup();">
   <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Your plan " onclick="javascript:submitPlan(2);">
   <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Start now" onclick="javascript:submitPlan(3);">
<?php } else{?>  
   <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Start now" onclick="javascript:register_popup();">
   <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Start now" onclick="javascript:submitPlan(2);">
   <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Your plan" onclick="javascript:submitPlan(3);">
<?php } ?>

所以我在每种情况下都有 3 个按钮,因此根据用户已经计划,我想将按钮值从现在开始更改为您的计划和休息 2 按钮应该有立即开始选项。所以在这段代码中发生了什么,它检查条件是否不匹配然后直接执行 else 而不检查我想首先检查的 else if 部分。

【问题讨论】:

  • var_dump($this-&gt;data['User']['plan_type']);你看到了什么?
  • 就是这么说的:您可以在此之前在一些代码中设置按钮文本,并且您不会只有一组按钮,例如&lt;input type="button" value="&lt;?= $button_text['A'] ?&gt;" onclick="register_popup()"&gt; 等。(顺便说一句,那些内联样式作为 CSS 中的 #containerId input.profilebtn 规则可能会更好。而且您不需要在 onclick 属性中使用 javascript:。)

标签: php html cakephp-1.3


【解决方案1】:

只有当值满足所有条件时,elseifs 才会运行。如果它们没有执行,那么至少有一个条件没有得到满足。

例如,你能肯定地说这里的变量:

$this->Session->read('Auth.User.id')!=''
&& $this->Session->read('Auth.User.role_id')=='3'
&& $this->data['User']['plan_type']=='B')

一切都如你所愿?

【讨论】:

    【解决方案2】:

    尝试以 (true) 为条件创建另一个 elseif。因此,您可以检查 elseif 是否可以正常工作。 如果它跳进去,那么你的两个条件都有问题。

    用类似的东西手动检查你的变量

    var_dump($this->Session->read('Auth.User.role_id')=='3');
    

    并检查输出是否符合您在条件下的计划。

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 2010-10-12
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多