0

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
1
  • 1
    What do you mean by I cannot for life of me loop through the array I have at the moment. You don't know how to use foreach? You don't know how to traverse multidimensional arrays? You got non-working code? Please be more specific about your problem. Commented Jan 6, 2010 at 17:00

4 Answers 4

3

It's an object, so you can't loop through it like an array. I do see what you're trying to do and understand why it seems like that makes sense, but to see what I'm talking about, try this:

Change 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();
}

To 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;
}

AND

Change this:

$image = array();
if ($query = $this->image_model->get_special_backgrounds()) {
    $image = $query;
}

To this:

   if($images = $this->image_model->get_special_backgrounds()) {
       foreach($images->result_array() as $image) {
          echo "<pre>";
          print_r($image);
          echo "</pre></br >";
       }
   }
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

1

Try foreach

$arr = (your array);

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

}

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.