There are 2 arrays: an array with groups for fields Array ([0] => Array ([id] => 2 [title] => Cars [description] => Fields with characteristics for cars))

[1] => Array ( [id] => 3 [title] => Недвижимость [description] => Группа для полей к объявлениям о недвижимости ) ) 

and an array for connecting fields that belong to these groups: Array ([0] => Array ([id] => 32 [id_field] => 12 [id_fieldsgroup] => 2)

 [1] => Array ( [id] => 33 [id_field] => 12 [id_fieldsgroup] => 3 ) ) 

How can you compare these two arrays so that when the elements match, all for which $ fgl ['id'] == $ fgff ['id_fieldsgroup'] were selected, I tried to do this, but this is not true:

 <div class="col-xs-12"> <select name="id_group[]" multiple="true" id="id_group" class="form-control col-xs-12"> <?php if(!empty($FieldsGroupList)):?> <?php foreach($FieldsGroupList as $fgl): ?> <?php foreach ($getFieldsGroupForField as $fgff): ?> <?php if ($fgl['id'] == $fgff['id_fieldsgroup']): ?> <option value="<?=$fgl['id']?>" selected><?=$fgl['title']?> - <?=$fgl['description']?></option> <?php endif ?> <?php endforeach ?> <?php endforeach ?> <?php else: ?> <option value="0">Нужно создать группы!</option> <?php endif ?> </select> </div> 

with this code, they are allocated only for which the condition coincided, the rest are not displayed at all, how to fix it ??

  • Describe correctly what you want to get as a result. - Nikolaj Sarry 2:44 pm
  • I want to build a select list with multiple choices so that those <option> for which $ fgl ['id'] == $ fgff ['id_fieldsgroup'] have been selected - user190950
  • Well, you make sure that the option tag is not created, but only the selected class is set. What is the problem? - Nikolaj Sarry pm

1 answer 1

In order for the rest of the options to be displayed, and the necessary ones should only be selected , the condition should be hung up to display the selected class, and not to draw the tag.

Instead:

 <?php if ($fgl['id'] == $fgff['id_fieldsgroup']): ?> <option value="<?=$fgl['id']?>" selected><?=$fgl['title']?> - <?=$fgl['description']?></option> <?php endif ?> 

Need to:

 <option value="<?=$fgl['id']?>" <?= ($fgl['id'] == $fgff['id_fieldsgroup']) ? 'selected' : '' ?>><?=$fgl['title']?> - <?=$fgl['description']?></option> 

Or so:

 <?php if ($fgl['id'] == $fgff['id_fieldsgroup']): ?> $selected = 'selected'; <?php endif ?> <option value="<?=$fgl['id']?>" <?= $selected ?>><?=$fgl['title']?> - <?=$fgl['description']?></option> 
  • And how to do the same thing in javascript ?? - user190950
  • what exactly is the same thing? If the prescription attribute is selected, then it is not clear why. If you want to rewrite everything to js, ​​then take it and start rewriting. Try it, but do not get a new question and attach your code. Nobody will write for you just like that. - Nikolaj Sarry pm