插入成功消息后的 Codeigniter 显示在视图中

Codeigniter after insert success message show in view

我是 Codeigniter 的新手,我需要在 insert 数据进入数据库后显示成功和错误消息。

如何在 view 页面中显示消息?

这是我的编码:

型号

function addnewproducts($data)
{
    if($data['product_name']!="" && $data['product_qty']!="" && $data['product_price']!="" && $data['date']!="")    
    {
        $res=$this->db->insert('product_list',$data);
        return $this->db->insert_id();
    }
    else
    {
        return false;
    }
}

控制器

function addnewproduct()
    {
        $this->load->model('products');
        $data['product_name'] = trim(strip_tags(addslashes($this->input->post('product_name'))));
        $data['product_qty'] = trim(strip_tags(addslashes($this->input->post('product_qty'))));
        $data['product_price'] = trim(strip_tags(addslashes($this->input->post('product_price'))));
        $data['datetime']=date('d-m-Y');
        $res = $this->products->addnewproducts($data);
        if($res==true)
        {
            $data['success'] = 'Successful';
            $this->load->view('addproduct',$data);
        }

    }

查看

<p><?php echo $success; ?></p>

有很多方法,但我推荐以下方法:

成功或错误时在控制器中设置临时会话

$res = $this->products->addnewproducts($data);
if($res==true)
{
    $this->session->set_flashdata('success', "SUCCESS_MESSAGE_HERE"); 
}else{
    $this->session->set_flashdata('error', "ERROR_MESSAGE_HERE");
}

在视图中你可以显示如下的flashdata:

echo $this->session->flashdata('success');
or 
echo $this->session->flashdata('error');

来源:Codeigniter官网https://codeigniter.com/userguide3/libraries/sessions.html

我很感激你得到了你的答案,但我认为闪存数据现在有点旧了,因为我们可以使用 bootstrap 来提醒是否有任何错误并且在网页上看起来不错。

In controller

$res = $this->products->addnewproducts($data);
if($res==true)
{
  $this->session->set_flashdata('true', 'write_the_message_you_want');
}
else
{
  $this->session->set_flashdata('err', "write_the_message_you_want");
}

In View

<?php 
   if($this->session->flashdata('true')){
 ?>
   <div class="alert alert-success"> 
     <?php  echo $this->session->flashdata('true'); ?>
<?php    
} else if($this->session->flashdata('err')){
?>
 <div class = "alert alert-success">
   <?php echo $this->session->flashdata('err'); ?>
 </div>
<?php } ?>

控制器:

function addnewproduct()
{
    $this->load->model('products');
    $data['product_name'] = trim(strip_tags(addslashes($this->input->post('product_name'))));
    $data['product_qty'] = trim(strip_tags(addslashes($this->input->post('product_qty'))));
    $data['product_price'] = trim(strip_tags(addslashes($this->input->post('product_price'))));
    $data['datetime']=date('d-m-Y');
    if($this->products->addnewproducts($data));
    {
        $this->session->set_flashdata('Successfully','Product is Successfully Inserted');
    }
    else
    {
       $this->session->set_flashdata('Successfully','Failed To 
        inserted Product');
    }
     // redirect page were u want to show this massage.
        redirect('Controller/Fucntion_name','refresh');
}// close function

观点: 在重定向页面上,将此代码写在 Form

顶部
    <?php if($responce = $this->session->flashdata('Successfully')): ?>
      <div class="box-header">
        <div class="col-lg-6">
           <div class="alert alert-success"><?php echo $responce;?></div>
        </div>
      </div>
    <?php endif;?>