views:

369

answers:

3

Hello,

I am currently working on a site that is being built on codeigniter, I am currently querying the data at the moment I think there could be possible 3 arrays that could be returned as array each with a varying amount of results, my question I cannot for life of me loop through the array I have at the moment,

my model looks like this

public function get_special_backgrounds() {
    $this->db->select('*');
    $this->db->from('background');
    $this->db->where('is_special', 1);

    $query = $this->db->get();
    return $query->result_array();
}

my controller

enter public function index() {
//  $this->output->enable_profiler(TRUE);
    $data = array();
    if($query = $this->category_model->get_all_online()) {
        $data['main_menu'] = $query;
    }
    $this->load->model('image_model');
    /*
    * Sort out the users backgrounds, basically do a check to see if there is a 'special' background
    * if there is not a 'special' background then IF the user is logged in and has a background of there
    * own show that one, if not show a generic one, if they are not logged in show a generic one
    */
    $image = array();
    if ($query = $this->image_model->get_special_backgrounds()) {
        $image = $query;
    }

    $data = array_merge($data, $image);
    die(print_r($data));
    $this->load->view('home/main_page.php', $data);
}

the array the gets return looks like this,

Array
(
    [main_menu] => CI_DB_mysql_result Object
        (
            [conn_id] => Resource id #28
            [result_id] => Resource id #35
            [result_array] => Array
                (
                )

            [result_object] => Array
                (
                )

            [current_row] => 0
            [num_rows] => 1
            [row_data] => 
        )

    [special] => Array
        (
            [0] => Array
                (
                    [background_id] => 6
                    [background_name] => Master-Backgrounds.png
                    [background_path] => /Users/Simon/Sites/mysite/media/uploads/backgrounds/
                    [is_special] => 1
                    [background_date_uploaded] => 1262687809
                    [users_user_id] => 1
                    [users_user_group_group_id] => 1
                )

            [1] => Array
                (
                    [background_id] => 11
                    [background_name] => Master-mysite-Template.png
                    [background_path] => /Users/Simon/Sites/mysite/media/uploads/backgrounds/
                    [is_special] => 1
                    [background_date_uploaded] => 1262795313
                    [users_user_id] => 5
                    [users_user_group_group_id] => 2
                )

        )

)
1
A: 

Looks like a result array, with an odd element at the beginning. I'd get rid of the first element and then just loop through it:

array_shift($data);
foreach ($data as $row) {
  // Do stuff with $row
  var_dump($row);
}
pygorex1
A: 

Do you need to loop on the special part of the array?

foreach ( $data['special'] as $row ) {
    // do stuff with the $row array
}
kemp
A: 

Try foreach

$arr = (your array);

foreach ($arr as $key => $insideArrays) {
 foreach ($insideArrays as $k2 => $insideInsideArrays){
  ..........
 }

}
Natkeeran