views:

249

answers:

1

I have a model and controller who gets some data from my database and returns the following array

Array
(
    [2010] => Array
        (
            [year] => 2010
            [months] => Array
                (
                    [0] => stdClass Object
                        (
                            [sales] => 2
                            [month] => Apr
                        )

                    [1] => stdClass Object
                        (
                            [sales] => 1
                            [month] => Nov
                        )

                )

        )

    [2011] => Array
        (
            [year] => 2011
            [months] => Array
                (
                    [0] => stdClass Object
                        (
                            [sales] => 1
                            [month] => Nov
                        )

                )

        )

)

It shows exactly what it should show but the key's have different names so I have no idea on how to loop through the years using foreach in my view. Arrays is something I'm not that good at yet :(

this is the controller if you need to know:

    function analytics()
    {
        $this->load->model('admin_model'); 
        $analytics = $this->admin_model->Analytics();
        foreach ($analytics as $a):
            $data[$a->year]['year'] = $a->year;
            $data[$a->year]['months'] = $this->admin_model->AnalyticsMonth($a->year);
        endforeach;

        echo"<pre style='text-align:left;'>";
        print_r($data);
        echo"</pre>";

        $data['main_content'] = 'analytics';
        $this->load->view('template_admin', $data);
    }//end of function categories()
+2  A: 

put the arrays of years in a separate index in the array so you can isolate it

foreach ($analytics as $a) {
    $data['dates'][$a->year]['year'] = $a->year;
    $data['dates'][$a->year]['months'] = $this->admin_model->AnalyticsMonth($a->year);
}

Then you can loop through that without having to worry about the other data

<?php foreach( $dates as $year => $year_content  ): ?>
<h2><?php echo $year ?></h2>

<?php foreach( $year_content['months'] as $months ): ?>
<h3><?php echo $months->month ?> - <?php echo $months->sales ?></h3>
<?php endforeach; ?>

<?php endforeach; ?>
Galen
thanks but I get Undefined variable: data (in the view)
krike
fixed!! try again
Galen
i forgot codeigniter extracts the keys from the arrays, naming the view variables after the keys.
Galen
I did the same but it still gives errors, Message: Undefined variable: year_content and Fatal error: Cannot access empty property in (the foreach line)
krike
you gotta do a little work!! i fixed it try again!
Galen
I'm not that good with arrays yet so sorry if I ask stupid questions, but it's not in the second foreach but in the first one that I get an error "$year -> $year_content ", that $year_content variable which should be the value, that's what he's whinning about :)
krike
I found the problem, in the first foreach I changed -> to => :) just a typo, it works now thanks for your help. I have another page where I have this problem so this really helps me out. so a big thank you :)
krike
oops! no problem
Galen