Is there a better way or shorter way to do this ? I am trying to check a radio button based on the data retrieved from a database field ??
<?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>";
}
?>
$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>";
Yes.
<?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>';
If $objResult["MOV"]
is null because it's not in the result set, the radio buttons will be displayed with neither checked. Instead, if you want one of them to be selected if it is null, you could do something like this:
<?php
$ck2 = !empty($objResult["MOV"]) ? $objResult["MOV"] : 'N';
Then, the "no" checkbox will be selected if it is null.
All together, it would look like this:
<?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>';
A function to display the radio button would work as well (as another person suggested). Using a template engine for your view kind of replaces the need for that, so learning the ternary operator (PHP Doc) will help with that as well.
<?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>";
?>
Same thing, a bit different approach and faster too:
<?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>
<?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>
It's better to use the function to generate input:
<?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");
?>