phparraysloopscodeigniter

foreach loop is only storing data from the last assignment


$offers variable include three value of ids. Now,I would like to fetch these ids from database and store them in $data['offer_day'] :

 $offers = explode('+', $post['offer_day']) ;

when I use this foreach, it stores just one and last query. now, how can I have three queries on $data['offer_day'] and how can use them in view ?

foreach ($offers as $o) {
    $offer_day = $this->m_general->get('coupons', array('id' => $o), false);
    $data['offer_day'] = $offer_day->result_array();
    if(!$data['offer_day'])
        exit("error");
}

Solution

  • Update like this:

    $data['offer_day'] = array();
    foreach($offers as $o){
          $offer_day = $this->m_general->get('coupons' , array('id'=>$o) , false );
            $data['offer_day'][]    = $offer_day->result_array();
            if(!$data['offer_day']) exit("error");
        }
    
    $this->load->view('your view file', $data);
    

    And in the view file you can access like $offer_day which will be an array.