我想将上传图片代码添加到我的表单中

时间:2019-06-11 03:08:49

标签: php codeigniter

任何人都可以帮助...

这是我的控制器代码(Actions.php)

public function add_admin()
    {
        $data['employee'] = $this->employee_model->get_new();
        $data['department'] = $this->employee_model->dpdown_admins();
        $data['gender'] = $this->employee_model->dpdown_gender();
        $data['status'] = $this->employee_model->dpdown_status();
        $data['attainment'] = $this->employee_model->dpdown_attainment();

        $rules = $this->employee_model->rules['insert'];
        $this->form_validation->set_rules($rules);

        if ($this->form_validation->run() == TRUE)
        {
            $post = $this->employee_model->array_from_post(array(
                                                            'ID_number',
                                                            'employee_no',
                                                            'department_id',
                                                            'department_position',
                                                            'tin_number',
                                                            'sss_number',
                                                            'ph_number',
                                                            'first_name',
                                                            'middle_name',
                                                            'last_name',
                                                            'suffix_name',
                                                            'street',
                                                            'barangay',
                                                            'city',
                                                            'zipcode',
                                                            'birthday',
                                                            'age',
                                                            'gender_id',
                                                            'nationality',
                                                            'religion',
                                                            'status_id',
                                                            'telephone_number',
                                                            'cellphone_number',
                                                            'email',
                                                            'educational_att_id',
                                                            'diploma_title',
                                                            'school_name',
                                                            'year_graduated',
                                                            'company1_name',
                                                            'position1_title',
                                                            'inclusive_dates1',
                                                            'company2_name',
                                                            'position2_title',
                                                            'inclusive_dates2',
                                                            'company3_name',
                                                            'position3_title',
                                                            'inclusive_dates3',
                                                            'date_employed'
                                                           ));

            $data = $this->employee_model->save($post);
            return self::redirect_to('human_resource/admin', '<b>Success</b><br>New Admin Employee has been successfully Registered.');
        }

        $data['error'] = validation_errors('<li class="error">', '</li>');

        $data['content'] = 'human_resource/actions/add_admin';
        return $this->load->view('templates', $data);
    }

然后这就是我的模型的样子(employee_model.php) 这些是我的规则

public $rules = array('insert' => array(
                                        'department_position' => array(
                                                        'field' => 'department_position', 
                                                        'label' => 'Employee Position', 
                                                        'rules' => 'trim|required',
                                                        'errors' => array('required' => 'This Field is Required')),
                                        'ID_number' => array(
                                                        'field' => 'ID_number', 
                                                        'label' => 'ID Number', 
                                                        'rules' => 'trim|required',
                                                        'errors' => array('required' => 'This Field is Required')),
                                        'employee_no' => array(
                                                        'field' => 'employee_no', 
                                                        'label' => 'Employee Number', 
                                                        'rules' => 'trim|required',
                                                        'errors' => array('required' => 'This Field is Required')),
                                        'tin_number' => array(
                                                        'field' => 'tin_number', 
                                                        'label' => 'Tin Number', 
                                                        'rules' => 'trim|required',
                                                        'errors' => array('required' => 'This Field is Required')),
                                        'sss_number' => array(
                                                        'field' => 'sss_number', 
                                                        'label' => 'SSS Number', 
                                                        'rules' => 'trim|required',
                                                        'errors' => array('required' => 'This Field is Required')),
                                        'ph_number' => array(
                                                        'field' => 'ph_number', 
                                                        'label' => 'Phil Health Number', 
                                                        'rules' => 'trim|required',
                                                        'errors' => array('required' => 'This Field is Required')),));

然后这是我的保存功能,仍在同一模型中

public function save($post, $employee_id = NULL)
    {
        if($this->timestamp == TRUE)
        {
            $now = date('Y-m-d H:i:s');
            $employee_id || $post['created_at'] = $now;
            $post['updated_at'] = $now;
        }

        if($employee_id === NULL)
        {
            !isset($post[$this->primary_key]) || $post[$this->primary_key] = NULL;

            $this->db->insert($this->table_name, $post);
            $employee_id = $this->db->insert_id();
        }else{
            $filter = $this->filter;
            $this->db->where($this->primary_key, $filter($employee_id))
                     ->update($this->table_name, $post);
        }
    }

然后这是我的视图表格 我并没有将所有内容仅放在视图代码的1/4

