0

I have a field of my form (which is uploading personal picture). So the user selects image from pc and submit the form.

Controller:

function do_upload()
{
    $config['upload_path']   = './images/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '100000';
    $config['max_width']     = '10240';
    $config['max_height']    = '7680';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        print_r($this->upload->data());
        $datafoto = $this->upload->data();
        $nm_file = time().$datafoto['orig_name'];
        $this->load->model('mkegiatan');
        $this->mkegiatan->update_foto($nm_file);
        copy('images/'.$datafoto['orig_name'], 'images/'.$nm_file);
    }
}

And contoller edit event like this:

  function edit_kegiatan()
        {

            $id_kegiatan = $this->uri->segment(4);

            //set validation properties

            $this->form_validation->set_rules('tanggal_kegiatan', 'Tanggal', 'required');
            $this->form_validation->set_rules('nama_kegiatan', 'Judul Berita', 'required');
            $this->form_validation->set_rules('content', 'Content', 'required');


            //run validation
            // jika dia ingin update data atau form validation error
            if ($this->form_validation->run() == FALSE) {

                $data  = $this->mkegiatan->get_by_id($id_kegiatan);
                $this->load->model('mkegiatan');
                $config['upload_path'] = './images/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '100000';
                $config['max_width']  = '10240';
                $config['max_height']  = '7680';

                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload('userfile'))       //you forgot this
                {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view('upload_form', $error);
                }
                else
                {
                    $data = array('upload_data' => $this->upload->data());
                    //print_r($this->upload->data());
                    $datafoto=$this->upload->data();
                    $nm_file = time().$datafoto['orig_name'];
                   $data = array(
                        'tanggal_kegiatan' => $this->input->post('tanggal_kegiatan'),
                        'nama_kegiatan' => $this->input->post('nama_kegiatan'),
                        'content' => $this->input->post('content'),
                        'image' => $nm_file
                );
                $this->mkegiatan->update_kegiatan($id_kegiatan,$data);
                $this->session->set_flashdata('message', generateSuccessMessage('Data berhasil diupdate'));
                redirect(site_url('admin/kegiatan'));
                }
            }

            $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));



            //var_dump($data);
            //$tmp_data = array('id_kegiatan' => $id_kegiatan);

            $this->data['contents'] = $this->load->view('admin/kegiatan/edit_kegiatan', $this->data, true);
            $this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
    }

View:

<?php echo form_open(current_url(),'name=form'); ?>
                        <div class="fours fields">
                            <div class="field">
                                <div class="ui vertical segment">
                                    <div class="date field">
                                        <label>Tanggal</label>
                                        <div class="ui small icon input left">
                                            <input type="text" id="datepicker" placeholder="xxxx-xx-xx" name="tanggal_kegiatan" value="<?php echo $tanggal_kegiatan;?>"><?php echo form_error('tanggal_kegiatan', '<div class="ui red pointing label">', '</div>'); ?>
                                            <i class="calendar icon"></i>
                                          </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="two fields">
                            <div class="field">
                                <label>Nama Acara</label>
                                <div class="ui small left icon input">
                                    <input type="text" placeholder="Nama Kegiatan" name="nama_kegiatan" value="<?php echo $nama_kegiatan;?>"><?php echo form_error('nama_kegiatan', '<div class="ui red pointing label">', '</div>'); ?>
                                    <i class="text file outline icon"></i>
                                </div>
                            </div>
                        </div>

                        <div class="field">
                            <label>Isi Kegiatan</label>
                            <textarea placeholder="Text" name="content">
                                 <?php echo $content;?>
                            </textarea><?php echo form_error('content', '<div class="ui red pointing label">', '</div>'); ?>
                        </div>

                        <input type="file" name="userfile" size="20">

                    <input class="ui small blue submit button" type="submit" value="Save">
                    </form>

Do I need to create two separate forms to accomplish this (one for the image upload and one for the text input)? Or is it possible to write a function in the controller that can validate and process both the upload and text input simultaneously?

1 Answer 1

6

try this

    function tambah_kegiatan()
{
    //set validation properties
    $this->form_validation->set_rules('nama_kegiatan', 'Judul Berita', 'required');


    if ($this->form_validation->run() == true) 
    {
       $this->load->model('mkegiatan');
        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100000';
        $config['max_width']  = '10240';
        $config['max_height']  = '7680';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('userfile'))       //you forgot this
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            print_r($this->upload->data());
            $datafoto=$this->upload->data();
            $nm_file = time().$datafoto['orig_name'];
           $data = array(
                'nama_kegiatan' => $this->input->post('nama_kegiatan'),
                'image' => $nm_file
        );

        $this->mkegiatan->insert_kegiatan($data);
        $this->session->set_flashdata('message', generateSuccessMessage('Data berhasil ditambah'));
        redirect(site_url('admin/kegiatan'));
        }


    }

    $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
    $this->data['contents'] = $this->load->view('admin/kegiatan/tambah_kegiatan', '', true);
    $this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}

and your image folder having all permissions

 function edit_kegiatan($id_kegiatan='')
        {



            //set validation properties

            $this->form_validation->set_rules('tanggal_kegiatan', 'Tanggal', 'required');
            $this->form_validation->set_rules('nama_kegiatan', 'Judul Berita', 'required');
            $this->form_validation->set_rules('content', 'Content', 'required');


            //run validation
            // jika dia ingin update data atau form validation error
            if ($this->form_validation->run() == FALSE) {

                $data  = $this->mkegiatan->get_by_id($id_kegiatan);
                $this->load->model('mkegiatan');
                $config['upload_path'] = './images/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '100000';
                $config['max_width']  = '10240';
                $config['max_height']  = '7680';

                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload('userfile'))       //you forgot this
                {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view('upload_form', $error);
                }
                else
                {
                    $data = array('upload_data' => $this->upload->data());
                    //print_r($this->upload->data());
                    $datafoto=$this->upload->data();
                    $nm_file = time().$datafoto['orig_name'];
                   $data = array(
                        'tanggal_kegiatan' => $this->input->post('tanggal_kegiatan'),
                        'nama_kegiatan' => $this->input->post('nama_kegiatan'),
                        'content' => $this->input->post('content'),
                        'image' => $nm_file
                );
                $this->mkegiatan->update_kegiatan($id_kegiatan,$data);
                $this->session->set_flashdata('message', generateSuccessMessage('Data berhasil diupdate'));
                redirect(site_url('admin/kegiatan'));
                }
            }

            $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));



            //var_dump($data);
            //$tmp_data = array('id_kegiatan' => $id_kegiatan);

            $this->data['contents'] = $this->load->view('admin/kegiatan/edit_kegiatan', $this->data, true);
            $this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
    }
Sign up to request clarification or add additional context in comments.

12 Comments

can you help me one more again? you can see in below i already edit and i put controller edit_kegiatan. i try edit but error Undefined variable: nama_kegiatan. how about that?
can you give me code of your view which is for edit?
what is current_url()
i think you not passing $id_kegiatan in url
take a look,your $id value should not be empty..:)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.