Skip to content

Commit

Permalink
PRJ666 Update
Browse files Browse the repository at this point in the history
  • Loading branch information
jevuu committed May 14, 2018
1 parent 5232594 commit ec7d817
Show file tree
Hide file tree
Showing 9 changed files with 737 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/build
/captures
.externalNativeBuild
PHP/conn.php
98 changes: 98 additions & 0 deletions PHP/getTestReceipt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

require "conn.php";

//Get user
//By ID
echo "<p>GET John Doe BY userID <br />";
$qString = "SELECT name FROM users WHERE userID = 1";
$resultName = mysqli_query($conn, $qString);
if(mysqli_num_rows($resultName) > 0){
$row = mysqli_fetch_row($resultName);
echo "Found: " . $row[0] . "<br />";
}else{
echo "No user found. <br />";
}
echo "</p>";

//By name
echo "<p>GET John Doe BY name <br />";
$qString = "SELECT name FROM users WHERE name = 'John Doe'";
$resultName = mysqli_query($conn, $qString);
if(mysqli_num_rows($resultName) > 0){
$row = mysqli_fetch_row($resultName);
echo "Found: " . $row[0] . "<br />";
}else{
echo "No user found. <br />";
}
echo "</p>";

//Get items
echo "<p>Get all items<br />";
$qString = "SELECT name, description FROM items";
$resultItems = mysqli_query($conn, $qString);
if(mysqli_num_rows($resultItems) > 0){
while($row = mysqli_fetch_row($resultItems)){
echo "Found Item: " . $row[0] . " - " . $row[1] . "<br />";
}
}else{
echo "No items found. <br />";
}
echo "</p>";

//Get all items in DB, show creators
echo "<p>Get all items from database from creator John Doe<br />";
$qString = "SELECT i.name, u.name
FROM items i
INNER JOIN users u ON i.userID = u.userID
WHERE i.userID = 1 ";
$resultItems = mysqli_query($conn, $qString);
if(mysqli_num_rows($resultItems) > 0){
while($row = mysqli_fetch_row($resultItems)){
echo "Found Item: " . $row[0] . " was created by " . $row[1] . "<br />";
}
}else{
echo "No items found. <br />";
}
echo "</p>";

//Build a receipt
echo "<p>Build an example receipt for John Doe.<br />";
$userID = 1;
$receiptID = 1;
$qString = "SELECT u.name, r.receiptID, r.creationDate, r.totalCost, r.tax
FROM users u
INNER JOIN receipts r ON r.userID = u.userID
WHERE u.userID =" . $userID . " AND r.receiptID =" . $receiptID;
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) > 0){
$row = mysqli_fetch_row($result);
$totalCost = $row[3];
$tax = $row[4];
echo "<table border=1><tr><th colspan=2>Receipt for " . $row[0] . "</th></tr>";
echo "<tr><td>Receipt: " . $row[1] . "</td><td> Created on: " . $row[2] . "</td></tr>";

$qString = "SELECT i.name, i.description, i.price
FROM items i
INNER JOIN receiptItems ri ON ri.itemID = i.itemID
INNER JOIN receipts r ON r.receiptID = ri.receiptID
WHERE r.receiptID =" . $receiptID;
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) > 0){
if ($totalCost > 0)
$dontSum = true;
while($row = mysqli_fetch_row($result)){
echo "<tr><td>" . $row[0] . "<br />" . $row[1] . "</td><td>" . $row[2] . "</td></tr>";
if(!$dontSum)
$totalCost += $row[2];
}
}else{
echo "<tr><td colspan=2> No items found. </td></tr>";
}
echo "<tr><td>Total</td><td>" . $totalCost . "</td></tr>";
echo "</table>";
}else{
echo "No data found.";
}

?>
8 changes: 7 additions & 1 deletion PHP/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
echo "No data.";
}

?>
?>
<!--
<form action="login.php" method="post">
<input type="text" name="userName">
<input type="submit">
</form>
--!>
9 changes: 8 additions & 1 deletion PHP/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

require "conn.php";

$userName = $_POST["userName"];
$json = file_get_contents('php://input');
$obj = json_decode($json);
$userName = $obj->{'userName'};

//$userName = $_POST["userName"];
//$password = $_POST["password"];

$qString = "SELECT * FROM test WHERE userName='$userName'";
Expand All @@ -17,7 +21,10 @@
echo $row[0] . " " . $row[2] . " " . $row[3] . " " . $row[4];
}
}else{

//var_dump($_POST);
echo "Failed. Maybe something went wrong? Here's the query: " . $qString;
//echo "var dump: " . $varDump;
}

?>
135 changes: 135 additions & 0 deletions PHP/makeGetDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

require "conn.php";

echo "<hr /><h2>Create a user named Donatello, create a receipt for him, add items to receipt, display receipt, delete Donatello (and all receipts and items)</h2>";

$uID = 0;
$rID = 0;
$iID = array();

//BIG TEST BELOW
//Create user
$userName = "Donatello";
$qString = "INSERT INTO users (`name`,`creationDate`) VALUES ('" . $userName . "', CURRENT_DATE)";
mysqli_query($conn, $qString);

$qString = "SELECT userID FROM users WHERE name='" . $userName. "'";
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) == 1){
$row = mysqli_fetch_row($result);
$uID = $row[0];
echo "User created! User ID: " . $uID . "<br /><br />";
}else{
echo "Failed to create user or multiple users.<br /><br />";
}

