diff --git a/README.md b/README.md index 9b8767a..482c9f5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ +## Instructions + +### Importing CSV data +1. Running data_import.sh + +./data_import.sh + +### PHP web service end point +webservice/load_data.php + +### Customer information web page +views/customer.php + + + +### Original Instructions Below + # PHP JavaScript Developer Test A simple test for PHP / JavaScript Developers diff --git a/data_import.sh b/data_import.sh new file mode 100644 index 0000000..20c5df9 --- /dev/null +++ b/data_import.sh @@ -0,0 +1,37 @@ +#!/bin/bash +#data file data/customers.csv + +# environment variables +DB="dev_test" +USER = "dev_test" +PASSWD="badPw0rd" + + +# create database +mysql -e "CREATE DATABASE $DB" +mysql -e "CREATE USER ${USER}@localhost IDENTIFIED BY '${PASSWD}';" +mysql -e "GRANT ALL PRIVILEGES ON ${DB}.* TO '${USER}'@'localhost';" +mysql -e "FLUSH PRIVILEGES;" + +# create customer table +mysql -e "CREATE TABLE IF NOT EXISTS `contact_table` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `first_name` varchar(255) NOT NULL, + `last_name` varchar(255) NOT NULL, + `email` varchar(255) NOT NULL, + `gender` varchar(255) NOT NULL, + `ip_address` varchar(255) NOT NULL, + `company` varchar(255) NOT NULL, + `city` varchar(255) NOT NULL, + `title` varchar(255) NOT NULL, + `website` varchar(255) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;" + +IFS=, +read; # skip header line +while read id first_name last_name email gender ip_address company city title website + do + echo "INSERT INTO contact_table (id,first_name,last_name,email,gender,ip_address,company,city,title,website) VALUES ('$id', '$first_name', '$last_name', '$email', '$gender', '$ip_address', '$company', '$city', '$title', '$website');" + +done < data/customers.csv | mysql -u dev_test -p badPw0rd dev_test; \ No newline at end of file diff --git a/db/mysql.php b/db/mysql.php new file mode 100644 index 0000000..e5fe672 --- /dev/null +++ b/db/mysql.php @@ -0,0 +1,49 @@ +'.mysql_error().'

'); + } + } else { + die('Error:: Unable to establish a connection

'.mysql_error().'

'); + } + } + + } + + public function query($query) { + + if ($result = mysql_query($query, self::$handle)) { + return $result; + } + + // [Nice to have] Adding Error handling, connection exception, SQL injection etc. + + } + +} + +?> \ No newline at end of file diff --git a/scripts/data_import.sh b/scripts/data_import.sh new file mode 100644 index 0000000..20c5df9 --- /dev/null +++ b/scripts/data_import.sh @@ -0,0 +1,37 @@ +#!/bin/bash +#data file data/customers.csv + +# environment variables +DB="dev_test" +USER = "dev_test" +PASSWD="badPw0rd" + + +# create database +mysql -e "CREATE DATABASE $DB" +mysql -e "CREATE USER ${USER}@localhost IDENTIFIED BY '${PASSWD}';" +mysql -e "GRANT ALL PRIVILEGES ON ${DB}.* TO '${USER}'@'localhost';" +mysql -e "FLUSH PRIVILEGES;" + +# create customer table +mysql -e "CREATE TABLE IF NOT EXISTS `contact_table` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `first_name` varchar(255) NOT NULL, + `last_name` varchar(255) NOT NULL, + `email` varchar(255) NOT NULL, + `gender` varchar(255) NOT NULL, + `ip_address` varchar(255) NOT NULL, + `company` varchar(255) NOT NULL, + `city` varchar(255) NOT NULL, + `title` varchar(255) NOT NULL, + `website` varchar(255) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;" + +IFS=, +read; # skip header line +while read id first_name last_name email gender ip_address company city title website + do + echo "INSERT INTO contact_table (id,first_name,last_name,email,gender,ip_address,company,city,title,website) VALUES ('$id', '$first_name', '$last_name', '$email', '$gender', '$ip_address', '$company', '$city', '$title', '$website');" + +done < data/customers.csv | mysql -u dev_test -p badPw0rd dev_test; \ No newline at end of file diff --git a/views/customer.php b/views/customer.php new file mode 100644 index 0000000..5c03d86 --- /dev/null +++ b/views/customer.php @@ -0,0 +1,109 @@ +title = 'Customer Information'; + $this->content = $this->request(); + + echo $this->content; + + } + + + /** + * Loading the JSON from web serice + **/ + private function request(){ + + // Calling to the web service + $curl_handle = curl_init(); + curl_setopt($curl_handle, CURLOPT_URL, '../webservice/load_data.php'); // [Should have] Loading the base URL + curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); + + // [Should have] check headers and header expection before continue + + $data = json_decode(curl_exec($curl_handle), true); + + // [Should have] check cURL error and http status, and throw execption about the erro + curl_close($curl_handle); + + return $this->getContent($data); + + } + + /** + * Loading Customer data into a simple table without CSS styling. [Should have] using DataTables.js to handle the table view + * @param array $data customer data to load to table + */ + private function getContent($data) { + + $view = '
'.$this->title.'<title></div>'; + + $tmp = ''; + + foreach ($data as $row) { + + $tmp = '<tr>'; + + foreach ($row as $column) { + + $tmp .= '<td>'.$column.'</td>'; + + } + + $tmp .= '</tr>'; + + } + + + // Customer data in a table + $view .= '<div> + <table> + <thead> + <tr> + <th>id</th> + <th>first_name</th> + <th>last_name</th> + <th>email</th> + <th>gender</th> + <th>ip_address</th> + <th>company</th> + <th>city</th> + <th>title</th> + <th>website</th> + </tr> + </thead> + <tbody>' . $tmp . '</tbody> + </table> + </div>'; + + + // Button that asynchronously reqests the customer data + $button = '<div> + <form action="customer.php"> + <input type="submit" value="Refresh Table"> + </form> + </div>'; + + + return $view . $button; + + + } + +} + +?> \ No newline at end of file diff --git a/webservice/load_data.php b/webservice/load_data.php new file mode 100644 index 0000000..5b962c2 --- /dev/null +++ b/webservice/load_data.php @@ -0,0 +1,29 @@ +<?php +/* + * File: load_data.php + * Description: web service that serves the data from the database as JSON + */ + +include_once('../db/mysql.php'); + +$db = new Database; +$data = new LoadData; + +class LoadData { + + // Contructor + public function __construct() { + + $request = 'SELECT * FROM contact_table'; + $result = $this->db->query($request); + + // [Should / nice to have] Adding CSRF, GET / POST handling, webservice exception + + header('Content-type: application/json'); + echo json_encode($result); + + } + +} + +?> \ No newline at end of file