Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
37 changes: 37 additions & 0 deletions data_import.sh
Original file line number Diff line number Diff line change
@@ -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;
49 changes: 49 additions & 0 deletions db/mysql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/*
* File: mysql.php
* Description: This handles the database connection for the application
*/
class Database {

private static $handle = null; // connection handler

private $host = 'localhost';
private $dbname = getenv("DB");
private $user = getenv("USER");
private $password = getenv("PASSWD");

// Contructor
public function __construct() {

if (is_null(self::$handle)) {

if (self::$handle = @mysql_connect($host, $user, $password)) {

//set to utf8 encoding
mysql_set_charset('utf8',self::$handle);

if (@mysql_select_db($dbname, self::$handle)) {
return true;
} else {
die('Error:: Unable to select database '.$dbname.' <p><i>'.mysql_error().'</i></p>');
}
} else {
die('Error:: Unable to establish a connection<p><i>'.mysql_error().'</i></p>');
}
}

}

public function query($query) {

if ($result = mysql_query($query, self::$handle)) {
return $result;
}

// [Nice to have] Adding Error handling, connection exception, SQL injection etc.

}

}

?>
37 changes: 37 additions & 0 deletions scripts/data_import.sh
Original file line number Diff line number Diff line change
@@ -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;
109 changes: 109 additions & 0 deletions views/customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/*
* File: customer.php
* Description: A simple webpage that asynchronously loads the JSON into a table when you click a button.
* [Should have] Using DataTables.js to load the data
*/

$customer = new Customer;

class Customer {

public $title;
public $content;

public function __construct() {

$this->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 = '<div><title>'.$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;


}

}

?>
29 changes: 29 additions & 0 deletions webservice/load_data.php
Original file line number Diff line number Diff line change
@@ -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);

}

}

?>