I have this code:

<div ng-init="users = [ <?foreach($users as $user):?> {id:'<?= $user['id'] ?>', name:'<?= $user['name'] ?>', phone:'<?= $user['phone'] ?>', country_id:'<?= $user['country_id'] ?>'}, <?endforeach;?> ]"></div> <div class="row" ng-controller="AngularCtrl" ng-repeat="user in users"> 

Here I do something with user. And then I need to use the list of countries and compare user.country_id with $ country ['id'].

  <select class="form-control" name="country" onchange="setCountry(user.id, this.value)" ng-disabled="!country"> <?foreach ($countries as $country):?> <option value="<?= $country['id'] ?>" <?if ($user['country_id'] == $country['id']) echo "selected"?>><?= $country['country'] ?></option> <?endforeach;?> </select> </div> 

How can I do that? Or how to rewrite the code accordingly?

  • It is not entirely clear what the problem is. And what's wrong with your code? - Alexey Prokopenko
  • I replaced foreach with ng-repeat, and now I don't have the $ user ['country_id'] php variable, but I have user.country_id. - Timur Musharapov
  • Let's first determine that js and php do not directly interact with each other. The interaction takes place on http. That is, php sends some kind of data, and then js works with them. If the client needs new data from the server, then another request is made. - Alexey Prokopenko 5:02 pm
  • If I fill an array of countries as well as an array of users, then what is the appropriate expression or condition, like "<? If ($ user ['country_id'] == $ country ['id']) echo" selected "?>", Me will need to write? - Timur Musharapov
  • And what foreach does not suit you? - dchayka

0