Adding checked variable from database with fields from database as well - php

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

Related

Dropdown List, when no data in database can't input manually

enter image description hereI'm creating a program, using the dropdown list.
so here is the form "Pilih Unit Mesin", where the machine unit that has the number "Torsi" in the database automatically will appear numbers in the form of "Torsi"
The problem is how if the machine unit has no numbers in database, i hope in the form "Torsi" can be input manually number
Please help, thank you
<label>Pilih Unit Mesin</label>
<select class="form-control" name="id_unit" id="id_unit" required="">
<option value=""><b>nama proyek - unit mesin</b></option>
<?php foreach ($mesin as $key => $value) :?>
<?php if ($value['status_unit'] === '0'): ?>
<option <?php echo $unit_selected == $value['id_unit'] ? 'selected="selected"' : '' ?>
value="<?php echo $value['id_unit'].'">'.' - '.$value['nama_project'].' - '.$value['unit_mesin'].'</option>'; ?>">
<?php else: ?>
<?php endif; ?>
<?php endforeach?>
</select>
</div>
<label>Torsi</label>
<select class="form-control" name="torsi" id="torsi" required="">
<?php foreach ($poross as $key => $val) :?>
<?php if ($val['torsi_poros'] != null): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="<?php echo $val['id_unit'] ?>" value="<?php echo $val['torsi_poros'] ?>"><?php echo $val['torsi_poros'] ?></option>
<span class="input-group-addon">Kg.mm</span>
<?php elseif ($val['torsi_poros'] != ""): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="form-group input-group">
<input class="form-control" name="torsi" type="text" required="" pattern="^(\+|-)?[0-9].*$">
<span class="input-group-addon">Kg.mm</span>
</div>
<?php endif; ?>
<?php endforeach?>
</select><br>
enter image description here
Screenshot 1:
Screenshot 2:
Screenshot 3:
enter image description here
You could combine multiple array functions to check whether specific key from multidimensional array is not empty :
<label>Torsi</label>
<?php
if (empty(array_filter(array_column($poross, 'torsi_poros')))) {
?>
<input type="number" name="torsi" id="torsi" class="form-control" >
<?php } else { ?>
<select class="form-control" name="torsi" id="torsi" required="">
<?php foreach ($poross as $key => $val) :?>
<?php if ($val['torsi_poros'] != null): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="<?php echo $val['id_unit'] ?>" value="<?php echo $val['torsi_poros'] ?>"><?php echo $val['torsi_poros'] ?></option>
<span class="input-group-addon">Kg.mm</span>
<?php elseif ($val['torsi_poros'] != ""): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="<?php echo $val['id_unit'] ?>" value="<?php echo $val['torsi_poros'] ?>"><?php echo $val['torsi_poros'] ?></option>
<span class="input-group-addon">Kg.mm</span>
</div>
<?php endif; ?>
<?php endforeach?>
</select>
<?php } ?>
<br>
Using array_column to return the values from a single column in the array, then array_filter to discard empty array.

How to retrieve checkbox value from database in 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>

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++;
}
} ?>

how to add check bo0x in selection box with multiple selection box

I used selection box with multiple selection now the problem is that it is selectable with ctrl+click of mouse. It is work properly but not that much prefrble to me and lookes like simple selection box and user cant get that its multiple selector not single.so thats why i want it with check box so user easly get it is multiple selector.please give apropriate solution thanks in advanced...
<select class="selopt" id="selPreLoc" name="SelPreLoc[]" multiple="multiple" size=5>
<option label="No Preference">No Preference</option>
<?php
//<option value=-1 selected>No Preference</option>
while ($rec = mysql_fetch_array($GetCityRecord)) {
if ($rec['City_Id'] == 30 || $rec['City_Id'] == 34 || $rec['City_Id'] == 35) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
?>
<option value="<?php $rec['City_Id']; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $rec['City']; ?>
</option>
<?php
}
foreach ($others as $ind => $val) {
?>
<option value="<?php echo $ind; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $val; ?>
</option>
<?php }
?>
</select>
<label class="formtxt" valign="bottom">Use Ctrl + Click to multi-select.</label></td>
I got many other solution with using div or other.
but i just want it with select option only is it posible if yes how .andi can fetch the result in mysql and i want that result with comma seprator in mysql.
My code is not tested, since i do not have the data, but based on your logic, you can use it like this:
$theOthers = array(30, 34, 35);
while ($rec = mysql_fetch_array($GetCityRecord)) {
if (in_array($rec['City_Id'], $theOthers)) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php $rec['City_Id']; ?>" <?php echo $checked; ?> /> <?php echo $rec['City']; ?> <br />
<?php
}
foreach ($others as $ind => $val) {
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php echo $ind; ?>" <?php echo $checked; ?> /> <?php echo $val; ?> <br />
<?php
}
NOTE: I improved your code a littlebit with the $theOthers array, i think it's more readable.
When form is submitted, let's var_dump($_POST["city"]);

