0

How to check check boxes if return value is match otherwise uncheck checkbox.

Return array:

Array (
    [0] => Array (
        [Property_Name] => LMS Unlink Customer
    )
    [1] => Array (
        [Property_Name] => LMS Notification
    )
) 
<th style="text-align: center;"><input type="checkbox" class="flat" <?php if($getlmsprivileges['Property_Name'] == 'LMS Reward'){ ?> checked <?php } ?> name="Reward"></th>
    <td style="text-align: center;"><input type="checkbox" class="flat" <?php if($getlmsprivileges['Property_Name'] == 'LMS Promotion'){ ?> checked <?php } ?> name="Promotion"></td>
    <td style="text-align: center;"><input type="checkbox" class="flat" <?php if($getlmsprivileges['Property_Name'] == 'LMS VAS'){ ?> checked <?php } ?> name="VAS"></td>                          
    <td style="text-align: center;"><input type="checkbox" class="flat" <?php if($getlmsprivileges['Property_Name'] == 'LMS Unlink Customer'){ ?> checked <?php } ?> name="Unlink_Customer"></td>                          
    <td style="text-align: center;"><input type="checkbox" class="flat"  <?php if($getlmsprivileges['Property_Name'] == 'LMS Notification'){ ?> checked <?php } ?> name="Notification"></td>                                                    
4
  • there's no LMS Notification value in the code you have shown Commented Sep 4, 2016 at 5:46
  • Your checkbox name is single..so how do you expect the array to have the keys "Property_Name"?? It hardly makes any sense. Commented Sep 4, 2016 at 5:51
  • You neglected to explain how you get this array in the first place. There's a disconnect between what you expect this code to do and the structure of your array. So the problem must reside in how the array is generated. Commented Sep 4, 2016 at 5:58
  • I got this array format on return value in yii queryAll result Commented Sep 4, 2016 at 6:01

2 Answers 2

1

The PHP function

function Property_Name_Check($data,$check){
  foreach ($data as $key => $value) {
    if($value['Property_Name'] == $check){
      return true;
    }
  }
return false;
}

The HTML elements

<th style="text-align: center;"><input type="checkbox" class="flat" name="Reward" <?php echo (Property_Name_Check($getlmsprivileges,'LMS Reward')) ? 'checked="true"' : ''; ?>></th>
<td style="text-align: center;"><input type="checkbox" class="flat" name="Promotion" <?php echo (Property_Name_Check($getlmsprivileges,'LMS Promotion')) ? 'checked="true"' : ''; ?>></td>
<td style="text-align: center;"><input type="checkbox" class="flat" name="VAS" <?php echo (Property_Name_Check($getlmsprivileges,'LMS VAS')) ? 'checked="true"' : ''; ?>></td>
<td style="text-align: center;"><input type="checkbox" class="flat" name="Unlink_Customer" <?php echo (Property_Name_Check($getlmsprivileges,'LMS Unlink Customer')) ? 'checked="true"' : ''; ?>></td>
<td style="text-align: center;"><input type="checkbox" class="flat" name="Notification" <?php echo (Property_Name_Check($getlmsprivileges,'LMS Notification')) ? 'checked="true"' : ''; ?>></td>
Sign up to request clarification or add additional context in comments.

Comments

0

Since you're retrieving this array from your database what you need to do is map the values of each row in the array to the corresponding checkbox in your form. This way determining if a chexkbox is checked or not is simply a boolean operation. The absence of the value in your array determines that it's unchecked while the presence of a value determines that it is checked.

Right now it looks like you store the name of the checkbox, with some prefix, as the value you're getting back from your database. So what you need to do is find a way to get all of the expected checkbox names you intend to print out in your form, and map them the values obtained from your database to get an array like this...

$allCheckboxNames = [
    "Reward"          => false,
    "Promotion"       => false,
    "VAS"             => false,
    "Unlink_Customer" => false,
    "Notification"    => false,
];

So let's say in your database result that "Unlink_Customer" and "Notification" are the ones that are checked.

$resultArray = [
    ["Property_Name" => "Unlink_Customer"],
    ["Property_Name" => "Notification"],
];

We're just going to create an intersection of the two and a union to get a boolean representation of all the of them as either checked or unchecked.

But first you have to translate your two-dimensional array to a flat one-dimension array like this.

$resultArray = array_map(function($n) { return $n["Property_Name"]; }, $resultArray);

// Since we also need the values as keys we'll flip the array
$resultArray = array_map(function($v) { return true; }, array_flip($resultArray));

Now we just need to intersect by key and union all.

$resultArray = array_intersect_key($resultArray, $allCheckboxNames) + $allCheckboxNames;

Now producing the output from that array becomes trivial. You just print them all out in a loop using the array's key as the checkbox name, and the array's boolean value as a depiction of whether or not it's checked.

<?php
foreach($resultArray as $name => $checked) {
  ?>
  <input type="checkbox" class="flat" <?=$checked ? 'checked' : null; ?> name="<?=$name?>">
  <?php
}

Here's a complete lab of the MVCE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.