How to retrieve checkbox value from database in php? - php

this is my code
<tr><td valign="top">Sebab Kekosongan</td>
<?php
$QQQ = "SELECT sbbKekosongan FROM infojawatan WHERE ID = '$ID'";
$rs_QQQ = mysql_query($QQQ);
while($row_QQQ = mysql_fetch_array($rs_QQQ))
{
$kerana = explode(",", $row_QQQ['kerana']); ?>
<td>
<input type="checkbox" name="kerana[]" value="retire" <?php if(in_array("retire",$kerana)) echo 'checked="checked"' ?>> Bersara
> Meninggal Dunia
## continue coding ##
> Bertukar
<input type="checkbox" name="kerana[]" value="promote" <?php if(in_array("promote",$kerana)) echo 'checked="checked"' ?>> Naik Pangkat
<input type="checkbox" name="kerana[]" value="others" <?php if(in_array("others",$kerana)) echo 'checked="checked"' ?>> Lain -lain
</td> <?php } ?> </tr>
my second test code
<tr><td valign="top">Sebab Kekosongan</td><td>
<?php
$keranaS = array('retire', 'death', 'change', 'promote', 'others');
if(! empty($keranaS))
{
foreach ($keranaS as $myKerana)
{
$checked = (in_array($myKerana, $kerana)) ? 'checked="checked"' : ''; ?>
<input type="checkbox" name="kerana[]" value="<?php echo $myKerana; ?>" <?php echo $checked; ?>> <?php echo $myKerana;?>
<?php } ?>
</td>
<?php } ?>
</tr>
i have tried both., but its not working

# Please Try again below code.. #
<tr><td valign="top">Sebab Kekosongan</td><td>
<?php
$keranaS = array('retire', 'death', 'change', 'promote', 'others');
//echo '<pre>'; print_r($keranaS); exit;
if(!empty($keranaS))
{
foreach ($keranaS as $myKerana)
{
$checked = (in_array($myKerana, $keranaS)) ? 'checked="checked"' : ''; ?>
<input type="checkbox" name="kerana[]" value="<?php echo $myKerana; ?>" <?php echo $checked; ?>> <?php echo $myKerana;?>
<?php } ?>
</td>
<?php } ?>
</tr>
# Please try again below dynamic through checkbox checked.
<tr><td valign="top">Sebab Kekosongan</td><td>
<?php
$keranaS = array('retire', 'death', 'change', 'promote', 'others');
//$kerana_data data databse though fetch
$kerana_data = array('retire', 'change', 'others');
if(!empty($keranaS))
{
$i = 0;
foreach ($keranaS as $myKerana)
{
if(in_array($myKerana, $kerana_data)){
$checked = (in_array($myKerana, $kerana_data)) ? 'checked="checked"' : ''; ?>
<input type="checkbox" name="kerana[]" value="<?php echo $myKerana; ?>" <?php echo $checked; ?>> <?php echo $myKerana;?>
<?php }else{?>
<input type="checkbox" name="kerana[]" value="<?php echo $myKerana; ?>"> <?php echo $myKerana;?>
<?php }?>
<?php $i++;
} ?>
</td>
<?php } ?>
</tr>

Related

Mark radio using unserialize inside foreach

