Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rachel Lum Final Project #6

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*.css.map
*.sass.map
*.scss.map
sftp-config.json
.DS_Store
3 changes: 3 additions & 0 deletions lum.rachel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*DS_Store
*sftp-config.json
*auth.php
12 changes: 12 additions & 0 deletions lum.rachel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Rachel Lum


## Related Links
- http://rachellauren.work/wnm608/lum.rachel/index.php
- http://rachellauren.work/wnm608/lum.rachel/styleguide/
- http://rachellauren.work/wnm608/lum.rachel/admin/


## Extra Links
- http://rachellauren.work/wnm608/lum.rachel/notes/
- http://rachellauren.work/wnm608/lum.rachel/notes/reading_data.php
48 changes: 48 additions & 0 deletions lum.rachel/added_to_cart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

include_once "lib/php/functions.php";
include_once "parts/templates.php";

$product = makeQuery(makeConn(),"SELECT * FROM `products` WHERE `id`=".$_GET['id'])[0];

$cart_product = cartItemBy($_GET['id']);
?>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Added to Cart</title>

<?php include "parts/meta.php"; ?>

</head>
<body>

<?php include "parts/navbar.php"; ?>

<div class="container">
<p><a href="product_item.php?id=<?= $_GET['id'] ?>">Back</a></p>
</div>



<div class="container">
<div class="card soft">


<h2>You added <?= $product->name ?> to your cart</h2>
<p>There are now <?= $cart_product->amount ?> of <?= $product->name ?> in your cart.</p>
<div class="display-flex">

<div class="flex-none"><h3><a href="product_list.php">Keep Shopping</a></h3></div>
<div class="flex-stretch"></div>
<div class="flex-none"><h3><a href="cart.php">View Cart</a></h3></div>
</div>
</div>
</div>


</body>
</html>
258 changes: 258 additions & 0 deletions lum.rachel/admin/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
<?php

include "../lib/php/functions.php";

$empty_product = (object)[
"name"=>"Laura Mercier Primer",
"price"=>"35.00",
"category"=>"face",
"description"=>"Hydrating Face Primer",
"thumbnail"=>"face_lauramercier_primer.jpg",
"images"=>"face_lauramercier_primer.jpg"
];



