Merge pull request #1 from jpez-development/categories

Categories
This commit is contained in:
Joshua Perry 2022-11-15 15:53:31 +00:00 committed by GitHub
commit 2b0f20ae20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 214 additions and 51 deletions

View File

@ -1,3 +1,13 @@
<?php
session_start();
if (isset($_SESSION['loggedin'])) {
$logButton = 'href="account/logout.php">Logout';
}
else {
$logButton = 'href="account/login.php">Login';
}
?>
<!DOCTYPE html>
<html>
<head>
@ -6,29 +16,36 @@
echo $pageTitle
?>
</title>
<link rel="stylesheet" href="assets/ibuy.css" />
<link rel="stylesheet" href="../assets/ibuy.css" />
</head>
<body>
<header>
<h1><a href="index.php"><span class="i">i</span><span class="b">b</span><span class="u">u</span><span class="y">y</span></a></h1>
<h1><a href="../index.php"><span class="i">i</span><span class="b">b</span><span class="u">u</span><span class="y">y</span></a></h1>
<form action="#">
<input type="text" name="search" placeholder="Search for anything" />
<input type="submit" name="submit" value="Search" />
</form>
</header>
<nav> <!--TODO: Populate this list from the categories defined by the admins-->
<ul>
<li><a class="categoryLink" href="index.php?pageHeading=Home+%26+Garden">Home &amp; Garden</a></li>
<li><a class="categoryLink" href="index.php?pageHeading=Electronics">Electronics</a></li>
<li><a class="categoryLink" href="index.php?pageHeading=Fashion">Fashion</a></li>
<li><a class="categoryLink" href="index.php?pageHeading=Sport">Sport</a></li>
<li><a class="categoryLink" href="index.php?pageHeading=Health">Health</a></li>
<li><a class="categoryLink" href="index.php?pageHeading=Toys">Toys</a></li>
<li><a class="categoryLink" href="index.php?pageHeading=Motors">Motors</a></li>
<li><a class="categoryLink" href="categories.php">More</a></li>
<?php
$server = 'mysql';
$username = 'student';
$password = 'student';
$schema = 'ibuy';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
$stmt = $pdo->prepare('SELECT * FROM categories');
$stmt->execute();
$cats = $stmt->fetchAll();
foreach ($cats as &$cat) {
echo '<li><a class="categoryLink" href="../index.php?pageHeading='. urlencode($cat['category_name']) .'">'. $cat['category_name'] .'</a></li>';
}
?>
<li><a class="categoryLink" <?php echo $logButton?></a></li>
</ul>
</nav>
<img src="../assets/banners/1.jpg" alt="Banner" />

View File

@ -0,0 +1,35 @@
<?php
$pageTitle = 'iBuy - Login';
$pageContent = '<p>Don\'t have an account?<a href=\'register.php\'>Click here to register</a></p>
<h1>Login</h1>
<form action="login.php" method="POST">
<label>Email</label> <input name="email" type="text" />
<label>Password</label> <input name="password" type="text" />
<input name="submit" type="submit" value="Submit" />
</form>';
$stylesheet = '../assets/ibuy.css';
require '../../layout.php';
$server = 'mysql';
$username = 'student';
$password = 'student';
$schema = 'ibuy';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
if (isset($_POST['submit'])) {
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
$values = [
'email' => $_POST['email']
];
$stmt->execute($values);
$user = $stmt->fetch();
if (password_verify($_POST['password'], $user['password'])) {
$_SESSION['loggedin'] = $user['user_id'];
echo'<script>window.location.href = "../index.php";</script>';
if ($user['admin'] === 'y') {
$_SESSION['loggedin'] = 'y';
}
}
else {
echo '<p>Unsuccessful Login</p>';
}
}
?>

View File

@ -0,0 +1,6 @@
<?php
session_start();
unset($_SESSION['loggedin']);
header('Location: ../index.php');
echo '<p>Logged Out</p>';
?>

View File

