Welcome Guest, Not a member yet? Register   Sign In
Group By Month/Year Within Array
#11

[eluser]Jay Logan[/eluser]
Still doesn't work the way I need it to. Here is kind of what I want the list to look like. I have most of the work done but I'm stuck at the last part.

http://www.fusioncomputersolutions.com/meets

I've managed to assign the month & year to a value in the array. Now all I need to do is group duplicate month_year values together so I can make the list work. If I can't get it, I guess I will just add a new column to the DB with the value and make life easier.

Here's my model.

Code:
function get_all_meets()
    {
        $this->db->select();
        $this->db->from('meets');
        $this->db->where('hide', '0');
        $this->db->orderby('date');
        $query = $this->db->get();
        $result = $query->result_array();
        $i = 0;
        foreach ($result as $meet) {
            $this->db->select();
            $this->db->from('meet_events');
            $this->db->where('meet_id', $meet['id']);
            $query = $this->db->get();
            $events = $query->result_array();
            $key = $i++;
            $result[$key]['event_count'] = count($events);
            $result[$key]['month_year'] = date('F Y', strtotime($meet['date']));
        }
        return $result;
    }
#12

[eluser]xwero[/eluser]
When i wrote loop the results, i was thinking about doing the loop in the view.
Code:
// model
function get_all_meets()
{
        $this->db->select();
        $this->db->from('meets');
        $this->db->where('hide', '0');
        $this->db->orderby('date');
        $query = $this->db->get();
        return $query->result_array();
}
// controller
$data['meets'] = $this->model->get_all_meets();
// view
if(count($meets) > 0)
{  
    ?&gt;<table><tr><th>date</th><th>venue</th></tr>&lt;?php
    // set baseline
    $first = array_shift($meets);
    $unix = mysql_to_unix($first->date);
    $month = date('m',$unix);
    $year = date('Y',$unix);
    ?&gt;<tr><td clas="subtitle" colspan="2">&lt;?php echo $month.' '.$year ?&gt;</td></tr>
    <tr><td>&lt;?php echo $first->date ?&gt;</td><td>&lt;?php echo $first->venue ?&gt;</td></tr>&lt;?php
    foreach($meets as $meet)
    {
       $unix = mysql_to_unix($meet->date);
       $loop_month = date('m',$unix);
       $loop_year = date('Y',$unix);
       if($loop_month != $month OR $loop_year != $year)
       {
         ?&gt;<tr><td clas="subtitle" colspan="2">&lt;?php echo $loop_month.' '.$loop_year ?&gt;</td></tr>&lt;?php
         $month = $loop_month;
         $year = $loop_year;
       }
       ?&gt;<tr><td>&lt;?php echo $meet->date ?&gt;</td><td>&lt;?php echo $meet->venue ?&gt;</td></tr>&lt;?php
    }
    ?&gt;</table>&lt;?php
}
#13

[eluser]Jay Logan[/eluser]
Well that works. Kind of clutters my view file with a bunch of PHP. I think I may just insert that extra column into my database. Thanks for all the help though.




Theme © iAndrew 2016 - Forum software by © MyBB