4d696e6820427569 4 years ago
commit d72e85376d

@ -0,0 +1,18 @@
<?php
echo "<html>";
echo "<head>";
echo "<link rel='stylesheet' type='text/css' href='styles.css' />";
echo "</head>";
echo "<body>";
echo "<h3>Add a quote</h3>";
echo "<div class='generalBox inputFields'>";
echo "<form method='post' autocomplete='off' action='controller.php'>";
echo "<textarea id='addQuote' type='textarea' name='addQuote' placeholder='Enter a new quote' cols='70' rows='10'></textarea><br>";
echo "<input id='author' type='text' name='author' placeholder='Author'><br><br>";
echo "<input type='submit' value='Add Quote'>";
echo "</form>";
echo "</div>";
echo "</body>";
echo "</html>";
?>

@ -0,0 +1,18 @@
<?php
include 'model.php';
$theDBA = new DatabaseAdapter();
if (isset($_GET))
if (isset($_GET[ "getQuote" ]) && $_GET[ "getQuote" ] === "true")
echo json_encode ($theDBA->getAllQuotations());
elseif (isset($_POST)) {
// Register/login user
if (isset( $_POST[ "userName" ] ) && isset( $_POST[ "password" ] ) )
echo "";
// Adding quote
if (isset( $_POST[ "addQuote" ] ) && isset( $_POST[ "author" ] ) ) {
$theDBA->addQuote($_POST[ "addQuote" ], $_POST[ "author" ]);
}
}

@ -0,0 +1,18 @@
<?php
echo "<html>";
echo "<head>";
echo "<link rel='stylesheet' type='text/css' href='styles.css' />";
echo "</head>";
echo "<body>";
echo "<h3>Login</h3>";
echo "<div class='generalBox inputFields'>";
echo "<form method='post' autocomplete='off' action='controller.php'>";
echo "<input id='userName' type='text' name='userName' placeholder='Username'><br>";
echo "<input id='password' type='password' name='password' placeholder='Password'><br><br>";
echo "<input type='submit' value='Login'>";
echo "</form>";
echo "</div>";
echo "</body>";
echo "</html>";
?>

@ -0,0 +1,91 @@
<?php
// Minh Bui
class DatabaseAdapter
{
// The instance variable used in every method below.
private $DB;
// Connect to an existing database named 'first'
public function __construct()
{
$dataBase = 'mysql:dbname=quotes;charset=utf8;host=127.0.0.1';
$user = 'root';
$password = '';
try {
$this->DB = new PDO($dataBase, $user, $password);
$this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo ('Error establishing Connection');
exit();
}
}
// Return a PHP array of all columns in all quotations
public function getAllQuotations() {
$sqlStmt = $this->DB->prepare("SELECT * FROM quotations");
$sqlStmt->execute();
return $sqlStmt->fetchAll(PDO::FETCH_ASSOC);
}
// Return a PHP array of all columns in all quotations
public function getAllUsers() {
$sqlStmt = $this->DB->prepare("SELECT * FROM users");
$sqlStmt->execute();
return $sqlStmt->fetchAll(PDO::FETCH_ASSOC);
}
// Return true if the given string $accountName's password matches the given string $psw,
// false if there no match or the user does not exist
public function verifyCredentials($accountName, $psw) {
$sqlStmt = $this->DB->prepare("SELECT username, password FROM users WHERE username='" . $accountName . "' AND password='" . $psw . "'");
$sqlStmt->execute();
return ! empty( $sqlStmt->fetchAll(PDO::FETCH_ASSOC) );
}
// Insert string $quote to the quotations table with the string $author of the quote.
// Set rating and flagged to default values of 0. added should be set to NOW()
public function addQuote($quote, $author) {
$sqlStmt = $this->DB->prepare("INSERT INTO quotations(quote, added, author, rating, flagged) values('" . $quote . "', NOW(), '" . $author . "', 0, 0)");
return $sqlStmt->execute();
}
// Insert a new user.
public function addUser($accountName, $psw) {
$sqlStmt = $this->DB->prepare("INSERT INTO users(username, password) values('" . $accountName . "', '" . $psw . "')");
return $sqlStmt->execute();
}
}
// Run as CLI console app
//$theDBA = new DatabaseAdapter();
// Testing code that should not be run when a part of MVC
//$theDBA->addUser('Dakota','abcd');
//$theDBA->addQuote('Mine too', 'Devon');
/*
if ($theDBA->verifyCredentials('Kim', '1234'))
echo 'works' . PHP_EOL;
else
echo 'broken' . PHP_EOL;
if (! $theDBA->verifyCredentials('Dakota', 'abXX'))
echo 'works' . PHP_EOL;
else
echo 'broken' . PHP_EOL;
if (! $theDBA->verifyCredentials('Not Here', 'abXX'))
echo 'works' . PHP_EOL;
else
echo 'broken' . PHP_EOL;
echo PHP_EOL;
$arr = $theDBA->getAllQuotations();
print_r($arr);
$arr = $theDBA->getAllUsers();
print_r($arr);
*/
?>

@ -0,0 +1,18 @@
<?php
echo "<html>";
echo "<head>";
echo "<link rel='stylesheet' type='text/css' href='styles.css' />";
echo "</head>";
echo "<body>";
echo "<h3>Register</h3>";
echo "<div class='generalBox inputFields'>";
echo "<form method='post' autocomplete='off' action='controller.php'>";
echo "<input id='userName' type='text' name='userName' placeholder='Username'><br>";
echo "<input id='password' type='password' name='password' placeholder='Password'><br><br>";
echo "<input type='submit' value='Register'>";
echo "</form>";
echo "</div>";
echo "</body>";
echo "</html>";
?>

@ -0,0 +1,21 @@
h1 {
font-family: cursive;
text-align: center;
}
.generalBox {
border-radius: 10px;
border: 3px solid black;
margin: 5px;
padding: 7px;
width: auto;
}
.inputFields {
width: min-content;
}
span {
margin: 4px;
}

@ -0,0 +1,43 @@
<?php
echo "<html>";
echo "<head>";
echo "<link rel='stylesheet' type='text/css' href='styles.css' />";
echo "</head>";
echo "<body onload='getQuotes()'>";
echo "<h1 id='banner'>Quotation Service</h1>";
echo "<div id='quotationsList'></div>";
echo "</body>";
echo "</html>";
?>
<script type='text/javascript'>
var ajax = new XMLHttpRequest();
function getQuotes() {
ajax.open("GET", "controller.php?getQuote=true", true);
ajax.send();
}
ajax.onreadystatechange = function() {
if (ajax.readyState == 4 && ajax.status == 200) {
var quotationsDivEle = document.getElementById("quotationsList");
var resultHTML = "";
var response = JSON.parse(ajax.responseText);
if (response.length == 0) {
} else {
for (var i = 0; i < response.length; i++) {
resultHTML += "<div class='generalBox'>";
resultHTML += '"' + response[i]["quote"] + '"</br></br>';
resultHTML += "--" + response[i]["author"] + "</br></br>";
resultHTML += "<span><button type='button'>+</button></span>";
resultHTML += "<span>" + response[i]["flagged"] + "</span>";
resultHTML += "<span><button type='button'>-</button></span>";
resultHTML += "<span><button type='button'>flag</button></span>";
resultHTML += "</div>";
}
quotationsDivEle.innerHTML = resultHTML;
}
}
}
</script>
Loading…
Cancel
Save