Firm
Firm

Reputation: 61

i can't call variable from another function

help, i want to use variable from another function but i get error

i want call a variable $nama from this function :

public function upload(){
    if(!empty($_FILES))
    {
        $config["upload_path"] = "./uploads/images";
        $config["allowed_types"] = "gif|jpg|png";
        $this->load->library('upload', $config);

        if($this->upload->do_upload("file")){
            $nama=$this->upload->data('file_name');
            return $nama;
        }
        else if(!$this->upload->do_upload("file")){
            echo "failed to upload file(s)";
        }
    }
}

and i want call it to this function :

public function daftar()
{

    if(isset($_POST['pendaftaran'])){

        //if form validation true
        if($this->form_validation->run() == TRUE){
            echo 'form validated';


            //foto
            $obj = new upload();
            $nama_foto = $obj->$nama;


            $data = array(
                'nama_foto' => $nama_foto,
                'email' => $email
                );

            $this->db->insert('foto',$data);

            $this->session->set_flashdata("success","your account has been created");
            redirect('pendaftaran/daftar','refresh');

        }
    }

    $this->load->view('view_pendaftaran');
    $this->load->view('footer');
}

i got this error like this :

An uncaught Exception was encountered

Type: Error

Message: Class 'upload' not found

Filename: C:\xampp\htdocs\percobaan\e-magang\application\controllers\pendaftaran.php

Line Number: 43

Backtrace:

File: C:\xampp\htdocs\percobaan\e-magang\index.php Line: 315 Function: require_once

this is the full code :

   <?php
     defined('BASEPATH') OR exit('No direct script access allowed');

  class Pendaftaran extends CI_Controller {

// private $upload_path = "./uploads/images";



public function daftar()
{

    if(isset($_POST['pendaftaran'])){
        $this->form_validation->set_rules('email','Email','required|is_unique[user.email]');
        $this->form_validation->set_rules('password','password','required|min_length[5]');
        $this->form_validation->set_rules('password2','konformasi password','required|min_length[5]|matches[password]');
        $this->form_validation->set_rules('nomor_induk','Nomor Induk Siswa / Mahasiswa','required|is_unique[pendidikan.no_induk]');


        //if form validation true
        if($this->form_validation->run() == TRUE){
            echo 'form validated';
            //data akun
            $email = $this->input->post('email');
            $pass = md5($this->input->post('password'));

            //data pribadi
            $nama = $this->input->post('nama_lengkap');
            $alamat = $this->input->post('alamat');
            $tgl_lhr = $this->input->post('date');
            $no_telp = $this->input->post('no_telp');

            //data pendidikan
            $tingkat_pendidikan = $this->input->post('tingkat_pendidikan');
            $nama_institusi = $this->input->post('namainstitusi');
            $jurusan = $this->input->post('jurusan');
            $no_induk = $this->input->post('nomor_induk');
            $nilai = $this->input->post('nilai');
            $no_telpon_institusi = $this->input->post('no_institusi');


            //foto
            $obj = new upload();
            $nama_foto = $obj->$nama;



            $data = array(
                'email' => $email,
                'password' => $pass
            );

            $data2 = array(
                'nama_lengkap' => $nama,
                'tanggal_lahir' => $tgl_lhr,
                'alamat' => $alamat,
                'no_telpon' => $no_telp,
                'email' => $email
                );

            $data3 = array(
                'tingkat_pendidikan' => $tingkat_pendidikan,
                'nama_institusi' => $nama_institusi,
                'jurusan' => $jurusan,
                'no_induk' => $no_induk,
                'nilai' => $nilai,
                'no_telpon_institusi' => $no_telpon_institusi,
                'email' => $email
                );

            $data4 = array(
                'nama_foto' => $nama_foto,
                'email' => $email
                );


            $this->db->insert('user',$data);
            $this->db->insert('data_pribadi',$data2);
            $this->db->insert('pendidikan',$data3);
            $this->db->insert('foto',$data4);

            $this->session->set_flashdata("success","your account has been created");
            redirect('pendaftaran/daftar','refresh');

        }
    }

    $this->load->view('view_pendaftaran');
    $this->load->view('footer');
}

public function upload(){
    if(!empty($_FILES))
    {
        $config["upload_path"] = "./uploads/images";
        $config["allowed_types"] = "gif|jpg|png";
        $this->load->library('upload', $config);

        if($this->upload->do_upload("file")){
            $nama=$this->upload->data('file_name');
            return $nama;
        }
        else if(!$this->upload->do_upload("file")){
            echo "failed to upload file(s)";
        }
    }
}

public function remove()
{
    $file = $this->input->post("file");
    if ($file && file_exists($this->upload_path . "/" . $file)) {
        unlink($this->upload_path . "/" . $file);
    }
}

// public function list_files()
// {
//  $this->load->helper("file");
//  $files = get_filenames($this->upload_path);
//  // we need name and size for dropzone mockfile
//  foreach ($files as &$file) {
//      $file = array(
//          'name' => $file,
//          'size' => filesize($this->upload_path . "/" . $file)
//      );
//  }

//  header("Content-type: text/json");
//  header("Content-type: application/json");
//  echo json_encode($files);
// }
}

Upvotes: 0

Views: 57

Answers (1)

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

upload is not a class it is a function of a class named as Pendaftaran. So to call this function from another function within the same controller use $this->function_name(). Try like this..

Replace

   $obj = new upload();
   $nama_foto = $obj->$nama;

With

$nama_foto = $this->upload();//calls upload function
echo $nama_foto;//test photo name here

And in upload() method:

public function upload(){
    if(!empty($_FILES))
    {
        $config["upload_path"] = "./uploads/images";
        $config["allowed_types"] = "gif|jpg|png";
        $this->load->library('upload', $config);

        if($this->upload->do_upload("file")){
            $nama=$this->upload->data('file_name');
            return $nama;
        }
        else if(!$this->upload->do_upload("file")){
            echo "failed to upload file(s)";
            exit;
        }
    }
}

Upvotes: 1

Related Questions