Reputation: 6976
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
Upvotes: 0
Views: 8533
Reputation: 31
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 >";
}
}
Upvotes: 3
Reputation: 1749
Try foreach
$arr = (your array);
foreach ($arr as $key => $insideArrays) {
foreach ($insideArrays as $k2 => $insideInsideArrays){
..........
}
}
Upvotes: 1
Reputation: 25060
Do you need to loop on the special
part of the array?
foreach ( $data['special'] as $row ) {
// do stuff with the $row array
}
Upvotes: 2
Reputation: 38298
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);
}
Upvotes: 0