Codeigniter - 更新头像

时间:2016-02-24 12:37:23

标签: php codeigniter

请有人帮助我,我试图分开"更新"来自"编辑个人资料的头像"在CI控制器上,在模型/视图上我也做了但是在控制器上我确实喜欢这个,但是当我按下按钮时不会改变头像,

请有人帮助我< 3

控制器代码部分:     #更新配置文件     public function updateprofile()     {

    $this->form_validation->set_rules('first_name', 'First Name',       'required|xss_clean');
    $this->form_validation->set_rules('last_name',  'last Name',        'required|xss_clean');
    $this->form_validation->set_rules('gender',     'Gender',           'required|xss_clean');


    $this->form_validation->set_rules('user_name',  'Username',         'required|callback_username_check|xss_clean');
    $this->form_validation->set_rules('user_email',     'Email',        'required|valid_email|callback_useremail_check|xss_clean');

    if($this->input->post('password')!='' || $this->input->post('confirm_password')!='')
        $this->form_validation->set_rules('password', 'Password', 'required|matches[confirm_password]|min_length[5]|xss_clean');



    if ($this->form_validation->run() == FALSE)
    {
        $action = $this->input->post('action');
        $id = $this->input->post('id');

        if($action=='editprofile')
        $this->editprofile($id);    
        else
        $this->edituser($id);   
    }
    else
    {
        $id = $this->input->post('id');
        if(constant("ENVIRONMENT")=='demo')
        {
            $this->session->set_flashdata('msg', '<div class="alert alert-success">Data updated.[NOT AVAILABLE ON DEMO]</div>');
            redirect(site_url('admin/editprofile/'.$id));   
        }
        else
        {
            $userdata['profile_photo']  = $this->input->post('profile_photo');
            $userdata['first_name']     = $this->input->post('first_name');
            $userdata['last_name']      = $this->input->post('last_name');
            $userdata['gender']         = $this->input->post('gender');
            $userdata['user_name']      = $this->input->post('user_name');
            $userdata['user_email']     = $this->input->post('user_email');

            if($this->input->post('password')!='') 
            {
                $this->load->library('encrypt');
                $userdata['password']   = $this->encrypt->sha1($this->input->post('password'));
            }


            add_user_meta($id,'company_name',$this->input->post('company_name'));
            add_user_meta($id,'phone',$this->input->post('phone'));
            add_user_meta($id,'about_me',$this->input->post('about_me'));
            add_user_meta($id,'fb_profile',$this->input->post('fb_profile'));
            add_user_meta($id,'twitter_profile',$this->input->post('twitter_profile'));
            add_user_meta($id,'li_profile',$this->input->post('li_profile'));
            add_user_meta($id,'gp_profile',$this->input->post('gp_profile'));
            add_user_meta($id,'hide_email',$this->input->post('hide_email'));
            add_user_meta($id,'hide_phone',$this->input->post('hide_phone'));

            $this->admin_model->update_profile($userdata,$id);
            $this->session->set_flashdata('msg', '<div class="alert alert-success">'.lang_key('user_updated').'</div>');

            $action = $this->input->post('action');

            if($action=='editprofile')
                redirect(site_url('admin/editprofile/'.$id));       
            else
                redirect(site_url('admin/edituser/'.$id));      
        }
    }

}

#update avatar 
public function updateavatar()
{

    if ($this->form_validation->run() == FALSE)
    {
        $id = $this->input->post('id');
        if(constant("ENVIRONMENT")=='demo')
        {
            $this->session->set_flashdata('msg', '<div class="alert alert-success">Data updated.[NOT AVAILABLE ON DEMO]</div>');
            redirect(site_url('admin/editavatar/'.$id));    
        }
        else
        {
            $userdata['profile_photo']  = $this->input->post('profile_photo');

            $this->admin_model->update_profile($userdata,$id);
            $this->session->set_flashdata('msg', '<div class="alert alert-success">'.lang_key('user_updated').'</div>');

            $action = $this->input->post('action');

            if($action=='editavatar')
                redirect(site_url('admin/editprofile/'.$id));       
            else
                redirect(site_url('admin/edituser/'.$id));      
        }
    }

}

模型代码部分:

function update_profile($data,$id)
{
    $this->db->update('users',$data,array('id'=>$id));
    if($id==$this->session->userdata('user_id'))
    $this->session->set_userdata('user_name',$data['user_name']);
}

function update_avatar($data,$id)
{
    $this->db->update('users',$data,array('id'=>$id));
    if($id==$this->session->userdata('user_id'))
    $this->session->set_userdata('user_name',$data['user_name']);
}

查看代码部分:

                                            <form role="form" action="<?php echo site_url('admin/updateavatar'); ?>" method="post">
                                            <div class="form-group">
                                                <label class="control-label">&nbsp;</label>
                                                <div class="controls">
                                                <img class="thumbnail" id="user_photo" src="<?php echo get_profile_photo_by_id($profile->id,'thumb');?>"  style="width:100px;" />
                                                <span id="profile_photo_error"><?php echo form_error('profile_photo'); ?></span>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="control-label"><?php echo lang_key('profile_picture'); ?></label>
                                                <div class="controls">
                                                <input type="hidden" name="profile_photo" id="profile_photo" value="<?php echo get_profile_photo_name_by_username($profile->user_name);?>">
                                                <iframe src="<?php echo site_url('admin/users/profile_photo_uploader');?>" style="border:0;margin:0;padding:0;height:130px;"></iframe>
                                                <span class="help-inline">&nbsp;</span>                            
                                                </div>
                                            </div>

                                                <div class="margin-top-10">
                                                    <div class="controls">
                                                    <button class="btn green-haze" type="submit">
                                                    <i class="fa fa-check"></i><?php echo lang_key("update") ?></button>
                                                    </div>
                                                </div>
                                            </form>

1 个答案:

答案 0 :(得分:1)

if ($this->form_validation->run() == TRUE)
    {
        $id = $this->input->post('id'); // FORM MISSES POST ID .SET ID AS HIDDEN
        if(constant("ENVIRONMENT")=='demo')
        {
            $this->session->set_flashdata('msg', '<div class="alert alert-success">Data updated.[NOT AVAILABLE ON DEMO]</div>');
            redirect(site_url('admin/editavatar/'.$id));    
        }
        else
        {
            $userdata['profile_photo']  = $this->input->post('profile_photo');

            $this->admin_model->update_profile($userdata,$id);
            $this->session->set_flashdata('msg', '<div class="alert alert-success">'.lang_key('user_updated').'</div>');

            $action = $this->input->post('action');

            if($action=='editavatar')
                redirect(site_url('admin/editprofile/'.$id));       
            else
                redirect(site_url('admin/edituser/'.$id));      
        }
    }