<!-- html-код --> <? if(!empty($array)) foreach ($array as $val){ ?> <!-- html-код --> <? $id = $val['id']; $string = Database->select(" SELECT COUNT(*) FROM `table` WHERE `id` = '$id' "); echo $string; ?> <!-- html-код --> <?}?> <!-- html-код --> 

I use on the site MVC-model. This is part of the code from View. As you can see, I am confused by the query to the database, because these are tasks of the controller or model.
Question : How to get rid of a query in View, if I need to somehow output the data in a loop depending on $id

PS I am sure that this question was understood somewhere, but I do not even know how to formulate it correctly.

  • What does it mean to display data depending on id ? will another markup be displayed or what? - Alexey Shimansky
  • Not. The request also shows that the sampling condition is changing. First, where id = 1, then where id = 2, etc. - FLighter
  • one
    And why it is impossible to make selection SELECЕ count(1) from table group by FIELD ? and you will have a sample of counts for each field ..... and further, the result is already passed to the view and cycle we analyze - Alexey Shimansky
  • Move your code to the model and controller, and transfer the already generated data to the view. In this case, select in advance all the necessary IDs in the controller and form one request in the model of them, instead of many: SELECT DISTINCT id, count(*) FROM table WHERE id IN [Список ID] GROUP BY id - Alex Krass
  • @AlexKrass only if id IN selects the total count , and the vehicle, as I understand it, for each id you need your own count - Aleksey Shimansky

0