diff options
Added JobOffer model and implemented a basic system for creating tables
2 files changed, 45 insertions, 0 deletions
| -rw-r--r-- | src/models/DB.php | 27 | ||||
| -rw-r--r-- | src/models/JobOffer.php | 18 |
diff --git a/src/models/DB.php b/src/models/DB.php index f6db512..1ca0137 100644 --- a/src/models/DB.php +++ b/src/models/DB.php @@ -1,10 +1,16 @@ <?php +include_once("JobOffer.php"); + class DB { private static $servername = "localhost"; private static $username = "root"; private static $password = "password"; private static $dbname = "devrix_job_board"; + public const STRING_TYPE = "VARCHAR(100)"; + public const FLOAT_TYPE = "DOUBLE(16,4)"; + public const INTEGER_TYPE = "INT"; + private $connection; function __construct() { @@ -23,9 +29,30 @@ class DB { // Make the actual connection to the database mysqli_select_db($this->connection, self::$dbname); + + $this->createTables(); + } + + private function createTables() { + $this->createTable(JobOffer::TABLE_NAME, JobOffer::TABLE_COLUMNS); } function __destruct() { mysqli_close($this->connection); } + + function createTable(string $tableName, array $columns) { + $query = "CREATE TABLE IF NOT EXISTS {$tableName}( + id INT NOT NULL AUTO_INCREMENT,"; + + foreach ($columns as $column) { + $query .= "{$column},"; + } + + $query .= "PRIMARY KEY (id));"; + + if (!mysqli_query($this->connection, $query)) { + die(mysqli_error($this->connection)); + } + } } diff --git a/src/models/JobOffer.php b/src/models/JobOffer.php new file mode 100644 index 0000000..1fb3d28 --- /dev/null +++ b/src/models/JobOffer.php @@ -0,0 +1,18 @@ +<?php +include_once("DB.php"); + +class JobOffer { + public const TABLE_NAME = "job_offers"; + public const TABLE_COLUMNS = array("title " . DB::STRING_TYPE, "description " . DB::STRING_TYPE, "company " . DB::STRING_TYPE, "salary " . DB::FLOAT_TYPE); + + public string $title; + public string $description; + public string $company; + public float $salary; + + private DB $db; + + function __construct(DB $db) { + $this->db = $db; + } +} |