//Create receipt
$qString = "INSERT INTO receipts (`userID`, `creationDate`) VALUES(" . $uID . ", CURRENT_DATE)";
mysqli_query($conn, $qString);

$qString = "SELECT receiptID FROM receipts WHERE userID =" . $uID; //This only works because only one receipt was ever created for this user. Won't work in real world.
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) == 1){
$row = mysqli_fetch_row($result);
$rID = $row[0];
echo "Receipt created! Receipt ID: " . $rID . "<br /><br />";
}else{
echo "Failed to create receipt, or multiple receipts.<br /><br />";
}

//Add items
$qString = "INSERT INTO items(`userID`, `name`, `price`) VALUES(" . $uID . ", 'Bread', '8.50'), (" . $uID . ", 'Milk', '9.50')";
mysqli_query($conn, $qString);

$qString = "SELECT itemID FROM items WHERE userID = " . $uID;
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) == 2){
echo "Success! Two items created.";
while($row = mysqli_fetch_row($result)){
array_push($iID, $row[0]);
}
}else{
echo "Error, no items or not 2 items created.<br /><br />";
}

//Link tables
$qString = "INSERT INTO receiptItems(`receiptID`, `itemID`) VALUES(" . $rID . "," . $iID[0] . "), (" . $rID . "," . $iID[1] . ")";
mysqli_query($conn, $qString);

//Build receipt
echo "<p>Build an example receipt for Donatello.<br />";
$qString = "SELECT u.name, r.creationDate, r.totalCost, r.tax
FROM users u
INNER JOIN receipts r ON r.userID = u.userID
WHERE u.userID =" . $uID . " AND r.receiptID =" . $rID;
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) > 0){
$row = mysqli_fetch_row($result);
$totalCost = $row[2];
$tax = $row[3];
echo "<table border=1><tr><th colspan=2>Receipt for " . $row[0] . "</th></tr>";
echo "<tr><td colspan=2> Created on: " . $row[1] . "</td></tr>";

$qString = "SELECT i.name, i.description, i.price
FROM items i
INNER JOIN receiptItems ri ON ri.itemID = i.itemID
INNER JOIN receipts r ON r.receiptID = ri.receiptID
WHERE r.receiptID =" . $rID;
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) > 0){
if ($totalCost > 0)
$dontSum = true;
while($row = mysqli_fetch_row($result)){
echo "<tr><td>" . $row[0] . "<br />" . $row[1] . "</td><td>" . $row[2] . "</td></tr>";
if(!$dontSum)
$totalCost += $row[2];
}
}else{
echo "<tr><td colspan=2> No items found. </td></tr>";
}
echo "<tr><td>Total</td><td>" . $totalCost . "</td></tr>";
echo "</table><br /><br />";
}else{
echo "No data found. <br /><br />";
}

//Delete Dontello
echo "Delete Donatello by userID, which should automatically delete all receipts and items belonging to him. <br /><br />";

$qString = "DELETE FROM users WHERE userID = " . $uID;
mysqli_query($conn, $qString);

$qString = "SELECT name FROM users WHERE userID = " . $uID;
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) == 0){
echo "Donatello was deleted! <br />";
}else{
echo "Donatello was not deleted! <br />";
}

$qString = "SELECT receiptID FROM receipts WHERE userID = " . $uID;
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) == 0){
echo "Donatello's receipt was deleted! <br />";
}else{
echo "Donatello receipt was not deleted! <br />";
}

$qString = "SELECT itemID FROM items WHERE userID = " . $uID;
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) == 0){
echo "Donatello's items were deleted! <br />";
}else{
echo "Donatello's items were not deleted! <br />";
}

$qString = "SELECT itemID FROM receiptItems WHERE receiptID = " . $rID;
$result = mysqli_query($conn, $qString);
if(mysqli_num_rows($result) == 0){
echo "Donatello's data in link table for receipt and items was deleted! <br />";
}else{
echo "Donatello's data in link table for receipt and items was not deleted! <br />";
}

?>
22 changes: 22 additions & 0 deletions PHP/testForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<html>
<head>
<title>
e-Ceipt Data Entry Test Form
</title>
</head>

<body>
<form action="testResult.php" method="post">
Customer name: <input type="text" name="userName" placeholder="Enter a name"><br />
Items purchased: (For every item name, a price MUST be present)<br />
<ol>
<li><input type="text" name="items[itemName][]" placeholder="Item name">...<input type="text" name="items[itemPrice][]" placeholder="Price"></li>
<li><input type="text" name="items[itemName][]" placeholder="Item name">...<input type="text" name="items[itemPrice][]" placeholder="Price"></li>
<li><input type="text" name="items[itemName][]" placeholder="Item name">...<input type="text" name="items[itemPrice][]" placeholder="Price"></li>
<li><input type="text" name="items[itemName][]" placeholder="Item name">...<input type="text" name="items[itemPrice][]" placeholder="Price"></li>
<li><input type="text" name="items[itemName][]" placeholder="Item name">...<input type="text" name="items[itemPrice][]" placeholder="Price"></li>
</ol>
<input type="submit">
</form>
</body>
</html>
Loading

0 comments on commit ec7d817

Please sign in to comment.