@ -0,0 +1,37 @@
<?php
function addUser() {
$server = 'mysql';
$username = 'student';
$password = 'student';
$schema = 'ibuy';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
$stmt = $pdo->prepare('INSERT INTO users (first_name, last_name, email, password, admin)
VALUES (:first_name, :last_name, :email, :password, :admin)');
$values = [
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email' => $_POST['email'],
'admin' => 'n',
'password' => password_hash($_POST['password'], PASSWORD_DEFAULT)
];
$stmt->execute($values);
}
$pageTitle = 'iBuy - Register';
$pageContent = '<p>Already have an account?<a href=\'login.php\'>Click here to Login</a></p>
<h1>Register</h1>
<form action="register.php" method="POST">
<label>First Name</label> <input name="first_name" type="text" />
<label>Last Name</label> <input name="last_name" type="text" />
<label>Email</label> <input name="email" type="text" />
<label>Password</label> <input name="password" type="text" />
<input name="submit" type="submit" value="Submit" />
</form>';
require '../../layout.php';
if (isset($_POST['submit'])) {
addUser();
echo '<p>Successful account creation</p>';
}
?>

View File

View File

@ -1,7 +1,6 @@
<?php
//Listing display page. Display the 10 auctions finishing soonest
//Can be used for index, search page, and category listing
$pageTitle = 'iBuy - Home';
if (isset($_GET['pageHeading'])) {
@ -12,23 +11,53 @@ else {
}
$pageContent = '<h1>'.$pageHeading.'</h1>
<ul class="productList">'.populateList().'</ul>';
<ul class="productList">'.populateList($pageHeading).'</ul>';
require '../layout.php';
function populateList() { //TODO: This will need to be updated to populate from the database
function populateList($category) { //TODO: This will need to be updated to populate from the database
$output = '';
for ($i = 0; $i <= 10; $i++) {
$server = 'mysql';
$username = 'student';
$password = 'student';
$schema = 'ibuy';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
if ($category === 'Latest Listings') {
$stmt = $pdo->prepare('SELECT * FROM listings WHERE listing_deadline > "'. date("Y-m-d H:i:s"). '" ORDER BY listing_deadline DESC');
$stmt->execute();
$listings = $stmt->fetchAll();
}
else {
$stmt = $pdo->prepare('SELECT * FROM listings WHERE listing_category = (SELECT category_id FROM categories WHERE category_name = :listing_category)');
$values = [
'listing_category' => $category
];
$stmt->execute($values);
$listings = $stmt->fetchAll();
}
foreach ($listings as &$listing) {
$stmt = $pdo->prepare('SELECT MAX(amount) FROM bids WHERE listing_id = :listing_id');
$values = [
'listing_id' => $listing['listing_id']
];
$stmt->execute($values);
$output .= '<li>
<img src="assets/product.png" alt="product name">
<article>
<h2>Product name</h2>
<h3>Product category</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sodales ornare purus, non laoreet dolor sagittis id. Vestibulum lobortis laoreet nibh, eu luctus purus volutpat sit amet. Proin nec iaculis nulla. Vivamus nec tempus quam, sed dapibus massa. Etiam metus nunc, cursus vitae ex nec, scelerisque dapibus eros. Donec ac diam a ipsum accumsan aliquet non quis orci. Etiam in sapien non erat dapibus rhoncus porta at lorem. Suspendisse est urna, egestas ut purus quis, facilisis porta tellus. Pellentesque luctus dolor ut quam luctus, nec porttitor risus dictum. Aliquam sed arcu vehicula, tempor velit consectetur, feugiat mauris. Sed non pellentesque quam. Integer in tempus enim.</p>
<p class="price">Current bid: £123.45</p>
<a href="listing.php" class="more auctionLink">More &gt;&gt;</a>
<h2>'. $listing['listing_name'] .'</h2>
<h3>'. $listing['listing_category'] .'</h3>
<p>'. $listing['listing_description'] .'</p>
<p class="price">Current bid:'. $stmt->fetch()['MAX(amount)'] .'</p>
<a href="listing.php?listing_id='. $listing['listing_id'] .'" class="more auctionLink">More &gt;&gt;</a>
</article>
</li>';
}
return $output;
}
?>

View File

@ -2,23 +2,61 @@
$pageTitle = 'iBuy - Product Listing';
//TODO: have page populate information based on listing in the database
$pageContent = '<h1>Product Page</h1>
<article class="product">
<article class="product">'. populateContent() .'</article>';
<img src="product.png" alt="product name">
require '../layout.php';
function populateContent() {
$server = 'mysql';
$username = 'student';
$password = 'student';
$schema = 'ibuy';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
$stmt = $pdo->prepare('SELECT * FROM listings WHERE listing_id= :listing_id');
$values = [
'listing_id' => $_GET['listing_id']
];
$stmt->execute($values);
$listing = $stmt->fetch();
$stmt = $pdo->prepare('SELECT * FROM categories WHERE category_id = :category_id');
$values = [
'category_id' => $listing['listing_category']
];
$stmt->execute($values);
$category = $stmt->fetch();
$stmt = $pdo->prepare('SELECT MAX(amount) FROM bids WHERE listing_id = :listing_id');
$values = [
'listing_id' => $listing['listing_id']
];
$stmt->execute($values);
$bid = $stmt->fetch();
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
$values = [
'email' => $listing['listing_email']
];
$stmt->execute($values);
$user = $stmt->fetch();
$output = ' <img src="product.png" alt="product name">
<section class="details">
<h2>Product name</h2>
<h3>Product category</h3>
<p>Auction created by <a href="#">User.Name</a></p>
<p class="price">Current bid: £123.45</p>
<time>Time left: 8 hours 3 minutes</time>
<h2>'. $listing['listing_name'] .'</h2>
<h3>'. $category['category_name'] .'</h3>
<p>Auction created by <a href="#">'. $user['first_name'].$user['last_name'] .'</a></p>
<p class="price">Current bid: '. $bid['MAX(amount)'] .'</p>
<time>Time left:'. round((strtotime($listing['listing_deadline']) - strtotime(date('Y-m-d H:i:s')))/60,1 ) .' Minutes</time>
<form action="#" class="bid">
<input type="text" name="bid" placeholder="Enter bid amount" />
<input type="submit" value="Place bid" />
</form>
</section>
<section class="description">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sodales ornare purus, non laoreet dolor sagittis id. Vestibulum lobortis laoreet nibh, eu luctus purus volutpat sit amet. Proin nec iaculis nulla. Vivamus nec tempus quam, sed dapibus massa. Etiam metus nunc, cursus vitae ex nec, scelerisque dapibus eros. Donec ac diam a ipsum accumsan aliquet non quis orci. Etiam in sapien non erat dapibus rhoncus porta at lorem. Suspendisse est urna, egestas ut purus quis, facilisis porta tellus. Pellentesque luctus dolor ut quam luctus, nec porttitor risus dictum. Aliquam sed arcu vehicula, tempor velit consectetur, feugiat mauris. Sed non pellentesque quam. Integer in tempus enim.</p>
<p>'. $listing['listing_description'] .'</p>
</section>
@ -37,7 +75,8 @@ $pageContent = '<h1>Product Page</h1>
<input type="submit" name="submit" value="Add Review" />
</form>
</section>
</article>';
require '../layout.php'
</section>';
return $output;
}
?>