<div class="main-content">
    <div class="main-content-inner">
        <div class="breadcrumbs ace-save-state" id="breadcrumbs">
            <ul class="breadcrumb">
                <li>
                    <i class="ace-icon fa fa-tachometer home-icon"></i>
                    <a href="<?php echo base_url('main/human_resource'); ?>">Dashboard</a>
                </li>

                <li>
                    <i class="ace-icon fa fa-user home-icon"></i>
                    <a href="<?php echo base_url('human_resource/admin'); ?>">Administration Table</a>
                </li>

                <li>
                    <a href="#">New Admin Form</a>
                </li>
                <li class="active">Application Form</li>
            </ul>
        </div>
        <!-- /end of breadcrumb -->         

        <div class="page-content">
            <div class="page-header">
                <h1>
                    <i class="fa fa-user-plus"></i>
                    New Admin Form
                    <small>
                        <i class="ace-icon fa fa-angle-double-right"></i>
                        Application Form
                    </small>
                </h1>
            </div>
            <!-- end of page header -->

            <?php echo form_open(NULL, array('class' => 'form-horizontal', 'role' => 'form'));?>
            <div class="row">
                <div class="col-xs-12">
                    <!-- table content -->
                    <div class="widget-box">
                        <div class="widget-header widget-header-blue widget-header-flat">
                            <h4 class="widget-title lighter">Form Wizard</h4>
                        </div>
                        <!-- end of widget header    -->

                        <!-- R-1 -->
                        <div class="widget-body">
                            <div class="widget-main"  style="padding-bottom: 0;">
                                        <h4 class="lighter block green">Employee General Information
                                            <span class="help-button" style="background-color: red;" data-rel="popover" data-trigger="hover" data-placement="right" data-content="If a field is not applicable please type N/A" title="Notice!">
                                                <i class="fa fa-exclamation"></i>
                                            </span>
                                        </h4>

                                        <div class="row">
                                            <!-- B-1 col -->
                                            <div class="col-xs-12 col-sm-4">
                                                <div class="widget-box">
                                                    <div class="widget-header">
                                                        <h5 class="widget-title">General Information</h5>   
                                                    </div>
                                                    <!-- end of widget header -->

                                                    <div class="widget-body">
                                                        <div class="widget-main">
                                                            <!-- employee information B-1-->
                                                            <!-- first name -->
                                                            <?php echo form_label('First Name', 'first_name', array('class' => 'control-label'));?>
                                                            <span style="font-weight: bolder; color: red;">*</span>
                                                            <div class="form-group <?php if (form_error('first_name')==TRUE){echo 'has-error';} ?>" style="margin-bottom: 0;">
                                                                <div class="col-sm-12">
                                                                    <?php echo form_input('first_name', set_value('first_name', $employee->first_name),
                                                                                    array(  'type' => 'text',
                                                                                            'name' => 'first_name', 
                                                                                            'id' => 'firstname', 
                                                                                            'class' => 'form-control')); ?>
                                                                </div>
                                                            </div>
                                                            <!-- validation -->
                                                            <?php if (!empty($error)): ?>
                                                                <div class="has-error" style="margin-bottom: 0;">
                                                                    <div class="help-block col-xs-12 col-sm-reset inline" 
                                                                         style="margin-bottom: 0; font-size: 12px;"> 
                                                                        <?php echo form_error('first_name'); ?>
                                                                    </div>
                                                                </div>
                                                            <?php endif ?>
                                                            <!-- last name -->
                                                            <?php echo form_label('Last Name', 'last_name', array('class' => 'control-label'));?>
                                                            <span style="font-weight: bolder; color: red;">*</span>
                                                            <div class="form-group <?php if (form_error('last_name')==TRUE){echo 'has-error';} ?>" style="margin-bottom: 0;">
                                                                <div class="col-sm-12">
                                                                    <?php echo form_input('last_name', set_value('last_name', $employee->last_name),
                                                                                    array(  'type' => 'text',
                                                                                            'name' => 'last_name', 
                                                                                            'id' => 'lastname', 
                                                                                            'class' => 'form-control'));?>
                                                                </div>
                                                            </div>
                                                            <!-- validation -->
                                                            <?php if (!empty($error)): ?>
                                                                <div class="has-error" style="margin-bottom: 0;">
                                                                    <div class="help-block col-xs-12 col-sm-reset inline" 
                                                                         style="margin-bottom: 0; font-size: 12px;"> 
                                                                        <?php echo form_error('last_name'); ?>
                                                                    </div>
                                                                </div>
                                                            <?php endif ?>
                                                            <!-- middle name -->
                                                            <?php echo form_label('Middle Name', 'middle_name', array('class' => 'control-label'));?>
                                                            <span style="font-weight: bolder; color: red;">*</span>
                                                            <div class="form-group <?php if (form_error('middle_name')==TRUE){echo 'has-error';} ?>" style="margin-bottom: 0;">
                                                                <div class="col-sm-12">
                                                                    <?php echo form_input('middle_name', set_value('middle_name', $employee->middle_name),
                                                                                    array(  'type' => 'text',
                                                                                            'name' => 'middle_name', 
                                                                                            'id' => 'middlename', 
                                                                                            'class' => 'form-control'));?>
                                                                </div>
                                                            </div>
                                                            <!-- validation -->
                                                            <?php if (!empty($error)): ?>
                                                                <div class="has-error" style="margin-bottom: 0;">
                                                                    <div class="help-block col-xs-12 col-sm-reset inline" 
                                                                         style="margin-bottom: 0; font-size: 12px;"> 
                                                                        <?php echo form_error('middle_name'); ?>
                                                                    </div>
                                                                </div>
                                                            <?php endif ?>
                                                            <!-- suffix name -->
                                                            <?php echo form_label('Suffix', 'suffix_name', array('class' => 'control-label'));?>
                                                            <div class="form-group" style="margin-bottom: 0;">
                                                                <div class="col-sm-6">
                                                                    <?php echo form_input('suffix_name', set_value('suffix_name', $employee->suffix_name),
                                                                                    array(  'type' => 'text',
                                                                                            'name' => 'suffix_name', 
                                                                                            'id' => 'suffixname', 
                                                                                            'class' => 'form-control'));?>
                                                                </div>
                                                            </div>
                                                            <hr>
                                                        </div>
                                                    </div>

                                                </div>
                                            </div>
                                            <!-- end of B-1 col -->

请任何我确实需要您帮助的人

1 个答案:

答案 0 :(得分:0)

您需要使用form_open_multipart

  

此函数与form_open()...完全相同,除了   它添加了一个multipart属性,如果您想   使用该表格上传文件。

参考:https://www.codeigniter.com/user_guide/helpers/form_helper.html