[php]// Call the form
new SimpleForm();
// ---------------------------------------------------------------
// There are three places in SimpleForm that need to be coded
// ---------------------------------------------------------------
class SimpleForm
{
/**
* @var object Holds the Model object
*/
var $model = null;
/**
* @var object Holds the View object
*/
var $view = null;
/**
* Starting point for our SimpleForm form.
*
* return void
*/
function __construct() {
$this->model = new SimpleFormModel;
$this->view = new SimpleFormView;
$this->controller();
}
/**
* A standard controller
*
* Possible actions:
* - Display the form (with possible errors)
* - Process the form and display the receipt on success (success.tpl)
*
* @return void
*/
function controller() {
// --- ADD CODE HERE ---
}
/**
* Checks the submitted form data and saves it to the database.
*
* @return bool TRUE if everything was successful, false otherwise
*/
function processSubmission() {
// --- ADD CODE HERE ---
}
/**
* Checks the submitted form data
*
* @return mixed TRUE if all input checks pass, an array of error messages otherwise
*/
function checkInput() {
$errors = array();
// First name must not be empty
// Last name must not be empty
// Email must not be empty and must be a valid email string
// At least 1 product must have been ordered
// --- ADD CODE HERE ---
if(count($errors)) {
return $errors;
}
return true;
}
}
// ---------------------------------------------------------------
// There are two places in SimpleFormModel that need to be coded
// ---------------------------------------------------------------
/**
-
Model class
*/
class SimpleFormModel
{/**
*/
var $db = NULL;/**
-
Constructor. Connects to the database server.
-
@return void
*/
function __construct() {
// Create a mysqli connection to the database using these credentials
// and save the connection resource to $this->db:
// host: localhost
// user: charmedi_testapp
// pass: simple
// database: charmedi_testapp// — ADD CODE HERE —
}
/**
-
Saves data into the database
-
Submissions are stored in two tables:
-
submissions
- Holds the submitter’s name and email -
products_ordered
- Holds how much of each product the submitter ordered -
The products are stored in a
products
table, but you shouldn’t need -
anything from there to save. Here are the structure of all three tables:
-
submissions
-
`id` int - Primary Key, auto-incrementing
-
`first_name` varchar(64)
-
`last_name` varchar(64)
-
`email` varchar(64)
-
products
-
`id` int - Primary Key, auto-incrementing
-
`name` varchar(64)
-
`price` float
-
products_ordered
-
`id_submissions` int - `submissions` table foreign key
-
`id_products` int - `products` table foreign key
-
`quantity` int - # of product ordered by the submitter for this product
-
`ordered_on` timestamp - timestamp
-
@param array $data prepared data to insert into the database
-
@return mixed TRUE if the save was successful otherwise an array of error messages.
*/
function save($data) {// — ADD CODE HERE —
}
/**
-
Gets the product data from the DB
-
@return array 2-dimensional array of
products
data from the DB
*/
function getProducts() {$rows = $this->db->query(‘SELECT * FROM
products
ORDER BYname
ASC’);$return = array();
while ($row = $rows->fetch_assoc()) {
$return[] = $row;
}
return $return;
}
}
// ---------------------------------------------------------------
// There is one place in SimpleFormView that needs to be coded
// ---------------------------------------------------------------
/**
-
View class
/
class SimpleFormView
{
/*-
Displays a template file to output buffer
-
@param string $tpl The file name of the template file to display
-
@param array $data any data that should be available to the template file being displayed
-
@return bool True if the template file was able to be read and outputted successfully
*/
function display($tpl, $data=array()) {if(!isset($data[‘POST’])) $this->addPost($data);
// Tip: The template files contain PHP code so they must be read
// in a manner that evalutates/executes that code.// — ADD CODE HERE —
}
/**
- Cleans _POST data and adds it to the POST key of the input array
- this is used to auto-populate form fields when the form needs
- to reload after an error. It’ll help prevent against JS injection
- attacks.
- @param array &$data
-
@return void
*/
function addPost(&$data) {
if(count($_POST)) {
$data[‘POST’] = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);
} else {
$data[‘POST’] = array(
‘first_name’ => ‘’,
‘last_name’ => ‘’,
‘email’ => ‘’,
);
}
}
}
?>[/php]
-