1

Getting undefined variable error when calling a static method.

I'm new to coding. please be kind.

I'm trying to dynamically display an event page. It has $title, $price, $location, etc. with a table in the database titled Onlineevent.

I wanted to add a photo gallery to this event page and thought it would be better to have a new table (event_gallery) with columns (id, event_id, and image_name). The event_id is a foreign key from the Onlineevent table.

I'm having no problem calling the Onlineevent data from the database with the method (findy_by_id()). However I cannot call the method that relates to the event_gallery. Please refer to the code.

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


    ?>

Now I'll share the classes.

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.',

);

This is passing $_FILES['uploaded_file'] as an argument

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


    }


}

Here is the second class

<?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.',

);

This is passing $_FILES['uploaded_file'] as an argument

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


    }


}

I would like to be able to call the static function Eventgallery::find_by_id(); so that I can access the data and then print it out on the event page.

Thank you

more
  • 125
  • 3
  • What's the error you're getting? – Bram Verstraten Jul 29 '19 at 06:36
  • Notice: Undefined variable: image in /Applications/MAMP/htdocs/gsm/event-single.php on line 134 – David Woodworth Jul 29 '19 at 07:17
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – DarkBee Jul 29 '19 at 07:30

1 Answers1

0

When the if statement fails, the $image variable is not defined.

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

echo $image;

You can solve this by declaring it first.

$image = '';

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

echo $image;
Bram Verstraten
  • 1,414
  • 11
  • 24
  • Thank you for this, but I found the problem and it was in information I didn't share. My id columns were not titled correctly and I was passing in the wrong id which made it fail. :( – David Woodworth Jul 29 '19 at 07:54