You are on page 1of 4

<?php /** * A form the uses object oriented programming to create an employee database.

* In addition, the form accepts new users and adds them to the database. * The form checks the input to make sure everything is the proper format. * If it's not, it returns the errors, reloads the page, and fills the form. * * @author Harrison Stewart * @date Feb 29, 2012 * Assignment 4 * @version 1.0 */ /** * Loads the validation and employee classes. */ require('validation.php'); require('employee.php'); /** * Creates a new validation object and an array of employee objects. */ $validation = new validation(); $people = $people[] $people[] $people[] $people[] $people[] $people[] array(); = new employee('Robert', 'Smith', '850.241.2302', '221'); = new employee('Amy', 'Jones', '850.520.1109', '242'); = new employee('Norman', 'Roberts', '904.102.3203', '255'); = new employee('Sally', 'Worth', '850.111.9999', '202'); = new employee('Mark', 'Sampson', '223.445.6677', '301'); = new employee('Jeff', 'Richards', '850.444.3233', '104');

/** * Creates an array to hold validation errors */ $validation_errors = array(); /** * Flag used to see if the validation process was a success * * @var bool */ $success = FALSE; /** * * If the post array has any entries, the validation process begins. * As functions are called, any errors returned are stored in the validation * errors array to be display later. */ if (count($_POST) > 0) { // Name field validation $result = $validation->check_required('name'); if ($result !== TRUE) { $validation_errors[] = $result; }

// Check the max length of the field $result = $validation->check_max_length('name', 100); if ($result !== TRUE) { $validation_errors[] = $result; } // Check minimum length of the name field $result = $validation->check_min_length('name', 3); if ($result !== TRUE) { $validation_errors[] = $result; } // Office field validation $result = $validation->check_required('office'); if ($result !== TRUE) { $validation_errors[] = $result; } // Check if the office field is numeric $result = $validation->check_numeric('office'); if ($result !== TRUE) { $validation_errors[] = $result; } // Phone number field validation $result = $validation->check_required('phone'); if ($result !== TRUE) { $validation_errors[] = $result; } // Checks the max length of the phone number $result = $validation->check_max_length('phone', 15); if ($result !== TRUE) { $validation_errors[] = $result; } // Makes sure the phone number meets character contraints $result = $validation->check_char_constraints('phone'); if ($result !== TRUE) { $validation_errors[] = $result; } /** * Checks to see how many errors are entered in the validation errors * array, if none, then it splits the name into first and last and adds * the new employee object to the $people array and sets the success * flag to true. */ if (count($validation_errors) < 1) { // Adds user to the database list($first, $last) = explode(' ', $_POST['name']); $people[] = new employee($first, $last, $_POST['phone'], $_POST['off ice']); // Sets user add success flag to true $success = TRUE; } }

?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php // If Success true, let's user know and displays the database. if ($success === TRUE) { ?> <h2>You have successfully been added to the database.</h2> <?php // Calls function to display the database, makes it look tidy display_database($people); ?> <?php // Otherwise displays the errors if any or displays the form } else { if (count($validation_errors) > 0) { echo "<ul>"; foreach($validation_errors as $err) { echo "<li>" . $err . "</li>"; } echo "</ul>"; } ?> <h2>Employee Registry</h2> <form action="index.php" method="post"> <label>Name:</label> <input type="text" name="name" value="<?php $validation->refill('nam e'); ?>" /><br /> <label>Office:</label> <input type="text" name="office" value="<?php $validation->refill('o ffice'); ?>" /><br /> <label>Phone Number:</label> <input type="text" name="phone" value="<?php $validation->refill('ph one'); ?>" /><br /> <input type="checkbox" name="display_dir" value="yes" <?php if (isset($_POST['newsletter']) && $_POST['newsletter'] == 'yes' ) echo "checked='checked'" ?> /> Yes <br /> <button type="submit">Submit</button>

</form> <?php } ?> </body> </html> <?php /** * Displays the database in table format using the $people array of employee * objects * @param array $people */ function display_database($people) { ?> <table border="1"> <tr> <th>Name</th> <th>Phone Number</th> <th>Office</th> </tr> <?php foreach ($people as $emp) { ?> <tr> <td><?php echo $emp->get_firstname() . ' ' . $emp->get_lastname(); ?> </td> <td><?php echo $emp->get_phone(); ?></td> <td><?php echo $emp->get_office(); ?></td> </tr> <?php } ?> </table> <?php } ?>

You might also like