簡體   English   中英

調用類方法時未定義的變量

[英]Undefined variable when calling a class method

調用靜態方法時出現未定義的變量錯誤。

我是編碼新手。 請客氣。

我正在嘗試動態顯示事件頁面。 它具有$title$price$location等,並在數據庫中有一個名為Onlineevent的表。

我想向此事件頁面添加一個圖片庫,並認為最好有一個新表( event_gallery ),其中包含列( idevent_idimage_name )。 event_idOnlineevent表中的外鍵。

我可以使用( findy_by_id() )方法從數據庫調用Onlineevent數據。 但是,我無法調用與event_gallery相關的方法。 請參考代碼。

<?php 

     $event = Onlineevent::find_by_id($_GET['id']); 

    if($event){


    $event_title = $event->event_title;
    $event_type = $event->event_type;
    $event_location = $event->event_location;
    $event_date = $event->event_date;
    $event_start_time = $event->event_start_time; 
    $event_finish_time = $event->event_finish_time; 
    $max_participants = $event->max_participants; 
    $event_price = $event->event_price; 
    $event_description = $event->event_description; 
    $event_picture = $event->picture_path();
    $event_inclusion_1 = $event->inclusion_1;
    $event_inclusion_2 = $event->inclusion_2;
    $event_inclusion_3 = $event->inclusion_3;
    $event_inclusion_4 = $event->inclusion_4;
    $event_inclusion_5 = $event->inclusion_5;
    $event_inclusion_6 = $event->inclusion_6;
    $event_inclusion_7 = $event->inclusion_7;
    $event_inclusion_8 = $event->inclusion_8;

    }


 $images = Eventgallery::find_by_id($_GET['id']);

    if($images){

        $image = $images->picture_path();
    }

            echo $image;


    ?>

現在,我將分享課程。

class Onlineevent extends Db_object{



protected static $db_table = "onlineevent";
protected static $db_table_fields = array('event_type','event_title','event_picture', 'event_location','event_date','event_start_time','event_finish_time','max_participants','event_price','event_description','event_map','inclusion_1','inclusion_2','inclusion_3','inclusion_4','inclusion_5','inclusion_6','inclusion_7','inclusion_8','inclusion_9','inclusion_10');

public $id;
public $event_type;
public $event_title;
public $event_picture;
public $event_location;
public $event_date;
public $event_start_time;
public $event_finish_time;
public $event_koreans;
public $max_participants;
public $event_foreigners;
public $event_price;
public $event_description;
public $event_map;
public $inclusion_1;
public $inclusion_2;
public $inclusion_3;
public $inclusion_4;
public $inclusion_5;
public $inclusion_6;
public $inclusion_7;
public $inclusion_8;
public $inclusion_9;
public $inclusion_10;



public $tmp_path;
public $upload_directory = "images";
public $errors = array();

public $upload_errors_array = array(
0 => 'There is no error, the file uploaded with success',
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',

);

這會將$ _FILES ['uploaded_file']作為參數傳遞

public function set_file($file) {

    if(empty($file) || !$file || !is_array($file)) {

        $this->errors[] = "There was no file uploaded here";
        return false; 

    } elseif($file['error'] !=0){

        $this->error[] = $this->upload_errors_array[$file['error']];
        return false;
    } else {

    $this->event_picture = basename($file['name']);
    $this->tmp_path = $file['tmp_name'];
    $this->type     = $file['type'];
    $this->size     = $file['size'];




    }



}

public function picture_path(){

    return $this->upload_directory.DS.$this->event_picture;
}



public function save(){

    if($this->id){

        $this->update();

    } else {

        if(!empty($this->errors)){

            return false;
        }

        if(empty($this->event_picture) || empty($this->tmp_path)){
            $this->errors[] = "the file was not available";
            return false;
        }

        $target_path = SITE_ROOT .DS. 'admin'.DS. $this->upload_directory. DS . $this->event_picture;

        if(move_uploaded_file($this->tmp_path, $target_path)){
            if($this->create()){

                unset($this->tmp_path);

                return true;
            }

        } else {

            $this->errors[] = "the folder probably does have permission";
            return false; 
        }


    }


}

這是第二節課

<?php

class Eventgallery extends Db_object{



protected static $db_table = "event_gallery";
protected static $db_table_fields = array('event_id','image_name');

public $id;
public $event_id;
public $image_name;


public $tmp_path;
public $upload_directory = "images";
public $errors = array();
//    public $allowTypes = array('jpg','png','jpeg','gif');

public $upload_errors_array = array(
0 => 'There is no error, the file uploaded with success',
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',

);

這會將$ _FILES ['uploaded_file']作為參數傳遞

public function set_file($file) {

    if(empty($file) || !$file || !is_array($file)) {

        $this->errors[] = "There was no file uploaded here";
        return false; 

    } elseif($file['error'] !=0){

        $this->error[] = $this->upload_errors_array[$file['error']];
        return false;
    } else {

    $this->image_name = basename($file['name']);
    $this->tmp_path = $file['tmp_name'];
    $this->type     = $file['type'];
    $this->size     = $file['size'];




    }



}

public function picture_path(){

    return $this->upload_directory.DS.$this->image_name;
}



public function save(){

    if($this->id){

        $this->update();

    } else {

        if(!empty($this->errors)){

            return false;
        }

        if(empty($this->image_name) || empty($this->tmp_path)){
            $this->errors[] = "the file was not available";
            return false;
        }

        $target_path = SITE_ROOT .DS. 'admin'.DS. $this->upload_directory. DS . $this->image_name;

        if(move_uploaded_file($this->tmp_path, $target_path)){
            if($this->create()){

                unset($this->tmp_path);

                return true;
            }

        } else {

            $this->errors[] = "the folder probably does have permission";
            return false; 
        }


    }


}

我希望能夠調用靜態函數Eventgallery::find_by_id(); 這樣我就可以訪問數據,然后在事件頁面上將其打印出來。

謝謝

如果if語句失敗,則不會定義$image變量。

if ($images) {
    $image = $images->picture_path();
}

echo $image;

您可以通過先聲明它來解決。

$image = '';

if ($images) {
    $image = $images->picture_path();
}

echo $image;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM