douqiu0351 2011-06-16 10:47
浏览 49
已采纳

在codeigniter中加载视图两次

Hello I just started CodeIgniter. I am having problem in loading view.

My scenrio is I am creating a sample add form. After submit it goes to controller and insert entries in database. I want if the database operation is successfull it again comes to same view having again some values. And on the basis of those values I am showing some particular rows informing user about insertion operation. My function in controller looks like

public function add_user()
{
    $this->load->view('add_user');
    $post=$this->input->post();
    if(isset($post['name']))
    {
        $data=array(
        'name'=>$post['name'],
        'designation'=>$post['designation']
        );
        if($this->db->insert('user',$data))
        $result['update']=true;
        else
        $result['update']=false;
        $this->load->view('add_user',$result);
    }

}

And my view looks like

    <h1 align="center">Add User</h1>
    <table border="0" cellpadding="2" cellspacing="2" align="center">
        <?php
        if(isset($update))
        {
            if($update)
            {
                ?>
                <tr bgcolor="#00FF00">
                    <td>Record Added Successfully</td>
                </tr>
            <?php
            }
            else
            {
                ?>
                <tr bgcolor="#FF0000">
                    <td>Insertion Operation Failed</td>
                </tr>
            <?php
            }   
        }
        ?>
        <?php echo(form_open('first/add_user'));?>
            <tr>
                <td>Name</td>
                <td><input type="text" name="name" /></td>
            </tr>
            <tr>
                <td>Designation</td>
                <td>
                    <select name="designation">
                        <option value="Junior PHP Developer">Junior PHP Developer</option>
                        <option value="Senior PHP Developer">Senior PHP Developer</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="submit" value="Add User" />
                </td>
            </tr>   
        </form>
    </table>

Now What I want that if insertion operation is successfull I am sending true value to view and if not I am sending false value. And on the basis of this value I am showing some rows. I am loading the view two times as per I understood the logic. Because first time it loads the form and second time It loads view with some value of true or false. But what happens that after it reloads there are two forms. I know this problem is due to double loading of my view. i want to ask if there is another way of sending values after database operation to view?

  • 写回答

2条回答 默认 最新

  • dongshi9719 2011-06-16 11:03
    关注

    Simply load your view once:

    public function add_user()
    {
      $post=$this->input->post();
      $result = array();
      if(isset($post['name']))
      {
          $data=array(
           'name'=>$post['name'],
           'designation'=>$post['designation']
          );
          if($this->db->insert('user',$data))
          $result['update']=true;
          else
          $result['update']=false;
      }
      $this->load->view('add_user',$result);
    }
    

    By the way your code is a bit messy, work on it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大