我不能从另一个函数调用变量

时间:2017-03-19 05:13:20

标签: php function codeigniter variables

  

帮助,我想从另一个函数使用变量,但我得到错误

     

我想从这个函数调用一个变量$ nama:

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 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');
}
  

我得到了这样的错误:

An uncaught Exception was encountered

输入:错误

消息:未找到班级“上传”

文件名:C:\ xampp \ htdocs \ percobaan \ e-magang \ application \ controllers \ pendaftaran.php

行号:43

回溯:

文件:C:\ xampp \ htdocs \ percobaan \ e-magang \ index.php 行:315 功能:require_once

  

这是完整的代码:

   <?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);
// }
}

1 个答案:

答案 0 :(得分:1)

上传不是classfunctionclass,名为Pendaftaran。因此,要从同一控制器中的另一个函数调用此函数,请使用$this->function_name()。试试这个..

替换

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

。通过

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

并在upload()方法中:

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;
        }
    }
}