<?php
class Destination{
 
    // database connection and table name
    private $conn;
    private $table_name = "destinations";
 
    // object properties
    public $Id;
    public $countryId;
    public $Name;
    public $Description;
    public $Recommended;
 
    public function __construct($db){
        $this->conn = $db;
    }
 
    // used by select drop-down list
    public function readAll(){
        //select all data
        $query = "SELECT
                    destinations.Id, destinations.Name, destinations.countryId, countries.Name as countryName, destinations.Description, destinations.Recommended
                FROM
                    destinations
                INNER JOIN countries ON destinations.countryId = countries.Id
                ORDER BY
                    Name
";
 
        	
        $stmt = $this->conn->prepare( $query );
        $stmt->execute();

        return $stmt;
    }
    
    public function readOne(){
 
    // query to read single record
    $query = "SELECT
                 Id, countryId, Name, Description, Recommended
            FROM
                " . $this->table_name . "
            WHERE
                Id = ?
            LIMIT
                0,1";
 
    // prepare query statement
    $stmt = $this->conn->prepare( $query );
 
    // bind id of product to be updated
    $stmt->bindParam(1, $this->Id);
 
    // execute query
    $stmt->execute();
 
    // get retrieved row
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
 
    // set values to object properties
    $this->Name = $row['Name'];
    $this->countryId = $row['countryId'];
    $this->Description = $row['Description'];
    $this->Recommended = $row['Recommended'];
    }


    public function readByCountryId(){
        //select all data

        $query = "SELECT
                    Id, countryId, Name, Description, Recommended
                FROM
                    " . $this->table_name . "
            WHERE
                countryId = ?
            ORDER BY
                Name";

        // prepare query statement
         $stmt = $this->conn->prepare( $query );
 
        // bind id of product to be updated
        $stmt->bindParam(1, $this->countryId);
        
        $stmt->execute();
        //$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        return $stmt;
    }
   
}
?>