Kazi Nayem Kazi Nayem - 2 months ago 6
PHP Question

how to extract this array through foreach loop

I have a multidimensional array.I want to extract this array through foreach loop and want to display in a unordered list. how to solve this. please help me. i am trying for 2 days but could not get any solution. i think i'm weak in loop.

Array
(
[id] => 1
[name] => Funny
[category_details] => Array
(
[get_everything] => Array
(
[0] => Array
(
[ci_cat_id] => 1
[img_name] => fapore kapor nosto
)

)
)

)

Array
(
[id] => 4
[name] => Events
[category_details] => Array
(
[get_everything] => Array
(
[0] => Array
(
[ci_cat_id] => 4
[img_name] => elo khushir eid
)

[1] => Array
(
[ci_cat_id] => 4
[img_name] => Eid e bari jacchi
)

)

)

)


i want the output like this

category name1:
1. test1
2. test2
3. test3
category name2:
1. test4
2. test5
3. test6

Answer

My Controller

function indexx() {
    $emp['get_all_img'] = $this->model_bundle->get_all_img();

    $t = $this->model_bundle->get_category();

    $i = 1;
    foreach ($t as $key => $data) {
        //$emp['get_everything'][$key]['id'] = $data['id'];
        $emp['get_everything'][$key]['name'] = $data['name'];
        $emp['get_everything'][$key]['category_details'] =  $this->get_category_wise_image($data['id']);
        $i++;
    }

    $this->layout->view('bundle/add_bundle', $emp);
}

function get_category_wise_image($cat_id) { 
    $data = $this->model_bundle->get_category_wise_image($cat_id);
    $i = 1;
    foreach ($data as $key => $data) {
        $emp['get_everything'][$key]['ci_cat_id'] = $data['ci_cat_id'];
        $emp['get_everything'][$key]['bangla_caption'] = $data['bangla_caption'];
        $emp['get_everything'][$key]['images_id'] = $data['images_id'];
        $emp['get_everything'][$key]['thumnail'] = $data['thumnail'];
        $i++; 
    }
    //echo "<pre>";  
    //print_r($emp);  
    return $emp;
}

My Model

function get_all_img()
{
    $this->db->select('id, bangla_caption, thumnail');
    $query = $this->db->get('images');
    return $query->result();
}

function get_category(){
    $this->db->select('id, name');
    $this->db->from('category');
    $this->db->order_by('id');
    $result = $this->db->get(); 
    return $result->result_array(); 
}

 function get_category_wise_image($id){
    $this->db->select('category_images.id as id, img_id, category_images.cat_id as ci_cat_id, images.cat_id as im_cat_id, bangla_caption, name, thumnail, images.id as images_id');
    $this->db->from('category_images');
    $this->db->join('category','category.id = category_images.cat_id','left');
    $this->db->join('images','images.id = category_images.img_id','left');
    $this->db->where('category_images.cat_id',$id);
    $result = $this->db->get(); 
    return $result->result_array(); 
}

view(add_bundle.php)

foreach($get_everything as $tasks){
    //echo "<pre>";
    //print_r($tasks);
echo '<div class="cat_name">'. $tasks['name'].'</div>';
echo "<ul>";
    foreach($tasks as $task){
                foreach($task as $p){ 
                    foreach($p as $pr){ 
                        echo '<div height="50px">
                        <label><input type="checkbox" name="img_id[]" class="second" value="'.$pr['images_id'].'">';
                        echo '<img src="'.base_url()."uploads/".$pr['thumnail'].'" width="60px">"'.$pr['bangla_caption'].'"</label></div>';              
                    }                        
                }  
        }echo "</ul>";
}