Created
May 7, 2017 01:09
-
-
Save phpfiddle/3a1758ef452b0cc80202929807cb38f4 to your computer and use it in GitHub Desktop.
[ Posted by Inkyu ] COMP484_Assignment#7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Start the session | |
session_start(); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<link href='http://fonts.googleapis.com/css?family=Ubuntu:400,400italic,700,700italic' rel='stylesheet' type='text/css'> | |
<title></title> | |
<link rel="stylesheet" type="text/css" href="style.css"> | |
</head> | |
<body> | |
<form action="index.php" method="post"> | |
<div> | |
<fieldset> | |
<legend id="legend">Add an Employee</legend> | |
<label>First Name</label><br> | |
<input type="text" name="first" required><br> | |
<label>Last Name</label><br> | |
<input type="text" name="last" required><br> | |
<label>Department</label><br> | |
<select name="department"> | |
<option value="Engineering">Engineering</option> | |
<option value="Business">Business</option> | |
<option value="Humanity">Humanity</option> | |
<option value="Art">Art</option> | |
<option value="Natural Science">Natural Science</option> | |
</select> | |
<br><br> | |
<input type="submit"> | |
</fieldset> | |
</div> | |
</form> | |
<div class="output"> | |
<?php | |
// make an global array that store employee objects | |
if (!isset($_SESSION["employees"])) { | |
$_SESSION["employees"] = array(); | |
} | |
// make an global array that store 8 digit ids for making sure it is unique | |
if (!isset($_SESSION["ids"])) { | |
$_SESSION["ids"] = array(); | |
} | |
// make an global array that store firstnames | |
if (!isset($_SESSION["firstnames"])) { | |
$_SESSION["firstnames"] = array(); | |
} | |
// make an global array that store lastnames | |
if (!isset($_SESSION["lastnames"])) { | |
$_SESSION["lastnames"] = array(); | |
} | |
// employee class | |
class Employee { | |
// attr | |
public $id; | |
public $firstname; | |
public $lastname; | |
public $department; | |
// constructor | |
public function __construct($id, $firstname, $lastname, $department) { | |
$this->id = $id; | |
$this->firstname = $firstname; | |
$this->lastname = $lastname; | |
$this->department = $department; | |
} | |
} | |
if ($_SERVER["REQUEST_METHOD"] == "POST") { | |
// check input value exist | |
if(isset($_POST["first"]) && isset($_POST["last"]) && isset($_POST["department"])) { | |
// get name, department | |
$first_name = $_POST["first"]; | |
$last_name = $_POST["last"]; | |
$department = $_POST["department"]; | |
// if the employee is not exist in employees array, let's add it | |
if(!in_array($first_name, $_SESSION["firstnames"] ) && !in_array($last_name, $_SESSION["lastnames"])) { | |
// generate random 8 digit number | |
$id = rand8(); | |
// check if this $id is not exist | |
while(in_array($id, $_SESSION["ids"])) { | |
// generate random integer value again | |
$id = rand8(); | |
} | |
// add id to id array | |
array_push($_SESSION["ids"], $id); | |
// add first name to first names array | |
array_push($_SESSION["firstnames"], $first_name); | |
// add first name to first names array | |
array_push($_SESSION["lastnames"], $last_name); | |
// add new employee as json object to employees array | |
array_push($_SESSION["employees"], json_encode(new Employee($id, $first_name, $last_name, $department))); | |
// output when employee added | |
echo "<h3>Employee Added</h3>"; | |
echo "<p>Name: " . $first_name . ", " . $last_name . "<br>"; | |
echo "Department: " . $department . "<br>"; | |
echo "Employee ID: " . $id . "<br>"; | |
echo "Hire Date: " . date("l m d Y") . "<br>"; | |
echo "Total Employees: " . count($_SESSION["employees"]) . "<br></p>"; | |
} | |
} | |
}// end of post handle | |
// 8 digit random number generator | |
function rand8() { | |
$random_number=''; | |
$count = 0; | |
while ( $count < 8 ) { | |
$random_digit = mt_rand(0, 9); | |
$random_number .= $random_digit; | |
$count++; | |
} | |
return $random_number; | |
} | |
?> | |
</div> | |
<?php | |
// to debug, lets print employees array on browser console in json format | |
console_log($_SESSION["employees"]); | |
// function to debug | |
function console_log($data){ | |
echo '<script>'; | |
echo 'console.log('. json_encode( $data ) .')'; | |
echo '</script>'; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment