dsnm64969 2015-06-28 22:58
浏览 35
已采纳

如何在html标记中正确实现内联if语句?

I want to have the checkbox seleted if the productNeedsAudit from the Json value is True. But I new to php and I am also stuck implementing the logic with an echo statement. I am trying the following:

echo "<tr><td><div class='control-group controls'>
          <label for='productNeedsAudit'>
                    <input name='productNeedsAudit' type='checkbox'id='productNeedsAudit'  value='True'" . $soJson->{'productNeedsAudit'} == 'True' ? 'checked' : '' . ">
                        Needs Audit
               </label> 
      </div></td></tr>";

In the results, the check box isn't selected. Instead, I am getting the following in the web browser.

> Needs Audit 

Here's the HTML

<table>
   <tr>
      <td>
         <select style='width: 100%;' id='productType' name='productType'>
            <option value='TEST: TEST</option>
         </select>
      </td>
   </tr>
   <tr>
      <td>
         <div class='control-group controls'>
            <label for='productDescription'>Description: </label>
            <input type='text' name='productDescription' value='*.1stdibs.com, *.1stdibs.us.com' />
         </div>
      </td>
   </tr>
   <tr>
      <td>
         <div class='control-group controls'>
            <label for='productPriceBookDescription'>Price Book Description: </label>
            <input type='text' id='priceBookDesc' name='productPriceBookDescription' style='width:100%' value='' />
         </div>
      </td>
   </tr>
   />
   Needs Audit
   </label> 
   </div></td></tr>
   <tr>
      <td>
         <div class='control-group controls'>
            <label for='productServiceOrderNumber'>Service Order: </label>
            <select  id='productServiceOrderNumber' name='productServiceOrderNumber'>
               <option value='992769:53:14' selected='selected'>992769:53:14</option>
            </select>
         </div>
      </td>
   </tr>
   <tr>
      <td>
         <input id='subcustomers' name='subcustomers'>
         </input>
         <input id='subproducts' name='subproducts'>
         </input>
      </td>
   </tr>
   <tr>
      <td>
         <input id='tags' name='tags'>
         </input>
      </td>
   </tr>
   </td></tr>
</table>
  • 写回答

1条回答 默认 最新

  • doufuhao8085 2015-06-28 23:17
    关注

    Group the ternary operator.

    echo "<tr><td><div class='control-group controls'>
              <label for='productNeedsAudit'>
                        <input name='productNeedsAudit' type='checkbox'id='productNeedsAudit'  value='True'" . ($soJson->{'productNeedsAudit'} == 'True' ? ' checked' : '') . ">
                            Needs Audit
                   </label> 
          </div></td></tr>";
    

    Longer write up on ternary operator. http://davidwalsh.name/php-shorthand-if-else-ternary-operators

    Functional example:

    <?php
    $a = "False";
    echo "<tr><td><div class='control-group controls'>
              <label for='productNeedsAudit'>
                        <input name='productNeedsAudit' type='checkbox'id='productNeedsAudit'  value='True'" . ($a == 'True' ? ' checked' : '') . ">
                            Needs Audit
                   </label> 
          </div></td></tr>";
    

    Output:

    <tr><td><div class='control-group controls'>
              <label for='productNeedsAudit'>
                        <input name='productNeedsAudit' type='checkbox'id='productNeedsAudit'  value='True'>
                            Needs Audit
                   </label> 
          </div></td></tr>
    

    As true:

    <tr><td><div class='control-group controls'>
              <label for='productNeedsAudit'>
                        <input name='productNeedsAudit' type='checkbox'id='productNeedsAudit'  value='True'checked>
                            Needs Audit
                   </label> 
          </div></td></tr>
    

    Note I added a space in the checked attribute. I think some browsers also prefer that to be checked="checked".

    When your expression isn't grouped the PHP is checking does " <tr><td><div class='control-group controls'><label for='productNeedsAudit'><input name='productNeedsAudit' type='checkbox'id='productNeedsAudit' value='True' + what ever $soJson->{'productNeedsAudit'} is" equal True. Since it doesn't it goes to the second part of the ternary operator and outputs:

    '' . ">
                            Needs Audit
                   </label> 
          </div></td></tr>"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条