V
V
vadimstroganov2015-04-19 14:31:43
PHP
vadimstroganov, 2015-04-19 14:31:43

How to group records by year?

Good afternoon!
There are many events and the year of their origin, for example:
2013 - event 1
2013 - event 2
2013 - event 3
2012 - event 4
2011 - event 5
There is no way to group them so that they can be displayed in such a form:
2013:
- event 1
- event 2
- event 3
2012:
- event 4
2011:
- event 5
I take data from the database and sort it by year in descending order
I would be grateful for any help!
Everything worked out, I did this:

<?
  $j = 0;
  if ($i == 0) {
    $tempYear = $year;
    echo $tempYear;
  }
  if (($i - $j == 1) && ($i != 0) ) {
    if ($tempYear != $year) {
      $tempYear = $year;
      echo $tempYear;
    }
    $j++;
  }
?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vadimstroganov, 2015-04-19
@vadimstroganov

<?
  $j = 0;
  if ($i == 0) {
    $tempYear = $year;
    echo $tempYear;
  }
  if (($i - $j == 1) && ($i != 0) ) {
    if ($tempYear != $year) {
      $tempYear = $year;
      echo $tempYear;
    }
    $j++;
  }
?>

M
Michael, 2015-04-19
@Remdev

$year = 0;
while($row = $db->fetch()){
    if($year!=$row['year']){
        $year = $row['year'];
        echo $year;
    }
    echo $row['event'];
}

Something like that probably

K
kompi, 2015-04-19
@kompi

Something like that:

$arr = [];
while($row = $db->fetch())
  $arr[$row['year']][] = $row['value'];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question