PHP和单选按钮

时间:2016-03-23 20:37:59

标签: php sql database if-statement conditional

有更好的方法或更短的方法吗?我试图根据从数据库字段中检索的数据来检查单选按钮?

<?php

          $ck2 = $objResult["MOV"];

          if ($ck2 == "Y") {

          echo "<input id='x1' type='radio' name='movie' value='Y' checked='checked'><label for=''x1'>Yes</label>";
          echo "</div>";
          echo "<div>";
          echo "<input id='x2' type='radio' name='movie' value='N'><label for=''x2'>No</label>";

          } elseif ($ck2 == "N") {

          echo "<input id='x1' type='radio' name='movie' value='Y'><label for=''x1'>Yes</label>";
          echo "</div>";
          echo "<div>";
          echo "<input id='x2' type='radio' name='movie' value='N' checked='checked'><label for=''x2'>No</label>";

          } else {

          echo "<input id='x1' type='radio' name='movie' value='Y'><label for=''x1'>Yes</label>";
          echo "</div>";
          echo "<div>";
          echo "<input id='x2' type='radio' name='movie' value='N'><label for=''x2'>No</label>";

          }

          ?>

6 个答案:

答案 0 :(得分:0)

 $ck2 = $objResult["MOV"];
 $checked_y = ($ck2 == "Y") ? "checked=\"checked\"" : "";
 $checked_n = ($ck2 == "N") ? "checked=\"checked\"" : "";

 echo "<input id='x1' type='radio' name='movie' value='Y' {$checked_y} /><label for=''x1'>Yes</label>";
 echo "</div>";
 echo "<div>";
 echo "<input id='x2' type='radio' name='movie' value='N' {$checked_n} /><label for=''x2'>No</label>";

答案 1 :(得分:0)

<?php

    echo '<input id="x1" type="radio" name="movie" value="Y"' . ($ck2 == 'Y' ? ' checked="checked"' : '') . '><label for="x1">Yes</label>';
    echo '<input id="x2" type="radio" name="movie" value="N"' . ($ck2 == 'N' ? ' checked="checked"' : '') . '><label for="x2">No</label>';

如果$objResult["MOV"]为空,因为它不在结果集中,则单选按钮将显示为未选中。相反,如果你想要其中一个被选中,如果它是null,你可以这样做:

<?php
    $ck2 = !empty($objResult["MOV"]) ? $objResult["MOV"] : 'N';

然后,&#34; no&#34;如果复选框为空,则会选中该复选框。

总之,它看起来像这样:

<?php

    $ck2 = !empty($objResult["MOV"]) ? $objResult["MOV"] : 'N';

    echo '<input id="x1" type="radio" name="movie" value="Y"' . ($ck2 == 'Y' ? ' checked="checked"' : '') . '><label for="x1">Yes</label>';
    echo '<input id="x2" type="radio" name="movie" value="N"' . ($ck2 == 'N' ? ' checked="checked"' : '') . '><label for="x2">No</label>';

显示单选按钮的功能也可以正常工作(如另一个人建议的那样)。为您的视图使用模板引擎取代了对此的需求,因此学习ternary operatorPHP Doc)也会对此有所帮助。

答案 2 :(得分:0)

<?php

$ck2 = $objResult["MOV"];

echo "<input id='x1' type='radio' name='movie' value='Y'". ($ck2 == "Y" ? " checked='checked'" : "") ."><label for=''x1'>Yes</label>";
echo "</div>";
echo "<div>";
echo "<input id='x2' type='radio' name='movie' value='N'". ($ck2 == "N" ? " checked='checked'" : "") ."><label for=''x2'>No</label>";

?>

详情:https://davidwalsh.name/php-ternary-examples

答案 3 :(得分:0)

同样的,有点不同的方法也更快:

<?php
$ck2 = $objResult["MOV"];
$ycheck= "";
$ncheck= "";
switch($ck2)
{
    case("Y"):
    {
        $ycheck= " checked='checked'";
        break;
    }
    case("N"):
    {
        $ncheck= " checked='checked'";
        break;
    }
    default:
    {
        break;
    }
}
?>
<input id='x1' type='radio' name='movie' value='Y'<?php echo $ycheck; ?>><label for=''x1'>Yes</label></div><div><input id='x2' type='radio' name='movie' value='N'<?php echo $ncheck; ?>><label for=''x2'>No</label>

答案 4 :(得分:0)

     <?php

          $ck2 = $objResult["MOV"];
          $isYes = ($ck2["MOV"] == "Y");
          $isNo = ($ck2["MOV"] == "N");

      ?>

      <input id="x1" type="radio" name="movie" value="Y" <? php if($isYes) echo "checked='checked'"; ?>><label for=''x1'>Yes</label>
      </div>
      <div>
      <input id="x2" type="radio" name="movie" value="N"<? php if($isNo) echo "checked='checked'"; ?>><label for=''x2'>No</label>

答案 5 :(得分:0)

最好使用该函数生成输入:

<?php
function radio_input($checked, $id)
{
    $checked_str = $checked ? " checked='checked' " : "" ;
    $label_str = $checked ? "Yes" : "No";
    $value_str = $checked ? "Y" : "N";
    return "<input id='$id' type='radio' name='movie' $checked_str value='$value_str'><label for='$id'>$label_str</label>";
}

$ck2 = $objResult["MOV"];

echo radio_input($ck2 == "Y","x1");
echo "</div>";
echo "<div>";
echo radio_input($ck2 == "Y","x2");
?>