Created
June 27, 2015 22:21
-
-
Save LukeXF/8d21ae2008ca5ba813ad to your computer and use it in GitHub Desktop.
A PHP Test - Complete the functions
This file contains hidden or 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 | |
// Testing | |
// please fill-in the body of the functions | |
// using methods to produce the suggested output | |
/* config */ | |
$db['host'] = "localhost"; | |
$db['name'] = "luke_lsm"; | |
$db['user'] = "luke_lsm"; | |
$db['pass'] = "DdEPLTvLZhNY6Esy"; | |
$input = array( | |
array('filename' => 'home.html', 'title' => 'home page'), | |
array('filename' => 'products.html', 'title' => 'products'), | |
array('filename' => 'contact.html', 'title' => 'contact'), | |
array('filename' => 'prices.html', 'title' => 'prices') | |
); | |
$filename = 'coverage.csv'; | |
/* function tests */ | |
echo remove_nonAtoZ("BA'3Ndf$%^A&*(nN)A")."</br>"."</br>"; | |
echo first_char_caps('TEST DATA bleh Orange')."</br>"."</br>"; | |
echo date_8_hours_ago()."</br>"."</br>"; | |
echo add_accesskeys($input); | |
echo table_columns(array('apple', 'orange', 'monkey', 'potato', 'cheese', 'badger', 'turnip'), 2)."</br>"."</br>"; | |
// $output = load_csv($filename); | |
// var_dump($output)."</br>"; | |
function remove_nonAtoZ($input) { | |
$output = preg_replace("/[^A-Z]+/", "", $input); | |
return $output; | |
} | |
function first_char_caps($input) { | |
$output = ucwords( strtolower($input) ); | |
return $output; | |
} | |
function load_csv($filename) { | |
global $db; // get database info | |
// get the csv data | |
$file = fopen($filename,"r"); | |
$data = fgetcsv($file,1000,",","'"); | |
// PDO MySQL insert | |
$conn = new PDO('mysql:host=' . $db['host'] . ';dbname=' . $db['name'] . ';charset=utf8', $db['user'], $db['pass']); | |
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$sql = $conn->prepare("INSERT INTO csv_data (user_first, user_last, user_email) VALUES (:first, :last, :email)"); | |
$sql->bindParam(':first', $data[0]); | |
$sql->bindParam(':last' , $data[1]); | |
$sql->bindParam(':email', $data[2]); | |
$sql->execute(); | |
return $data; | |
} | |
function date_8_hours_ago() { | |
date_default_timezone_set('Europe/London'); | |
$date = date('D jS M g:ia', strtotime('-8 hour')); | |
echo $date; | |
} | |
function email_postvars() { | |
// include the Mandrill PHP Wrapper | |
require 'lib/mandrill/src/Mandrill.php'; | |
$mandrill = new Mandrill($mykey); | |
// generate the message that is sent to the user upon sending an email | |
$message = array( | |
// the subject will be sent as the email subject. | |
'subject' => 'Your information ' . $_POST['forename'] . ' ' . $_POST['surname'], | |
'from_email' => '[email protected]', | |
'from_name' => 'Luke Brown', | |
'to' => array( | |
array('email' => $_POST['email'], 'name' => $user_name) | |
) | |
); | |
$email_name = 'test'; // the mandrill template name | |
$extraCounter = 0; // how many extra values have been found | |
// loop through the post array to find the extra_ prefix | |
for ($i=0; $i < count($_POST); $i++) { | |
// if the post array contains the exra prefix | |
if (strpos($_POST, 'extra_') === true) { | |
$key[$extraCounter] = str_replace('extra_', "", array_keys($_POST[$i])); // get key (example: age or hobbies) as a word) | |
$extra[$extraCounter] = $_POST[$i]; // get the value of extra prefix array | |
$extraCounter++; | |
} | |
} | |
// build the array for the content that is extra data | |
for ($i=0; $i < count($key); $i++) { | |
$outputExtraData .= "<b>" . $key[$i] . ":</b> " . $extra[$i] . "<br>"; | |
} | |
// email body | |
$email_content = array( | |
array( | |
'name' => 'main', // set it to Mandrill's main email body | |
'content' => 'Your extra content:<br>' . $outputExtraData) // output the array in the new format | |
); | |
// check if sent successfully | |
$returned_message = $mandrill->messages->sendTemplate($email_name, $email_content, $message); | |
if($returned_message[0]['status'] == "sent") { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function table_columns($input, $cols) { | |
echo "<table>"; | |
$eachCol = ceil(count($input) / $cols); // findout how many rows in a column | |
$itemCounter = 0; // for looping through each input array | |
// while counter is less than the number of columns (build the columns) | |
for ($i=0; $i < $cols; $i++) { | |
// while counter is less than the number of item in a column (fill the columns with data) | |
for ($f=0; $f < $eachCol; $f++) { | |
$list[$i][$f] = $input[$itemCounter]; | |
$itemCounter++; | |
} | |
} | |
// output each column (4) | |
for ($i=0; $i < $eachCol; $i++) { | |
echo "<tr>"; | |
// output each row (2) | |
for ($j=0; $j < $cols; $j++) { | |
echo "<td>" . $list[$j][$i] . "</td>"; | |
} | |
echo "</tr>"; | |
} | |
echo "</table>"; | |
} | |
function add_accesskeys($input) { | |
$output = "<ul>"; | |
$i = 0; | |
while ($i < count($input)) { | |
// split the titles up | |
$title = $input[$i]["title"]; | |
$firstLetter = substr($title, 0, 1); | |
$restOfString = substr($title, 1, strlen($title)); | |
$output .= "<li><a href='" . $input[$i]['filename'] . "' accesskey='" . $firstLetter . "'><em>" . $firstLetter . "</em>" . $restOfString . "</a></li>"; | |
$i++; | |
} | |
$output .= '</ul>'; | |
return $output; | |
} | |
function returnKey($array) { | |
/*example data | |
$array[0] = 3, $array[1] = -2, $array[2] = 5, $array[3] = 3, $array[4] = 1, $array[5] = -2, $array[6] = 7 | |
3 -2 5 3 1 -2 7 | |
-2 -2 1 3 3 5 7 | |
//function | |
return 3; | |
return index (int) 3 because sum of all values before $array[3] (6) equal sum of all values above $array[3] (6) | |
write function that will return first index of any array that meets the above condition, if no match -1; | |
assume indices are all integers ordered by size ASC already and values are all numbers; | |
*/ | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment