There is such a table of reports by dates.

enter image description here

Using the query (in the model)

$query = DB::select('id', 'date', 'content', array(DB::expr('year(`date`)'), 'year')) ->from('reports') ->where('activity_id', '=', $ID) ->order_by('date', 'DESC'); 

Got data
But now I need to output them to a template, this way
enter image description here
Those. create a list in which “year” needs to be output only once to all reports created in a particular year. Cycle <?php foreach($list_reports as $key => $list_report):?> I can <?php foreach($list_reports as $key => $list_report):?> and output, only it turns out to display a year for each article.

The question is whether you need to redo the query itself or the resulting array?

  • Wrote a procedure that changes the keys of an array, before this, by copying an existing array (query result) into another variable $ editList = $ query; , but when the procedure starts to work, the error Kohana_Exception [0] falls : Database results are read-only - Konstantin78
  • Since this is a raw query, I think you can add a column with the year to it. - Andrei Talanin 4:34 pm
  • so the column with "year" ( array (DB :: expr ('year ( date )'), 'year') ) is already there. Now the task , in view, is to display it only once - Konstantin78
  • Sorry, did not notice. Well then there is only a custom grid to write, you better not imagine. - Andrei Talanin
  • Andrew @, what do you mean by "custom grid"? Give me a tip, please. And you can tell me how I can copy $ query to another variable - so that you can change it (this is an array) - Konstantin78

1 answer 1

 <?php foreach($list_reports as $key => $list_report): ?> ... <? if(!array_key_exists($list_report['year']), $years): ?> <?= $list_report['year'] ?> ... <? $years[$list_report['year']] = true; ?> ... <? endif; ?> ... 
  • Thanks Andrew - it helped ... With an additional array in view, I would not think of it (I used to use all php in the controller). True, a slight hesitation was with "$ years", but then in the controller I created an empty array "$ years" and passed it to view. - Konstantin78
  • I just gave the idea, and you understood it correctly) - Andrei Talanin