Search code examples
phphtmlcodeignitercodeigniter-2

How to display flashdata in codeigniter


Following is my view I am catching the data from the model and displaying on the view using flashdata in codeigniter

My Controller cart.php

 public function coupon(){
    for ($i = 0; $i <= $this->input->post("products_in_cart"); $i++) {

        if (!empty($this->input->post("coupn-" . $i))) {

            $couponname = $this->input->post("coupn-" . $i);
            $products_id = $this->input->post("product_id" . $i);

            $data = $this->home_model->getCoupon($couponname, $products_id);
            $data1 = 'hello';
            $info = array(
                "PromotioanlName" => $data->PromotionalName,
            );
        } else {

            $info = 'Thers in no value<br>';
        }



    }
    echo $this->session->set_flashdata('message', $info);
    redirect(site_url('cart'));
}

My view cart.php

 $message = $this->session->flashdata('message');
  print_r($message);

But my problem is that my data is overwritten by the next value


Solution

  • in for loop, you have written if and in if, $info is an array and in else, $info is string!! Thus in loop when condition of if will be true, it'll be worked as an array and it'll be overwritten if condition will be true again in second recurs of loop!! And while condition false, it'll return string that will overwrite your array..

    Try by using, $info[] instead of $info.. May be it solved your problem..

    public function coupon(){
        for ($i = 0; $i <= $this->input->post("products_in_cart"); $i++) {
    
            if (!empty($this->input->post("coupn-" . $i))) {
    
                $couponname = $this->input->post("coupn-" . $i);
                $products_id = $this->input->post("product_id" . $i);
    
                $data = $this->home_model->getCoupon($couponname, $products_id);
                $data1 = 'hello';
                $info[] = array(
                    "PromotioanlName" => $data->PromotionalName,
                );
            } else {
    
                $info[] = 'Thers in no value<br>';
            }
    
    
    
        }
        echo $this->session->set_flashdata('message', $info);
        redirect(site_url('cart'));
    }