Skip to content
Snippets Groups Projects
Commit 33af4ede authored by Fadhil Imam Kurnia's avatar Fadhil Imam Kurnia
Browse files

Create Driver and User model

parent 72a3eeb3
Branches project_structure
1 merge request!2Project structure
.idea
<?php
require_once __DIR__.'/../model/User.php';
require_once __DIR__.'/../model/Driver.php';
class ProfilController {
public static function ProfilHandler() {
......@@ -12,35 +15,17 @@ class ProfilController {
// Decrypt user id
$uid = simpleCrypt($_GET['u'], 'd');
try {
// Get connection to database
$dbconn = DB::getInstance();
// Prepare and execute sql query
$stmt = $dbconn->prepare("SELECT * FROM user WHERE id=$uid");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$user = $stmt->fetchObject();
if (!$user) {
echo "User not found!";
return;
}
$user->is_driver = 1;
// Getting driver profile
$dbconn = DB::getInstance();
$user = Driver::Create($uid, $dbconn);
if ($user->is_driver) {
$driver_rating = 4.7;
$driver_order = 1728;
}
require __DIR__.'/../view/profil.php';
} catch (PDOException $e) {
echo "Error: ".$e->getMessage();
if (!$user) {
echo "User not found!";
return;
}
require __DIR__.'/../view/profil.php';
}
......
<?php
class Driver extends User {
public $rating;
public $sumOrder;
public static function Create($id, PDO $dbconn) {
try {
$stmt = $dbconn->prepare("
SELECT id, name, username, email, phone, rating, is_driver AS isDriver, sum_order AS sumOrder
FROM user NATURAL JOIN driver
WHERE id =:id"
);
$stmt->execute(array('id'=>$id));
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetchObject('Driver');
return $result;
} catch (PDOException $e) {
echo "Error : ".$e->getMessage();
return false;
}
}
}
\ No newline at end of file
<?php
class User {
public $id;
public $name;
public $username;
public $email;
public $phone;
public $isDriver;
public static function Create($id, PDO $conn) {
try {
$stmt = $conn->prepare("SELECT * FROM user WHERE id=$id");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$user = $stmt->fetchObject();
$result = new User($user->id, $user->name, $user->username, $user->email, $user->phone);
$result->isDriver = $user->is_driver;
return $result;
} catch (PDOException $e) {
echo "Error : ".$e->getMessage();
return false;
}
}
}
\ No newline at end of file
......@@ -27,7 +27,7 @@
<img class="img-circle" src="<?=$user->photo?>"/><br>
<h2>@<?=$user->username?></h2>
<p><?=$user->name?></p>
<?php if ($user->is_driver) : ?>
<?php if ($user->isDriver) : ?>
<p>Driver | <?=$driver_rating?> (<?=$driver_order?> vote<?=($driver_order>1)?'s':''?>)</p>
<?php else : ?>
<p>Non Driver</p>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment