This commit is contained in:
boris
2025-02-06 12:30:01 +00:00
parent f30a8fe711
commit f64a9eadbe
176 changed files with 21418 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
<?php
class Database {
/**
* @var Database
*/
protected static $_dbInstance = null;
/**
* @var PDO
*/
protected $_dbHandle;
/**
* @return Database
*/
public static function getInstance() {
if(self::$_dbInstance === null) { //checks if the PDO exists
// creates new instance if not, sending in connection info
self::$_dbInstance = new self();
}
return self::$_dbInstance;
}
private function __construct() {
try {
$this->_dbHandle = new PDO("sqlite:students.sqlite");
}
catch (PDOException $e) { // catch any failure to connect to the database
echo $e->getMessage();
}
}
/**
* @return PDO
*/
public function getdbConnection() {
return $this->_dbHandle; // returns the PDO handle to be used elsewhere
}
public function __destruct() {
$this->_dbHandle = null; // destroys the PDO handle when no longer needed
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* Class StudentData
*
* This class models a students data, utilizing protected fields for data encapsulation.
* It takes a database row (array) as input in the constructor and assigns values to private fields, which include:
* - Student ID
* - First and Last Name
* - Course ID
* - International status (converted to 'yes' or 'no' strings for clarity)
*/
class StudentData {
// private fields
protected $_id, $_firstName, $_lastName, $_courseID, $_international;
// constructor
public function __construct($dbRow) {
$this->_id = $dbRow['student_id'];
$this->_firstName = $dbRow['first_name'];
$this->_lastName = $dbRow['last_name'];
if ($dbRow['international']) $this->_international = 'yes'; else $this->_international = 'no';
$this->_courseID = $dbRow['courseID'];
}
// accessors
public function getStudentID() {
return $this->_id;
}
// add in accessors for the other fields, check the view file in the given project to see what
// the accessor methods should be called
}

View File

@@ -0,0 +1,32 @@
<?php
require_once ('Database.php');
require_once ('StudentData.php');
class StudentsDataSet {
protected $_dbHandle, $_dbInstance;
public function __construct() {
$this->_dbInstance = Database::getInstance();
$this->_dbHandle = $this->_dbInstance->getdbConnection();
}
public function fetchAllStudents(): array
{
$sqlQuery = 'SELECT * FROM students;';
$statement = $this->_dbHandle->prepare($sqlQuery); // prepare a PDO statement
$statement->execute(); // execute the PDO statement
$dataSet = [];
// loop through and read the results of the query and cast
// them into a matching object
while ($row = $statement->fetch()) {
$dataSet[] = new StudentData($row);
}
return $dataSet;
}
}