1

I am building a webpage for an photographer and this page contains a bunch of galleries.

I have connected my page to the Database and I can return everything i need from database and place it inside Gallery objects.

The problem lies here:
The webpage should be able to load all the galleries and create an Gallery object for each. It should place it in an array called galleries (this part works fine though).

Next, i want to get data from the objects via a getter, but when i try this, i get the following error :

Fatal error: Call to undefined function getTitle() in C:\xampp\htdocs\index.php on line 21'

I am stuck at this point, is there any one with an solution?

By the way, This is my code :

index.php

<?php

    // REQUIRED FILES

    require_once('PHPclasses/database.class.php');


    $database = new Database('localhost','root','','localdatabase');

    include('PHPclasses/gallery.class.php');

    $results = $database->doQuery("SELECT * FROM gallery");

    $galleries = array();

    for($i = 0; $i < count($results); $i++) {
        $galleries[$i] = new Gallery($i+1, $database);
    }

    foreach($galleries as $gallery) {
        $gallery.getTitle();
    }
?>

database.class.php

<?php

/*
 * This class contains the connection with the database.
 *
 * The database connection will be made when instantiating a new database object via 'new Database()'.
 * This is done by providing the 4 parameters:
 *      - server host
 *      - database user
 *      - database password
 *      - database name
 *
 * The database object can be used to input a query via the doQuery method.
 * This method needs the SQL query as a String, It will return an 2D array, being filled with result index as first 
 *      and the database-table contents s second.
 */

class Database 
{
    // Database connection variables
    private $serverhost;
    private $username;
    private $password;
    private $database;

    // Database connection itself
    private $db_link;

    // Query results
    private $resultsArray;

    public function __construct($host, $user, $pass, $db) {

        $this->serverhost = $host;
        $this->username = $user;
        $this->password = $pass;
        $this->database = $db;

        // Create connection
        $this->db_link = new mysqli(
                $this->serverhost,
                $this->username,
                $this->password,
                $this->database
        );

        // Check for errors in connection
        if ($this->db_link->connect_error) {
            die("Connection failed: " . $this->db_link->connect_error);
        }
    }

    public function doQuery($query){
        $q = $this->db_link->query($query)
            or die("Error: ".mysqli_error($this->db_link));

        $i = 0;
        $resultsArray = array();

        while($row = mysqli_fetch_array( $q )) { 
            $resultsArray[$i] = $row;
            $i++;
        }

        return $resultsArray;   
    }
}

?>

And last but not least gallery.class.php

<?php

/*
 * This class contains the Gallery
 *
 * The gallery object is one instance of the complete gallery. 
 * The ID given in the constructor is the ID within the database,
 * 
 * This row in the database contains all the properties for each object of gallery
 *
 * All properties set in the constructor can later be obtained via the getters.
 */

class Gallery
{

    // location of the gallery folder
    private $root;

    // descriptive variables
    private $title;
    private $description;
    private $thumb;
    private $genre;
    private $day;

    /**
     * Constructor if this class
     * 
     * Takes in an identifier which should be the ID of the gallery in the database
     *      and an database object, which should be the database which stores all the data
     */
    public function __construct($galleryIdentifier, $database) {
        $result = $database->doQuery("SELECT * FROM  `gallery` WHERE `galleryID` = '{$galleryIdentifier}'");

        $this->root = $result[0]["galleryRoot"];

        $this->title = $result[0]["title"];
        $this->description = $result[0]["description"];
        $this->thumb = $result[0]["galleryThumb"];
        $this->genre = $result[0]["genre"];
        $this->day = $result[0]["galleryRoot"];
    }

    // list of getters (start)
    public function getRoot() {
        return $this->root;
    }
    public function getTitle() {
        return $this->title;
    }
    public function getDescription() {
        return $this->description;
    }
    public function getThumb() {
        return $this->thumb;
    }
    public function getGenre() {
        return $this->genre;
    }
    public function getDate() {
        return $this->day;
    }
    // list of getters (end)
}

?>
4
  • 1
    You have a typo error, correct is $gallery->getTitle(); Commented Jan 16, 2015 at 16:27
  • Thank you for your reply. I was programming java for half a year, got things mixed up. thnx again! Commented Jan 16, 2015 at 16:38
  • @BrainFooLong I don't understand why people post answers in comments ;) Commented Jan 16, 2015 at 16:45
  • @Tek Well good question, was maybe a mistake by myself, or maybe i'm a bit weird. It is as it is :) I'll remember this when i write an answer next time. Commented Jan 16, 2015 at 19:23

1 Answer 1

3

You made a typo. Fix your method call to

$gallery->getTitle();
Sign up to request clarification or add additional context in comments.

3 Comments

Also, (unrelated to your problem) instead of performing a second loop, why not call $galleries[$i]->getTitle(); in your for(...) loop?
I did that to test if I got returning values. Later on in this project I will make the objects as soon as the page loads but only need the data after a request. Then I have to split things up. However, you are right, it would have made more sence in this instance
Great. Then if Tomasz' answer has everything working for you, please accept it.

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.