Passing checkbox values into an array?

I have set up a group of checkboxes. They are dynamic, so the number of checkboxes will be different dependent on the person using the site. The structure of how the checkboxes are created is:
<label for="plabackformat-holder-label">Format</label>
<div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
<div class="playback-format-radio-buttons">
<label for="notset-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="notset" name="playback_format[<?php echo $second_num; ?>]" id="notset-<?php echo $num; ?>" <?php if($field['playback_format'] == 'notset') { echo 'checked'; } ?>>None
</label>
<label for="dvd-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format[<?php echo $second_num; ?>]" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format'] == 'dvd') { echo 'checked'; } ?>>DVD
</label>
<label for="bluray-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format[<?php echo $second_num; ?>]" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format'] == 'bluray') { echo 'checked'; } ?>>Bluray
</label>
<label for="3d-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="3d" name="playback_format[<?php echo $second_num; ?>]" id="3d-<?php echo $num; ?>" <?php if($field['playback_format'] == '3d') { echo 'checked'; } ?>>3d
</label><br />
</div>
</div>
My save function is:
$new = array();
for ( $i = 0; $i < $count; $i++ ) {
$new[$i]['playback_format'] = $playbackFormats[$i];
}
I've been reading up on this issue and it seems its because my input fields do not contain unique names. I'm trying to store the data into an array, so it would be ['playback_format'] => dvd,3d,bluray or something similar.
Right now its only storing the last checked value. Is there a way I can use a forloop or something to iterate over the checked values and push them into my array??
You can just get rid of the "$second_num" in each <input name="playback_format[]"/> html tag. This will put everything into an array for you once you submit the form. You can check this by adding this line to the page as a test.
<?php print_r($_REQUEST['playback_format']); ?>
Generally, You want to avoid any loop if they aren't required.
Hope that helps with what you are doing.
What is $second_num? Does it need to be a part of the input name?
You can get PHP to recognise the submitted values as an array if you do it this way:
<input name="playback_format[<?php echo $second_num; ?>][]">
Or if you don't need $second_num as part of the name, just:
<input name="playback_format[]">
$_POST['playback_format'] will then be an array containing all the selected options.
There is a section in the PHP docs specifically about this behaviour.
Checkboxes have all the same name in your example. Name it differently like :
<label for="plabackformat-holder-label">Format</label>
<div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
<div class="playback-format-radio-buttons">
<label for="notset-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="notset" name="playback_format_notset" id="notset-<?php echo $num; ?>" <?php if($field['playback_format_notset'] == 'notset') { echo 'checked'; } ?>>None
</label>
<label for="dvd-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format_dvd" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format_dvd'] == 'dvd') { echo 'checked'; } ?>>DVD
</label>
<label for="bluray-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format_bluray" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format_bluray'] == 'bluray') { echo 'checked'; } ?>>Bluray
</label>
<label for="3d-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="3d" name="playback_format_3d" id="3d-<?php echo $num; ?>" <?php if($field['playback_format_3d'] == '3d') { echo 'checked'; } ?>>3d
</label><br />
</div>
</div>
And Try this in PHP :
//WHen you want to see what is send in Post :
var_dump($_POST);
//So, for get result :
$tab = array();
foreach($_POST as $key =>$value){
$tab[$key] = $value;
//Display it
echo $key . "=" . $value;
}

Categories