added adminCategories

This commit is contained in:
Joshua Perry 2022-11-16 13:27:51 +00:00
parent c08abe7915
commit a50f0bbce1
6 changed files with 54 additions and 16 deletions

13
db.php Normal file
View File

@ -0,0 +1,13 @@
<?php
function fetchCats() {
$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();
return $cats;
}

View File

@ -1,11 +1,12 @@
<?php <?php
session_start();
if (isset($_SESSION['loggedin'])) { if (isset($_SESSION['loggedin'])) {
$logButton = 'href="account/logout.php">Logout'; $logButton = 'href="../account/logout.php">Logout';
} }
else { else {
$logButton = 'href="account/login.php">Login'; $logButton = 'href="../account/login.php">Login';
} }
require_once 'db.php';
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
@ -33,14 +34,7 @@ else {
<nav> <!--TODO: Populate this list from the categories defined by the admins--> <nav> <!--TODO: Populate this list from the categories defined by the admins-->
<ul> <ul>
<?php <?php
$server = 'mysql'; $cats = fetchCats();
$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) { foreach ($cats as &$cat) {
echo '<li><a class="categoryLink" href="../index.php?pageHeading='. urlencode($cat['category_name']) .'">'. $cat['category_name'] .'</a></li>'; echo '<li><a class="categoryLink" href="../index.php?pageHeading='. urlencode($cat['category_name']) .'">'. $cat['category_name'] .'</a></li>';
} }

View File

@ -1,10 +1,11 @@
<?php <?php
session_start();
$pageTitle = 'iBuy - Login'; $pageTitle = 'iBuy - Login';
$pageContent = '<p>Don\'t have an account?<a href=\'register.php\'>Click here to register</a></p> $pageContent = '<p>Don\'t have an account?<a href=\'register.php\'>Click here to register</a></p>
<h1>Login</h1> <h1>Login</h1>
<form action="login.php" method="POST"> <form action="login.php" method="POST">
<label>Email</label> <input name="email" type="text" /> <label>Email</label> <input name="email" type="text" />
<label>Password</label> <input name="password" type="text" /> <label>Password</label> <input name="password" type="password" />
<input name="submit" type="submit" value="Submit" /> <input name="submit" type="submit" value="Submit" />
</form>'; </form>';
$stylesheet = '../assets/ibuy.css'; $stylesheet = '../assets/ibuy.css';
@ -23,10 +24,11 @@ if (isset($_POST['submit'])) {
$user = $stmt->fetch(); $user = $stmt->fetch();
if (password_verify($_POST['password'], $user['password'])) { if (password_verify($_POST['password'], $user['password'])) {
$_SESSION['loggedin'] = $user['user_id']; $_SESSION['loggedin'] = $user['user_id'];
echo'<script>window.location.href = "../index.php";</script>';
if ($user['admin'] === 'y') { if ($user['admin'] === 'y') {
$_SESSION['loggedin'] = 'y'; $_SESSION['admin'] = 'y';
} }
echo'<script>window.location.href = "../index.php";</script>';
} }
else { else {
echo '<p>Unsuccessful Login</p>'; echo '<p>Unsuccessful Login</p>';

View File

@ -1,6 +1,6 @@
<?php <?php
session_start(); session_start();
unset($_SESSION['loggedin']); unset($_SESSION['loggedin']);
header('Location: ../index.php'); unset($_SESSION['admin']);
echo '<p>Logged Out</p>'; echo'<script>window.location.href = "../index.php";</script>';
?> ?>

View File

@ -0,0 +1,28 @@
<?php
session_start();
$pageTitle = 'iBuy - Admin';
$stylesheet = '../assets/ibuy.css';
if(isset($_SESSION['admin'])) {
if($_SESSION['admin'] != 'y') {
//echo'<script>window.location.href = "../index.php";</script>';
}
}
else {
//echo'<script>window.location.href = "../index.php";</script>';
}
require_once '../../db.php';
$pageContent = '<h1>Categories <a href="addCategory.php">Add</a></h1>
<ul>'. populateContent() .'</ul>';
require '../../layout.php';
function populateContent() {
$output = '';
$cats = fetchCats();
foreach ($cats as &$cat) {
$output .= '<li>'. $cat['category_name'] . ' <a href="editCategory.php?category_id='. urlencode($cat['category_id']) .'">edit</a> <a href="deleteCategory.php?category_id='. urlencode($cat['category_id']). '">delete</a></li>';
}
return $output;
}
?>

View File

@ -1,4 +1,5 @@
<?php <?php
session_start();
//Listing display page. Display the 10 auctions finishing soonest //Listing display page. Display the 10 auctions finishing soonest
//Can be used for index, search page, and category listing //Can be used for index, search page, and category listing
$pageTitle = 'iBuy - Home'; $pageTitle = 'iBuy - Home';