Skip to content
Snippets Groups Projects
Commit 1b0f1e23 authored by Iqbal's avatar Iqbal
Browse files

Merged branch 'crud-methods' of...

Merged branch 'crud-methods' of http://gitlab.informatika.org/DagoDev/TugasBesar1_2017 into login-and-register
parents dc2e4033 36b4a771
Branches
No related merge requests found
.idea
-- phpMyAdmin SQL Dump
-- version 4.5.4.1deb2ubuntu2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 04, 2017 at 09:40 PM
-- Server version: 10.0.31-MariaDB-0ubuntu0.16.04.2
-- PHP Version: 7.0.22-0ubuntu0.16.04.1
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `db_dagojek`
--
-- --------------------------------------------------------
--
-- Table structure for table `driver`
--
CREATE TABLE `driver` (
`id` int(11) NOT NULL,
`rating` float NOT NULL DEFAULT '0',
`sum_order` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `driver`
--
INSERT INTO `driver` (`id`, `rating`, `sum_order`) VALUES
(1, 4.3, 121);
-- --------------------------------------------------------
--
-- Table structure for table `order`
--
CREATE TABLE `order` (
`id_order` int(11) NOT NULL,
`id_driver` int(11) NOT NULL,
`id_customer` int(11) NOT NULL,
`source` varchar(20) NOT NULL,
`destination` varchar(20) NOT NULL,
`rating` int(11) NOT NULL,
`comment` varchar(120) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Table structure for table `prefered_location`
--
CREATE TABLE `prefered_location` (
`id_driver` int(11) NOT NULL,
`location` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Table structure for table `user`
--
CREATE TABLE `user` (
`id` int(15) NOT NULL,
`name` varchar(50) NOT NULL,
`username` varchar(20) NOT NULL,
`email` varchar(40) NOT NULL,
`password` varchar(100) NOT NULL,
`phone` varchar(15) NOT NULL,
`photo` varchar(100) NOT NULL,
`is_driver` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `user`
--
INSERT INTO `user` (`id`, `name`, `username`, `email`, `password`, `phone`, `photo`, `is_driver`) VALUES
(1, 'Fadhil Imam Kurnia', 'fadhilimamk', '13515146@std.stei.itb.ac.id', 'dtashfVATSaashdbgasdjy78123bahsVJSF72vsahsj', '085797490039', 'http://www.simian-risk.com/wp-content/themes/custom/images/empty-profile.png', 1);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `driver`
--
ALTER TABLE `driver`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `order`
--
ALTER TABLE `order`
ADD PRIMARY KEY (`id_order`);
--
-- Indexes for table `prefered_location`
--
ALTER TABLE `prefered_location`
ADD PRIMARY KEY (`id_driver`,`location`);
--
-- Indexes for table `user`
--
ALTER TABLE `user`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `username` (`username`,`email`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `user`
--
ALTER TABLE `user`
MODIFY `id` int(15) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `driver`
--
ALTER TABLE `driver`
ADD CONSTRAINT `driver_user_id` FOREIGN KEY (`id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
\ No newline at end of file
<?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;
}
}
public static function GetAllUsers(PDO $conn) {
try {
$result = $conn->query("SELECT * FROM user")->fetchAll();
return $result;
} catch (PDOException $e) {
echo "Error: ".$e->getMessage();
return false;
}
}
public static function GetUserById($id, PDO $conn) {
try {
$stmt = $conn->prepare("SELECT * FROM user WHERE id=?");
$stmt->execute([$id]);
$stmt->setFetchMode(PDO::FETCH_OBJ);
$user = $stmt->fetch();
return $user;
} catch (PDOException $e) {
echo "Error: ".$e->getMessage();
return false;
}
}
public static function UpdateUser($user, PDO $conn) {
try {
if ($user instanceof User) {
$newAttributes = "";
$newAttributes .= "name = "."$user->name, ";
$newAttributes .= "email = "."$user->email, ";
$newAttributes .= "phone = "."$user->phone, ";
$newAttributes .= "is_driver = "."$user->isDriver";
$conn->prepare("UPDATE user SET $newAttributes WHERE id =?")->execute([$user->id]);
}
} 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