0

I have to compare dropdown with multiple array, my first array is

 [menu_master] => Array
(
    [master/city] => City
    [master/national_holiday] => National Holiday
    [master/operator_comments] => Operator Comments
    [master/sensors] => Sensors
    [master/modbus] => Modbus
    [master/manufacturers] => Manufacturers
    [master/make_model] => Make Model
)

my second array is

 [role_assign] => Array
(
    [1] => View
    [2] => Write
)

And my third array is come form database

    [database_fetch] => Array
        (
            [master/city] => 1
            [master/national_holiday] => 2
            [master/operator_comments] => 1
            [master/sensors] => 2
            [master/modbus] => 1
            [master/manufacturers] => 2
            [master/make_model] => 1
)

Now these array apply to below code. I want show selected option of dropdown which comes form database fetch array.

<?php
                    $menu_manster =  menu_master();
                    $i = 1;
                    foreach($menu_manster as $k => $val) {
                    ?>
                    <tr>
                      <td>{{ $i }}</td>
                      <td class="mailbox-name">{{ $val }}</td>
                      <td><?php $roles_assign =  roles_assign_id(); ?>
                            <select class="form-control master-menu" name="master_menu[{{$k}}]">
                              <option value="">Select Role</option>
                                <?php 

                                    foreach ($roles_assign as $key => $value) { 
                                ?>
                                <option value="<?php echo $key; ?>"><?php echo $value; ?></option>
                              <?php } ?>
                            </select>
                      </td>
                    </tr>
                    <?php $i++;  } ?>
0

1 Answer 1

1

Within your $roles_assign loop, you have to check whether the $value matches with the value of the database_fetch item. Assuming you have stored the database_fetch data in a variable named $database_fetch, the solution would look something like this:

<?php
$menu_manster =  menu_master();
$i = 1;
foreach($menu_manster as $k => $val) {
?>
<tr>
  <td>{{ $i }}</td>
  <td class="mailbox-name">{{ $val }}</td>
  <td><?php $roles_assign =  roles_assign_id(); ?>
        <select class="form-control master-menu" name="master_menu[{{$k}}]">
          <option value="">Select Role</option>
            <?php 

                foreach ($roles_assign as $key => $value) { 
            ?>
            <option value="<?php echo $key; ?>"<?=($database_fetch[$k] === $key ? ' selected' : '')?>><?php echo $value; ?></option>
          <?php } ?>
        </select>
  </td>
</tr>
<?php $i++;  } ?>
Sign up to request clarification or add additional context in comments.

2 Comments

how to check with database_fetch array, as it is alerady in array
i have little confusion as why are you check with first menu_master array?

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.