Mark radio using unserialize inside foreach - php

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.

Related

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

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

PHP Update multiple rows in MySQL

I have this PHP/HTML Code that is selecting data from a MySQL Database:
<?php
$sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' ";
$rs3=mysql_query($sql3,$conn);
while($property_img=mysql_fetch_array($rs3))
{
?><tr>
<td colspan="2"><img src="http://domain.co.uk/img/property-images/<?php echo $property_img["image"]; ?>" width="80px" height="80px" /></td>
<td colspan="2"><input type="checkbox" name="image1" value="Y" <?php if($property_img["image1"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image2" value="Y" <?php if($property_img["image2"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image3" value="Y" <?php if($property_img["image3"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image4" value="Y" <?php if($property_img["image4"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image5" value="Y" <?php if($property_img["image5"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image6" value="Y" <?php if($property_img["image6"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image7" value="Y" <?php if($property_img["image7"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image8" value="Y" <?php if($property_img["image8"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image9" value="Y" <?php if($property_img["image9"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image10" value="Y" <?php if($property_img["image10"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image11" value="Y" <?php if($property_img["image11"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image12" value="Y" <?php if($property_img["image12"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image13" value="Y" <?php if($property_img["image13"] == 'Y') { echo 'checked="checked"'; } ?> /></td>
</tr><?php
}
?>
each row, has its own image with the columns image1 - image13
i want to update the table with the checkboxes that are checked and unchecked on the form update
how is this possible?
Thanks
If you mean that you want to update the $property_img["imagexx"] val on db depending on the user click on the checkbox you must use ajax.
Fire an event on triggering each checkbox and send the value to a php page that update the php.
Jquery Ajax function can help you on this task.
L
name all your checkboxes images[]
<input type="checkbox" name="images[]" value="1" />
<input type="checkbox" name="images[]" value="2" />
<input type="checkbox" name="images[]" value="3" />
<input type="checkbox" name="images[]" value="4" />
You can then update with PHP :
<?php
$q = "UPDATE property_images SET";
foreach($_POST["images"] as $image) {
$q .= " image" . $image ." = 'Y', ";
}
$q .= " property_seq = '".$property["sequence"]."' WHERE property_seq = '".$property["sequence"]."'";
mysql_query($q);
?>
First of all, mysql_ functions are deprecated, please google mysqli_ or PDO, mysql_ won't be supported on future versions, and is unsafe, etc
Your html output could be much simpler with a loop, also have an eye on putting the sequence number on a hidden field or something first:
<?php
$sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' ";
$rs3=mysql_query($sql3,$conn);
while($property_img=mysql_fetch_array($rs3)){
?>
<tr>
<td colspan="2"><img src="http://domain.co.uk/img/property-images/<?php echo $property_img["image"]; ?>" width="80px" height="80px" /></td>
<!-- THIS IS VERY IMPORTANT: send the sequence or ID through a hidden field, to know which row you are gonna update later-->
<input type="hidden" name="sequence" value="<?php echo $property_img['sequence']; ?>"/>
<td colspan="2">
<?php for($i = 1; $i <= 13; $i++): ?>
<input type="checkbox" name="images[]>" value="<?php echo $i; ?>" <?php if($property_img["image$i"] == 'Y') { echo 'checked="checked"'; } ?> />
<?php endfor ?>
</td>
</tr>
<?php
}
?>
Then this is the next page where the update is done, have a good look at the comments:
<?php
//Handle as you want the situation when there are no images selected instead of using an exit(), I used it here just for the quickness;
if(count($_POST['images'] < 1) exit('no images where selected to update');
$images = array();
for($i = 1; $i <= 13; $i++){
$string = "image$i = ";
if(in_array($i, $_POST['images'])){
$string .= "'Y'"; //notice the double quoting here, it's important
} else {
//This updates the table so if it was checked when loaded, but unchecked by the user, this makes the change!
$string .= "'N'";
}
}
//This creates a string like: image1 = 'Y', images2 = 'N', etc...
$images = implode(', ', $images );
//try to sanitize the query first ... I won't cos am just showing you your question xD
$sql = "UPDATE property_images SET $images WHERE property_seq = " . mysql_real_escape_string($_POST[sequence]) . ";
mysql_query($sql,$conn);
?>

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