I would like to know how the correct way is to get the checkbox checked.
Currently looping inside loop ...
In the first foreach it lists all the options to be marked, as in the image below:
In the second foreach it would be to mark the option that is coming from the DB. It checks with this code <?php if($checkIni[$key] == "tem"){echo "checked ";}; ?>. What changes is the value that is TEM, NAO, AVA.
<?php
$checklistIni = "a:16:{i:0;s:3:"nao";i:1;s:3:"ava";i:2;s:3:"tem";i:3;s:3:"tem";i:4;s:3:"tem";i:5;s:3:"tem";i:6;s:3:"tem";i:7;s:3:"tem";i:8;s:3:"tem";i:9;s:3:"tem";i:10;s:3:"tem";i:11;s:3:"tem";i:12;s:3:"tem";i:13;s:3:"tem";i:14;s:3:"tem";i:15;s:3:"tem";}";
$checkIni = unserialize($checklistIni );
foreach($rsChecklist as $checklist) {
foreach($checkIni as $key => $val){
?>
<tr>
<td class="texto-branco">
<?php echo $checklist['nome'] ?>
</td>
<td><label><input value="tem" name="checklistFim<?php echo $checklist['ID_Checklist'] ?>" class="checklistFim" type="radio" <?php if($checkIni[$key] == "tem"){echo "checked ";}; ?> /><span></span></label></td>
<td><label><input value="nao" name="checklistFim<?php echo $checklist['ID_Checklist'] ?>" class="checklistFim" type="radio" <?php if($checkIni[$key] == "nao"){echo "checked ";}; ?> /><span></span></label></td>
<td><label><input value="ava" name="checklistFim<?php echo $checklist['ID_Checklist'] ?>" class="checklistFim" type="radio" <?php if($checkIni[$key] == "ava"){echo "checked ";}; ?> /><span></span></label></td>
</tr>
<?php
};
};
?>
Your code is a little messed up, by the way hope I can help you with this:
$checklistIni = 'a:16:{i:0;s:3:"nao";i:1;s:3:"ava";i:2;s:3:"tem";i:3;s:3:"tem";i:4;s:3:"tem";i:5;s:3:"tem";i:6;s:3:"tem";i:7;s:3:"tem";i:8;s:3:"tem";i:9;s:3:"tem";i:10;s:3:"tem";i:11;s:3:"tem";i:12;s:3:"tem";i:13;s:3:"tem";i:14;s:3:"tem";i:15;s:3:"tem";}';
$checkIni = unserialize($checklistIni);
$rsChecklist = array(
array(
'nome' => 'A',
)
);
foreach ($rsChecklist as $k => $checklist) {
foreach ($checkIni as $key => $val) {
?>
<table>
<tr>
<td class="texto-branco">
<?php echo $checklist['nome'] ?>
</td>
<td><label><input value="tem" name="checklistFim<?php echo $key ?>" class="checklistFim"
type="radio" <?php if ($val == "tem") {
echo "checked='checked' ";
}; ?> /><span></span></label></td>
<td><label><input value="nao" name="checklistFim<?php echo $key ?>" class="checklistFim"
type="radio" <?php if ($val == "nao") {
echo "checked='checked' ";
}; ?> /><span></span></label></td>
<td><label><input value="ava" name="checklistFim<?php echo $key ?>" class="checklistFim"
type="radio" <?php if ($val == "ava") {
echo "checked='checked' ";
}; ?> /><span></span></label></td>
</tr>
</table>
<?php
}
}
Essentially you have to change those lines <input value="tem" name="checklistFim<?php echo $checklist['ID_Checklist'] ?>" class="checklistFim" type="radio" <?php if($checkIni[$key] == "tem"){echo "checked ";}; ?> />
The name attribute must be the same for each of three radio in lane and different with others lanes.

For loop display radio buttons with first one checked

I have a for loop that displays radio buttons and I want the first one to display as checked. But when I put a if statement inside the for loop for this the page nevers loads. Any ideas?
$mains = array(0=>'Beef Steak', 1=>'Chicken Breast', 2=>'Pork Chops');
$mainscount = count($mains);
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if($mainNO = 0){ echo 'checked="checked"'; } ?>/>
<?php } ?>
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if ($mainNO == 0) {
echo ' checked="checked" ';
} ?>/>
<?php } ?>
you use = where you should use ==
u are using assingment operator in comparision statement
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if ($mainNO == 0) {
echo " checked";
} ?>/>
and in HTML5 you can use checked only
<input type="checkbox" checked>
// Note: You used single = in if condition that is wrong, it will create indefinite loop . Tested code.
$mains = array(0=>'Beef Steak', 1=>'Chicken Breast', 2=>'Pork Chops');
$mainscount = count($mains);
for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) {
// Checked if value is 0
if($mainNO == 0){ $checked = 'checked="checked"'; }else { $checked =''; };
echo "<label for='mains".$mainNO."' class='radiobutton'>".$mains[$mainNO]."</label>";
echo "<input type='radio' name='mains' id='mains".$mainNO."' value='".$mainNO."' $checked />";
}

