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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分