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

时间:2015-06-28 22:58:43

标签: php html

如果来自Json值的productNeedsAudit为True,我希望选中该复选框。但我刚开始使用php而且我也无法使用echo语句实现逻辑。我正在尝试以下方法:

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>";

在结果中,未选中复选框。相反,我在网络浏览器中获得以下内容。

> Needs Audit 

这是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 个答案:

答案 0 :(得分:1)

对三元运算符进行分组。

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>";

更长时间写三元运算符。 http://davidwalsh.name/php-shorthand-if-else-ternary-operators

功能范例:

<?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>";

输出:

<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>

正如:

<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>

注意我在checked属性中添加了一个空格。我认为某些浏览器也更喜欢checked="checked"

当你的表达式没有分组时,PHP正在检查“<tr><td><div class='control-group controls'><label for='productNeedsAudit'><input name='productNeedsAudit' type='checkbox'id='productNeedsAudit' value='True' + $soJson->{'productNeedsAudit'}是什么”等于True。因为它没有进入三元运算符的第二部分并输出:

'' . ">
                        Needs Audit
               </label> 
      </div></td></tr>"