Welcome Guest, Not a member yet? Register   Sign In
How to split db result with date values into single day groups
#1

[eluser]LuckyFella73[/eluser]
Hello,

I have a db table having date entries. I need to get all
entries and when looping through the result_array I
have to group the entries by day.

My task is to build HTML list items where I have all rows
of one day in one list item.

Code:
<ul>
<li>
  All rows having DB date 2012-08-12
</li>
<li>
  All rows having DB date 2012-08-13
</li>
<li>
  All rows having DB date 2012-08-14
</li>
<li>
  etc.
</li>
</ul>

It may be easy but I have no idea how to do that ...

#2

[eluser]Massaki[/eluser]
You must order your result by date, and when reading each row, check if the previous one has the same value, adding a new "<li>" when it doesn't.
#3

[eluser]Aken[/eluser]
[quote author="Massaki" date="1344859807"]You must order your result by date, and when reading each row, check if the previous one has the same value, adding a new "<li>" when it doesn't.[/quote]
That would result in a lot of logic in your view, which is not really recommended. They might not also want the dates sorted that way.

In your model, loop through your results, and sort them into a new array whose keys are the dates.

Here's what it might look like (this is not actual code for you, so don't just copy/paste):

Code:
$sorted = array();

foreach ($result as $row)
{
$sorted[$row->date][] = $row;
}
#4

[eluser]LuckyFella73[/eluser]
What Aken posted was my first intention but the problem was
that I had a time format like this in my DB table:
Code:
2012-08-13 14:15:00

and my loop had to "ignore" the part after the day value to
get the needed result.

In the end I get it to work this way:
Code:
$query = $this->db->query('SELECT * FROM '.$table_name.' GROUP BY DATE(start)'); // this mysql function "filters" by the Y-m-d value

  echo '<div class="items">';

  foreach ($query->result_array() as $row)
  {
   $start_date_complete = strtotime($row['start']);
   $date_ymd = date("Y-m-d", $start_date_complete);

   echo $this->get_day_wrapper_start($date_ymd);
   echo $this->get_info($date_ymd).'<br><br>';
   echo $this->get_day_wrapper_end();
  }
  echo '</div>';

Thanks to both of you for your replies !!




Theme © iAndrew 2016 - Forum software by © MyBB