0

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()
1
  • there is no problem, I just don't know which name to use in the foreach to loop throught the array items. If I use $year as $y it will give Message: Invalid argument supplied for foreach() Commented Apr 29, 2010 at 15:39

1 Answer 1

2

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; ?>
Sign up to request clarification or add additional context in comments.

4 Comments

i forgot codeigniter extracts the keys from the arrays, naming the view variables after the keys.
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)
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 :)
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 :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.