doulong4169
2014-08-23 06:54
浏览 53
已采纳

如何在codeigniter中显示flashdata

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

图片转代码服务由CSDN问答提供 功能建议

以下是我的视图我从模型中捕获数据并使用codeigniter中的 flashshata在视图上显示

My Controller cart.php

  public function coupon(){
 for($ i = 0; $ i&lt; = $ this  - &gt; input-&gt; post(“products_in_cart”); $ i ++){
 
 if(!empty($ this-&gt; input-&gt; post(“coupn-”。$ i))){\  n 
 $ couponname = $ this-&gt; input-&gt; post(“coupn-”。$ i); 
 $ products_id = $ this-&gt; input-&gt; post(“product_id”。$ i);  
 
 $ data = $ this-&gt; home_model-&gt; getCoupon($ couponname,$ products_id); 
 $ data1 ='hello'; 
 $ info = array(
“PromotioanlName”=&gt; $  data-&gt; PromotionalName,
); 
} else {
 
 $ info ='Thers in no value&lt; br&gt;'; 
} 
 
 
 
} 
 echo $ this-  &gt; session-&gt; set_flashdata('message',$ info); 
 redirect(site_url('cart')); 
} 
   
 
 

我的观点 cart.php

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

但我的问题是我的数据被下一个值覆盖

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanlian1320 2014-08-23 07:20
    已采纳

    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'));
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题