Multiselect Checkbox Validation in PHP

I am having a dynamic checkbox which is working on foreach. I want to add a validation for that. If I have added 'required' in my input type, it is asking to select all the checkboxes, Is there any other option to validate at least one checkbox is checked.
My Form
<?php
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" >
<?php echo $list->cat_title;
}
} ?>
try this code
<?php
$k==0;
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" <?php if($k==0) echo 'required';?>>
<?php echo $list->cat_title;
$k++;
}
} ?>

Adding checked variable from database with fields from database as well

I have a form which has a list of checkboxes to be filled in on insert to database as well as when editing. When editing I am trying to populate the fields with a list of fields from the database which is in the $groups variable and the checked value being checked against the group_id from groups with the group_id from the $user. It is working except it only fills out one checkbox and some users belong to multiple groups. Any ideas, or more efficient ways to do this.
Heres my code so far
<?php foreach($groups as $group) : ?>
<?php foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = "checked";
} else {
$checked = '';
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
This problem is that you are searching all the $users['group'] array and not stopping when you find a match, so unless last $uG['group_id'] matches $group['id'] you continue and therefore clear $checked after potentially setting it.
So just add a break when you find a match.
Also, officially the correct way of setting the checked status is checked="checked" although most modern browsers are not that pedantic, its possibly better to stick to the HTML spec
<?php foreach($groups as $group) : ?>
<?php
$checked = '';
foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = 'checked="checked"';
break;
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
you could also use a ternary operator and in_array() to do this all in one simple statement
<?php
foreach($groups as $group) :
$checked = in_array( $group['id'], $user['groups'] ) ? 'checked="checked"' : '';
?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
Define $checked = ''; first then loop and change the value if match found
<?php foreach($groups as $group) : ?>
<?php
$checked = '';
foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = "checked";
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>

if statement is equal to a value

I have a result(string) of 1,1,0,0 - These come from $sub_array['state']
Currently all of my check boxes are checked. How can I code the code below so that if its 1 its checked else its not? as the current code gives them all 'checked'
<?php
foreach($assoc_categories as $sub_array)
{
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
}
?>
<div>
<input
class="checkbox"
type="checkbox"
name="product_category"
class="product_category_selector"
id="product_category_<?php echo $sub_array['cat_id']; ?>"
data-id="<?php echo $sub_array['cat_id']; ?>"
<?php echo $checked_state; ?>
/>
<?php echo $sub_array['name']; ?>
</div>
<input
class="order"
type="input"
value="<?php echo $sub_array['sorder']; ?>"
/>
<?php
}
?>
Change:
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
}
To:
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
} else
{
$checked_state = "";
}
Basically, you are not clearing the previous value as the loop continues.
Alternatively, you could use:
$checked_state = ($sub_array['state'] == 1) ? " checked='checked'" : "" ;
You forget to reset checked_state or reset it to '' if $sub_array['state'] is equal to 0.
<?php
$assoc_categories = array(
array('state'=>1, 'cat_id'=>1, 'name'=>'one', 'sorder'=>1),
array('state'=>1, 'cat_id'=>2, 'name'=>'three', 'sorder'=>2),
array('state'=>0, 'cat_id'=>3, 'name'=>'four', 'sorder'=>3),
array('state'=>0, 'cat_id'=>4, 'name'=>'five', 'sorder'=>4),
);
foreach($assoc_categories as $sub_array)
{
$checked_state = $sub_array['state'] == 1 ? " checked='checked'" : '';
?>
<div>
<input
class="checkbox"
type="checkbox"
name="product_category"
class="product_category_selector"
id="product_category_<?php echo $sub_array['cat_id']; ?>"
data-id="<?php echo $sub_array['cat_id']; ?>"
<?php echo $checked_state; ?>
/>
<?php echo $sub_array['name']; ?>
</div>
<input
class="order"
type="input"
value="<?php echo $sub_array['sorder']; ?>"
/>
<?php
}

Categories