//Logic
try{
$conn = makePDOConn();
switch($_GET['action']) {
case "update" :
$statement = $conn->prepare("UPDATE
`products`
SET
`name`=?,
`price`=?,
`category`=?,
`description`=?,
`thumbnail`=?,
`images`=?,
`date_modify`=NOW()
WHERE `id`=?
");
$statement->execute([
$_POST['product-name'],
$_POST['product-price'],
$_POST['product-category'],
$_POST['product-description'],
$_POST['product-thumbnail'],
$_POST['product-images'],
$_GET['id']
]);

header("location:{$_SERVER['PHP_SELF']}?id={$_GET['id']}");
break;

case "create" :
$statement = $conn->prepare("INSERT INTO
`products`
(
`name`,
`price`,
`category`,
`description`,
`thumbnail`,
`images`,
`date_modify`,
`date_create`
)
VALUES (?,?,?,?,?,?,NOW(),NOW())
");
$statement->execute([
$_POST['product-name'],
$_POST['product-price'],
$_POST['product-category'],
$_POST['product-description'],
$_POST['product-thumbnail'],
$_POST['product-images'],
]);
$id = $conn->lastInsertId();
header("location:{$_SERVER['PHP_SELF']}?id=$id");
break;

case "delete" :
$statement = $conn->prepare("DELETE FROM `products` WHERE id=?");
$statement->execute([$_GET['id']]);
header("location:{$_SERVER['PHP_SELF']}");
break;

}
} catch(PDOException $e){
die($e->getMessage());
}



//Templates
function productListItem($r,$o) {
return $r.<<<HTML
<div class="card soft">
<div class="display-flex">
<div class="flex-none images-thumbs"><img src='img/$o->thumbnail'></div>
<div class="flex-stretch" style="padding:1em">$o->name</div>
<div class="flex-none"><a href="{$_SERVER['PHP_SELF']}?id=$o->id" class="form-button">Edit</a></div>
</div>
</div>
HTML;
}

function showProductPage($o) {
$id = $_GET['id'];
$addoredit = $id == "new" ? "Add" : "Edit";
$creatorupdate = $id == "new" ? "create" : "update";
$images = array_reduce(explode(", ", $o->images),function($r,$o){return $r."<img src='img/$o'>";});

//heredoc
$display = <<<HTML
<div>
<h2>$o->name</h2>
<div class="form-control">
<label class="form-label">Price</label>
<span>&dollar;$o->price</span>
</div>
<div class="form-control">
<label class="form-label">Category</label>
<span>$o->category</span>
</div>
<div class="form-control">
<label class="form-label">Description</label>
<span>$o->description</span>
</div>
<div class="form-control">
<label class="form-label">Thumbnail</label>
<span class="images-thumbs"><img src='img/$o->thumbnail'></span>
</div>
<div class="form-control">
<label class="form-label">Other Images</label>
<span class="images-thumbs">$o->images</span>
</div>

</div>
HTML;


$form = <<<HTML

<form method="post" action="{$_SERVER['PHP_SELF']}?id=$id&action=$creatorupdate">
<h2>$addoredit Product</h2>
<div class="form-control">
<label class="form-label" for="product-name">Name</label>
<input class="form-input" name="product-name" id="product-name" type="text" value="$o->name" placeholder="Enter Product Name">
</div>
<div class="form-control">
<label class="form-label" for="product-price">Price</label>
<input class="form-input" name="product-price" id="product-price" type="Number" value="$o->price" placeholder="Enter Product Price">
</div>
<div class="form-control">
<label class="form-label" for="product-category">Category</label>
<input class="form-input" name="product-category" id="product-category" type="text" value="$o->category" placeholder="Enter Product Category">
</div>
<div class="form-control">
<label class="form-label" for="product-description">Description</label>
<textarea class="form-input" name="product-description" id="product-description" value="$o->description" placeholder="Enter product description"></textarea>
</div>
<div class="form-control">
<label class="form-label" for="product-thumbnail">Thumbnail</label>
<input class="form-input" name="product-thumbnail" id="product-thumbnail" type="text" value="$o->thumbnail" placeholder="Enter Product Thumbnail">
</div>
<div class="form-control">
<label class="form-label" for="product-images">Images</label>
<input class="form-input" name="product-images" id="product-images" type="text" value="$o->images" placeholder="Enter Product Images">
</div>

<div class="form-control">
<input class="form-button" type="submit" value="Save Changes">
</div>
</form>

HTML;

$output = $id == "new" ? "<div class='card soft'>$form</div>" :
"<div class='grid gap'>
<div class='col-xs-12 col-md-7'><div class='card soft'>$display</div></div>
<div class='col-xs-12 col-md-5'><div class='card soft'>$form</div></div>
</div>";



$delete = $id == "new" ? "" : "<a href='{$_SERVER['PHP_SELF']}?id=$id&action=delete'>Delete</a>";


echo <<<HTML
<div class="card soft">
<nav class="display-flex">
<div class="flex-stretch"><a href="{$_SERVER['PHP_SELF']}">Back</a></div>
<div class="flex-none">$delete</div>
</nav>
</div>
$output
HTML;
}




?>
<! DOCTYPE html>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Product Admin Page</title>

<?php include "../parts/meta.php"; ?>

</head>
<body>

<header class="navbar">
<div class="container display-flex">
<div class="flex-none">
<h1>Product Admin</h1>
</div>
<div class="flex-stretch"></div>
<nav class="nav nav-flex flex-none">
<ul>
<li><a href="<?= $_SERVER['PHP_SELF'] ?>">Product List</a></li>
<li><a href="<?= $_SERVER['PHP_SELF'] ?>?id=new">Add New Product</a></li>
</ul>
</nav>
</div>
</header>


<div class="container">
<?php

if(isset($_GET['id'])){

echo showProductPage(
$_GET['id']=="new" ?
$empty_product :
makeQuery(makeConn(), "SELECT * FROM `products` WHERE `id`=".$_GET['id'])[0]
);

} else {

?>

<h2>Product List</h2>

<?php

$result= makeQuery(makeConn(), "SELECT * FROM `products` ORDER BY `date_create` DESC");

echo array_reduce($result,'productListItem');

?>

<?php }?>

</div>

</body>




Loading