本次作业几乎凝聚了本人本学期PHP所有的知识。
要求:
进入首页需要登录
成功登录之后用session方法记住此用户。
成功登录后对于collections可以实现增删改查的操作。
对于collections中对应的cars可以实现增删的操作。
目录结构:
代码如下:
app/controller/CarsController.php
include_once 'controller/Controller.php';
include_once 'model/CarModel.php';
include_once 'view/Page.php';
class CarsController extends Controller {
var $booking = null;
function __construct() {
$this->booking = new BookingModel();
}
function index() {
include_once 'view/content.php';
}
function viewDetails() {
$path = "../";
$id = $_GET['id'];
$records = $this->booking->readCarRecords($id);
include_once 'view/CarDetails.php';
}
function insertCars() {
$id = $_GET['id'];
$details = "";
$price_paid = "";
$image_filename = "";
if (isset($_POST['submit'])) {
$errors = [];
if (isset($_POST["details"])) {
$details = trim($_POST["details"]);
if (strlen($details) == 0) {
$errors["details"] = "Missing input";
}
}
if (isset($_POST["price_paid"])) {
$price_paid = trim($_POST["price_paid"]);
if (strlen($price_paid) == 0) {
$errors["price_paid"] = "Missing input";
}
}
// image
$image_filename = $_FILES["image_filename"]["name"];
$temp_file = $_FILES["image_filename"]["tmp_name"];
$type = $_FILES["image_filename"]["type"];
$size = $_FILES["image_filename"]["size"];
$errorLevel = $_FILES["image_filename"]["error"];
$error_messages = [
"Upload successful",
"File exceeds maximum upload size specified by default",
"File exceeds size specified by MAX_FILE_SIZE",
"File only partially uploaded",
"Form submitted with no file specified",
"",
"No temporary folder",
"Cannot write file to disk",
"File type is not permitted"
];
$destination = 'photos/';
$target_file = $destination . $image_filename;
$max = 3000000;
if ($errorLevel > 0) {
// Set the error message to the errors array
$errors["image"] = $error_messages[$errorLevel];
} else {
if (file_exists($temp_file)) {
$size = $_FILES["image_filename"]["size"];
if ($size <= $max) {
$permitted = ["gif", "jpg", "jpeg", "png"];
$ext = pathinfo($image_filename, PATHINFO_EXTENSION);
if (in_array($ext, $permitted)) {
move_uploaded_file($temp_file, $target_file);
//$errors["image"] = "The file $filename has been uploaded.";
} else {
$errors["image"] = "$image_filename type is not permitted";
}
} else {
$errors["image"] = "$image_filename is too big – upload failed";
}
} else {
$errors["image"] = "File upload has failed";
}
}
if (count($errors) == 0) {
$details = $_POST["details"];
$price_paid = $_POST["price_paid"];
$values = [$image_filename, $details, $price_paid];
$success = $this->booking->addCars($values, $id);
$data = [];
$records = $this->booking->readRecords();
$data['records'] = $records;
if ($success) {
include_once 'index.php';
echo "Add car successfully";
} else {
include_once 'index.php';
echo "Add car Error";
}
} else {
include_once('view/addCars.php');
}
} else {
include_once('view/addCars.php');
}
}
function deleteCars(){
$car_id = $_GET['car_id'];
$success = $this->booking->deleteCars($car_id);
if($success)
echo "Delete successfully";
else
echo "Delete successfully";
}
}
app/controller/ContactController.php
include_once 'controller/Controller.php';
include_once 'model/CarModel.php';
include_once 'view/Page.php';
class ContactController extends Controller {
var $booking = null;
function __construct() {
$this->booking = new BookingModel();
}
function index() {
include_once 'view/content.php';
}
function addContacts() {
if (isset($_POST['submit'])) {
$errors = [];
if (isset($_POST["name"])) {
$name = trim($_POST["name"]);
if (strlen($name) == 0) {
$errors["name"] = "Missing input";
} else {
$temp = str_replace(' ', '', $name);
if (!ctype_alpha($temp)) {
$errors["name"] = "Enter alpha only";
}
}
}
if (isset($_POST["period"])) {
$period = trim($_POST["period"]);
if (strlen($period) == 0) {
$errors["period"] = "Missing input";
}
}
if (isset($_POST["make"])) {
$make = trim($_POST["make"]);
if (strlen($make) == 0) {
$errors["make"] = "Missing input";
}
}
if (isset($_POST["model"])) {
$model = trim($_POST["model"]);
if (strlen($model) == 0) {
$errors["model"] = "Missing input";
}
}
if (isset($_POST["car_type"])) {
$car_type = trim($_POST["car_type"]);
if (strlen($car_type) == 0) {
$errors["car_type"] = "Missing input";
}
}
if (isset($_POST["origin"])) {
$origin = trim($_POST["origin"]);
if (strlen($origin) == 0) {
$errors["origin"] = "Missing input";
}
}
if (isset($_POST["user_id"])) {
$user_id = trim($_POST["user_id"]);
if (strlen($user_id) == 0) {
$errors["user_id"] = "Missing input";
}
}
if (count($errors) == 0) {
$values = [];
unset($_POST['submit']);
foreach ($_POST as $key => $value) {
${$key} = $value;
$values[] = $value;
}
//$values = [$name,$period,$make,$model,$car_type,$origin,$user_id];
$success = $this->booking->insertRecord($values);
include_once('index.php');
echo"Add successfully";
} else {
include_once('view/addCollectionsForm.php');
}
} else {
$data = [];
include_once('view/addCollectionsForm.php');
}
}
function deleteContacts() {
$id = $_GET['id'];
$success = $this->booking->deleteRecord($id);
if ($success)
echo "Delete successfully";
else
echo "Delete successfully";
}
function editContacts() {
$id = $_GET['id'];
$records = $this->booking->readRecords();
for ($i = 0; $i < count($records); $i++) {
if ($records[$i]['collection_id'] == $id) {
$name = $records[$i]['name'];
$period = $records[$i]['period'];
$model = $records[$i]['model'];
$make = $records[$i]['make'];
$car_type = $records[$i]['car_type'];
$origin = $records[$i]['origin'];
}
}
if (isset($_POST['submit'])) {
$errors = [];
if (isset($_POST["name"])) {
$name = trim($_POST["name"]);
if (strlen($name) == 0) {
$errors["name"] = "Missing input";
} else {
$temp = str_replace(' ', '', $name);
if (!ctype_alpha($temp)) {
$errors["name"] = "Enter alpha only";
}
}
}
if (isset($_POST["period"])) {
$period = trim($_POST["period"]);
if (strlen($period) == 0) {
$errors["period"] = "Missing input";
}
}
if (isset($_POST["make"])) {
$make = trim($_POST["make"]);
if (strlen($make) == 0) {
$errors["make"] = "Missing input";
}
}
if (isset($_POST["model"])) {
$model = trim($_POST["model"]);
if (strlen($model) == 0) {
$errors["model"] = "Missing input";
}
}
if (isset($_POST["car_type"])) {
$car_type = trim($_POST["car_type"]);
if (strlen($car_type) == 0) {
$errors["car_type"] = "Missing input";
}
}
if (isset($_POST["origin"])) {
$origin = trim($_POST["origin"]);
if (strlen($origin) == 0) {
$errors["origin"] = "Missing input";
}
}
if (count($errors) == 0) {
$values = [];
unset($_POST['submit']);
foreach ($_POST as $key => $value) {
${$key} = $value;
$values[] = $value;
}
//$values = [$name,$period,$make,$model,$car_type,$origin];
$success = $this->booking->updateRecord($values, $id);
include_once('index.php');
if ($success)
echo "Update successfully";
else
echo "Update Error";
} else {
include_once('view/editContacts.php');
}
} else {
//$data = [];
include_once('view/editContacts.php');
}
}
function searchContacts() {
$path = "../";
$keyword = $_POST["keyword"];
$records = $this->booking->searchRecords($keyword);
include_once('view/viewContacts.php');
}
function viewContacts() {
$path = "../";
$records = $this->booking->readRecords();
include_once 'view/viewContacts.php';
}
}
app/controller/Controller.php
abstract class Controller {
//put your code here
abstract function index();
}
app/controller/HomeController.php
include_once 'controller/Controller.php';
class HomeController extends Controller{
public function index() {
include_once 'view/content.php';
}
}
app/controller/LoginController.php
include_once 'controller/Controller.php';
include_once 'model/UserModel.php';
if(session_status() == PHP_SESSION_NONE){
session_start();
}
class LoginController extends Controller{
var $user;
function __construct() {
$this->user = new UserModel();
}
function index(){
include_once 'view/LoginForm.php';
}
function login(){
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
$user_id = $this->user->authenticate($username, $password);
if($user_id > 0){
$_SESSION['user_id'] = $user_id;
$_SESSION['username'] = $username;
header("location:./management");
} else {
$errors['username'] = "Username or Password is incorret!";
include_once 'view/LoginForm.php';
}
} else {
include_once 'view/LoginForm.php';
}
}
function logout(){
if(session_status() == PHP_SESSION_ACTIVE){
session_destroy();
}
header("location:./../");
}
}
app/database/dbconfig.json
{
"DSN": "mysql:host=localhost;dbname=ecarsdb","USERNAME":"root","PASSWORD":""
}
app/factory/PDOFactory.php
class PDOFactory {
const DB_CONFIG_FILE_PATH = __DIR__.'/../database/dbconfig.json';
static function getConnection(){
$f = fopen(PDOFactory::DB_CONFIG_FILE_PATH,"r");
$content = fread($f, filesize(PDOFactory::DB_CONFIG_FILE_PATH));
$json_data = json_decode($content);
$dsn = $json_data->DSN;
$username = $json_data->USERNAME;
$password = $json_data->PASSWORD;
$conn = new PDO($dsn,$username,$password);
return $conn;
}
}
app/model/CarModel.php
include_once 'DataModel.php';
include_once 'factory/PDOFactory.php';
class BookingModel extends DataModel {
function __construct() {
parent::__construct('bookings');
}
function deleteRecord($id) {
$conn = PDOFactory::getConnection();
$sql = "delete from collections where collection_id=$id";
$statement = $conn->prepare($sql);
$success = $statement->execute();
return $success;
}
function deleteCars($car_id) {
$conn = PDOFactory::getConnection();
$sql = "delete from cars where car_id=$car_id";
$statement = $conn->prepare($sql);
$success = $statement->execute();
return $success;
}
function insertRecord($values) {
$conn = PDOFactory::getConnection();
$sql = "insert into collections(name,period,make,model,car_type,origin,user_id)values(?,?,?,?,?,?,?)";
$statement = $conn->prepare($sql);
$success = $statement->execute($values);
}
function readRecords() {
$conn = PDOFactory::getConnection();
$statement = $conn->query("select * from collections");
$statement->setFetchMode(PDO::FETCH_ASSOC);
$records = [];
while ($row = $statement->fetch()) {
$records[] = $row;
}
return $records;
}
function searchRecords($keyword) {
$conn = PDOFactory::getConnection();
$sql = "select * from collections where name like '%$keyword%'
or period like '%$keyword%'
or model like '%$keyword%'
or origin like '%$keyword%'
or car_type like '%$keyword%'";
$statement = $conn->query($sql);
$records = [];
while ($row = $statement->fetch()) {
$records[] = $row;
}
return $records;
}
function updateRecord($values, $id) {
//$conn = PDOFactory::getConnection();
$sql = "update collections set name = ?,
period = ?,
make = ?,
model = ?,
car_type = ?,
origin = ?
where collection_id=$id";
$this->statement = $this->conn->prepare($sql);
$success = $this->statement->execute($values);
return $success;
}
function readCarRecords($id) {
$conn = PDOFactory::getConnection();
$statement = $conn->query("select * from cars where collection_id = $id");
$statement->setFetchMode(PDO::FETCH_ASSOC);
$records = [];
while ($row = $statement->fetch()) {
$records[] = $row;
}
return $records;
}
function addCars($values, $id) {
$conn = PDOFactory::getConnection();
$sql = "insert into cars(image_filename,details,price_paid,collection_id)values(?,?,?,$id)";
$statement = $conn->prepare($sql);
$success = $statement->execute($values);
return $success;
}
}
app/model/Crudable.php
interface Crudable {
function deleteRecord($id);
function insertRecord($values);
function readRecords();
function searchRecords($keyword);
function updateRecord($values,$id);
function readCarRecords($id);
function addCars($values,$id);
}
app/model/DataModel.php
include_once 'Crudable.php';
include_once 'factory/PDOFactory.php';
abstract class DataModel implements Crudable{
var $conn = null;
var $table = '';
function __construct($table) {
$this->table = $table;
$this->conn = PDOFactory::getConnection();
}
function close(){
$this->conn = null;
}
}
app/model/UserModel.php
include_once "model/DataModel.php";
class UserModel extends DataModel{
function __construct() {
parent::__construct("users");
}
function authenticate($username,$password){
$user_id = 0;
$sql = "select user_id,password from users where username = '$username'";
$statement = $this->conn->query($sql);
if($row = $statement->fetch()){
if($row['password'] == $password){
$user_id = $row['user_id'];
}
}
return $user_id;
}
public function deleteRecord($id){}
public function insertRecord($values){}
public function readRecords(){}
public function searchRecords($keyword){}
public function updateRecord($values,$id){}
public function readCarRecords($id){}
public function addCars($values, $id) {}
}
app/view/CarDetails.php
<br>
<br>
<div class="container">
<div class="row">
<div class="col-md-2 text-center"></div>
<div class="col-md-8 box text-center">View Contacts:= count($records) ?>results</div>
<div class="col-md-2 text-center"></div>
</div>
<div class="row">
<div class="col-md-2 text-center"></div>
<div class="col-md-8 jumbotron text-center">
<table class="table table-striped table-hover" >
<tr>
<th>Car ID</th>
<th>Image</th>
<th>Details</th>
<th>Price</th>
<th>Collection ID</th>
<th>Delete</th>
</td>
</tr>
foreach ($records as $row): ?>
<tr>
<td align="left">= $row['car_id'] ?></td>
<td align="left">
<img src="photos/= $row['image_filename'] ?>" width="50" height="50"/>
</td>
<td align="left">= $row['details'] ?></td>
<td align="left">= $row['price_paid'] ?></td>
<td align="left">= $row['collection_id'] ?></td>
<td align="left">
<a href="?action=deleteCars&car_id== $row['car_id'] ?>" >Delete</a>
</td>
</tr>
endforeach; ?>
<tr align="middle">
<a href="?action=addCars&id== $row['collection_id'] ?>" >
<button>ADD A CAR</button></a>
</tr>
</table>
</div>
<div class="col-md-2 text-center"></div>
</div>
</div>
\
app/view/LoginForm.php
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 box text-center">Login</div>
<div class="col-sm-3"></div>
</div>
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 jumbotron">
<form class="form-horizontal" action="" method="post" novalidate>
<div class="form-group">
<label class="control-label" for="username">
User Name
<span class="error" style="color:red">
= isset($errors['username']) ? $errors['username'] : "" ?>
</span>
</label>
<input class="form-control" type="text" name="username" />
</div>
<div class="form-group">
<label class="control-label" for="password">
Password
</label>
<input class="form-control" type="password" name="password" />
</div>
<br/>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary btn-block" name="submit" />
</div>
</form>
</div>
<div class="col-sm-3"></div>
</div>
app/view/Page.php
class Page {
const HEADER_FILE = "view/header.php";
const FOOTER_FILE = "view/footer.php";
static function getView($page,$data = []){
foreach ($data as $key => $value){
${$key} = $value;
}
$content = "view/$page.php";
include_once Page::HEADER_FILE;
include_once $content;
include_once Page::FOOTER_FILE;
}
}
app/view/addCars.php
<div class="container">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 box text-center">
ADD Cars Details
</div>
<div class="col-sm-3"></div>
</div>
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 jumbotron">
<form action="" method="post" novalidate="true"enctype="multipart/form-data">
<div class="form-group">
<label class="control-label">
Photo
</label>
<span class="error" style="color:red">
= isset($errors['image']) ? $errors['image'] : "" ?>
</span>
<input class="form-control" type="file" name="image_filename" />
</div>
<div class="form-group">
<label class="control-label">
Details
</label>
<span class="error" style="color:red">
= isset($errors['details']) ? $errors['details'] : "" ?>
</span>
<input class="form-control" type="text" name="details" />
</div>
<div class="form-group">
<label class="control-label">
Price Paid
</label>
<span class="error" style="color:red">
= isset($errors['price_paid']) ? $errors['price_paid'] : "" ?>
</span>
<input class="form-control" type="text" name="price_paid" />
</div>
<br/>
<div class="form-group">
<input class="btn btn-primary btn-block" type="submit" name="submit" />
</div>
</form>
</div>
<div class="col-sm-3"></div>
</div>
</div>
app/view/addCollectionsForm.php
<div class="container">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 box text-center">
ADD Collections
</div>
<div class="col-sm-3"></div>
</div>
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 jumbotron">
<form action="" method="post" novalidate="true"enctype="multipart/form-data">
<div class="form-group">
<label class="control-label" >
Name
</label>
<span class='error'style="color:red">
= isset($errors["name"]) ? $errors["name"] : ""; ?>
</span>
<input class="form-control" type="text" name='name'/>
</div>
<div class="form-group">
<label class="control-label">
Period
<span class='error'style="color:red">
= isset($errors["period"]) ? $errors["period"] : ""; ?>
</span>
</label>
<input class="form-control" type="text" name="period" />
</div>
<div class="form-group">
<label class="control-label">
Make
<span class='error'style="color:red">
= isset($errors["make"]) ? $errors["make"] : ""; ?>
</span>
</label>
<input class="form-control" type="text" name="make" />
</div>
<div class="form-group">
<label class="control-label">
Model
<span class='error'style="color:red">
= isset($errors["model"]) ? $errors["model"] : ""; ?>
</span>
</label>
<input class="form-control" type="text" name="model" />
</div>
<div class="form-group">
<label class="control-label">
Car_type
<span class='error'style="color:red">
= isset($errors["car_type"]) ? $errors["car_type"] : ""; ?>
</span>
</label>
<input class="form-control" type="text" name="car_type" />
</div>
<div class="form-group">
<label class="control-label">
Origin
<span class='error'style="color:red">
= isset($errors["origin"]) ? $errors["origin"] : ""; ?>
</span>
</label>
<input class="form-control" type="text" name="origin" />
</div>
<div class="form-group">
<label class="control-label">
Use_id
<span class='error'style="color:red">
= isset($errors['user_id']) ? $errors['user_id'] : ""; ?>
</span>
</label>
<input class="form-control" type="text" name="user_id" />
</div>
<input type="submit" value="SUBMIT" class="btn btn-primary btn-block" name="submit">
</form>
</div>
<div class="col-sm-3"></div>
</div>
</div>
app/view/content.php
<div class="slogan">
<h2> <span class="text_color">CARS COLLECTIONS</span> </h2>
<h4>Welcome to LEO's car's collections</h4>
</div>
app/view/editContacts.php
<div class="container">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 box text-center">
Update Collections
</div>
<div class="col-sm-3"></div>
</div>
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 jumbotron">
<form class="form-horizontal" action="" method="post" novalidate>
<div class="form-group">
<label>
Name
</label>
<span class='error'style="color:red">
= isset($errors["name"]) ? $errors["name"] : ""; ?>
</span>
<input class="form-control" type="text" name="name" maxlength='30' value='= $name ?>'/>
</div>
<div class="form-group">
<label>
Period
</label>
<span class='error'style="color:red">
= isset($errors["period"]) ? $errors["period"] : ""; ?>
</span>
<input class="form-control" type="text" name="period" maxlength='30' value='= $period ?>'/>
</div>
<div class="form-group">
<label>
Make
</label>
<span class='error'style="color:red">
= isset($errors["make"]) ? $errors["make"] : ""; ?>
</span>
<input class="form-control" type="text" name="make" maxlength='30' value='= $make ?>'/>
</div>
<div class="form-group">
<label>
Model
</label>
<span class='error'style="color:red">
= isset($errors["model"]) ? $errors["model"] : ""; ?>
</span>
<input class="form-control" type="text" name="model" maxlength='30' value='= $model ?>'/>
</div>
<div class="form-group">
<label>
Car Type
</label>
<span class='error'style="color:red">
= isset($errors["car_type"]) ? $errors["car_type"] : ""; ?>
</span>
<input class="form-control" type="text" name="car_type" maxlength='30' value='= $car_type ?>'/>
</div>
<div class="form-group">
<label>
Origin
</label>
<span class='error'style="color:red">
= isset($errors["origin"]) ? $errors["origin"] : ""; ?>
</span>
<input class="form-control" type="text" name="origin" maxlength='30' value='= $origin ?>'/>
</div>
<input type="submit" value="UPDATE" class="btn btn-primary btn-block" name="submit">
</form>
</div>
<div class="col-sm-3"></div>
</div>
</div>
</div>
app/view/footer.php
</section>
<!-- /Section: intro -->
<footer>
<div class="container">
<div class="row">
<div class="col-md-12 col-lg-12">
<div class="wow shake" data-wow-delay="0.4s">
<div class="page-scroll marginbot-30">
</div>
<p>©SquadFREE. All rights reserved.</p>
<div class="credits">
Designed by <a href="https://bootstrapmade.com/">BootstrapMade</a>
</div>
</div>
</div>
</div>
</footer>
<!-- Core JavaScript Files -->
<script src="= isset($path) ? $path : ""; ?>assets/js/jquery.min.js"></script>
<script src="= isset($path) ? $path : ""; ?>assets/js/bootstrap.min.js"></script>
<script src="= isset($path) ? $path : ""; ?>assets/js/jquery.easing.min.js"></script>
<script src="= isset($path) ? $path : ""; ?>assets/js/jquery.scrollTo.js"></script>
<script src="= isset($path) ? $path : ""; ?>assets/js/wow.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="= isset($path) ? $path : ""; ?>assets/js/custom.js"></script>
</body>
</html>
app/view/header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>CARS COLLECTION</title>
<!-- Bootstrap Core CSS -->
<link href="=isset($path)? $path: ""; ?>assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<!-- Fonts -->
<link href="=isset($path)? $path: ""; ?>assets/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="=isset($path)? $path: ""; ?>assets/css/animate.css" rel="stylesheet" />
<!-- Squad theme CSS -->
<link href="=isset($path)? $path: ""; ?>assets/css/style.css" rel="stylesheet">
<link href="=isset($path)? $path: ""; ?>assets/color/default.css" rel="stylesheet">
</head>
<body id="page-top" data-spy="scroll" data-target=".navbar-custom">
<nav class="navbar navbar-custom navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-main-collapse">
<i class="fa fa-bars"></i>
</button>
<a class="navbar-brand" href="index.html">
<h1>=isset($username)? $username: ""; ?></h1>
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-right navbar-main-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#intro">Home</a></li>
if(isset($path)) { ?>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Drop down <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="?action=viewContacts">VIEW COLLECTIONS</a></li>
<li><a href="?action=addContacts">ADD COLLECTIONS</a></li>
<li><a href="?action=searchContacts">SEARCH COLLECTIONS</a></li>
</ul>
</li>
<li><a href="?action=logout">Logout</a></li>
} else { ?>
<li><a href="?action=login">Login</a></li>
} ?>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Section: intro -->
<section id="intro" class="intro">
app/view/searchContacts.php
<div class="row">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 box text-center">
Search Contacts
</div>
<div class="col-sm-3"></div>
</div>
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6 jumbotron">
<form action="" method="post" novalidate="true" >
<div class="form-group">
<label class="control-label">
Keyword
</label>
<input class="form-control" type="text" name="keyword" />
</div>
<br/>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block" name="search" value="">SEARCH</button>
</div>
</form>
</div>
<div class="col-sm-3"></div>
</div>
</div>
app/view/viewContacts.php
<br>
<br>
<div class="container">
<div class="row">
<div class="col-md-2 text-center"></div>
<div class="col-md-8 box text-center">View Contacts:=count($records) ?>results</div>
<div class="col-md-2 text-center"></div>
</div>
<div class="row">
<div class="col-md-2 text-center"></div>
<div class="col-md-8 jumbotron text-center">
<table class="table table-striped table-hover" >
<tr>
<th>Collection ID</th>
<th>Name</th>
<th>Period</th>
<th>Make</th>
<th>Model</th>
<th>Car_type</th>
<th>Origin</th>
<th>User_ID</th>
<th>Edit</th>
<th>View Details</th>
<th>Delete</th>
</td>
</tr>
foreach ($records as $row): ?>
<tr>
<td align="left">=$row['collection_id'] ?></td>
<td align="left">=$row['name'] ?></td>
<td align="left">=$row['period'] ?></td>
<td align="left">=$row['make'] ?></td>
<td align="left">=$row['model'] ?></td>
<td align="left">=$row['car_type'] ?></td>
<td align="left">=$row['origin'] ?></td>
<td align="left">=$row['user_id'] ?></td>
<td align="left">
<a href="?action=editContacts&id==$row['collection_id']?>" >Edit</a>
</td>
<td align="left">
<a href="?action=ViewDetails&id==$row['collection_id']?>" >View</a>
</td>
<td align="left">
<a href="?action=deleteContacts&id==$row['collection_id']?>" >Delete</a>
</td>
</tr>
endforeach; ?>
</table>
</div>
<div class="col-md-2 text-center"></div>
</div>
</div>
management/index.php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
include_once '../config.php';
include_once 'view/page.php';
include_once 'controller/ContactController.php';
include_once 'controller/LoginController.php';
include_once 'controller/HomeController.php';
include_once 'controller/CarsController.php';
$bookingController = new ContactController();
$loginController = new LoginController();
$carsController = new CarsController();
$contactsController = new ContactController();
$path = "../";
$name = '';
$period = '';
$make = '';
$model = '';
$car_type = '';
$origin = '';
if (isset($_SESSION['user_id']) && isset($_SESSION['username'])) {
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];
include_once 'view/header.php';
if (isset($_GET['action'])) {
$action = $_GET['action'];
if ($action == 'logout') {
$loginController->logout();
} else {
if ($action == 'viewContacts') {
$contactsController->viewContacts();
} else if ($action == 'addContacts') {
$contactsController->addContacts();
} else if ($action == 'editContacts') {
$contactsController->editContacts();
} else if ($action == 'ViewDetails') {
$carsController->viewDetails();
} else if ($action == 'addCars') {
$carsController->insertCars();
} else if ($action == 'deleteContacts') {
$contactsController->deleteContacts();
} else if ($action == 'deleteCars') {
$carsController->deleteCars();
} elseif ($action == 'searchContacts') {
if (isset($_POST['search'])) {
$contactsController->searchContacts();
} else {
include_once ('view/searchContacts.php');
}
}
}
} else {
$bookingController->index();
}
include_once 'view/footer.php';
} else {
$loginController->logout();
}
config.php
set_include_path(__DIR__.'/app');
index.php
include_once 'config.php';
include_once 'view/header.php';
include_once 'view/page.php';
include_once 'controller/ContactController.php';
include_once 'controller/LoginController.php';
include_once 'controller/HomeController.php';
$loginController = new LoginController();
$contactsController = new ContactController();
$homeController = new HomeController();
if (isset($_GET['action'])) {
$action = $_GET['action'];
if ($action == 'login') {
$loginController->login();
}
} else {
$homeController->index();
}
include_once 'view/footer.php';
以上。源文件在本人博客资源中。