CSY2028-assignment-1/functions.php

55 lines
1.2 KiB
PHP
Raw Normal View History

2022-11-16 13:27:51 +00:00
<?php
function fetchCats() {
2022-11-20 13:20:58 +00:00
$pdo = startDB();
2022-11-16 19:17:35 +00:00
$stmt = $pdo->prepare('SELECT * FROM category');
2022-11-16 13:27:51 +00:00
$stmt->execute();
$cats = $stmt->fetchAll();
return $cats;
2022-11-16 19:00:51 +00:00
}
2022-11-19 15:38:26 +00:00
function adminCheck() {
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>';
}
}
2022-11-20 13:20:58 +00:00
function startDB() {
$server = 'mysql';
$username = 'student';
$password = 'student';
$schema = 'assignment1';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
return $pdo;
}
2022-11-20 14:44:18 +00:00
function checkListing() {
if (!isset($_GET['listing_id'])) {
echo '<script>window.location.href = "index.php";</script>';
}
}
function getListing() {
$pdo = startDB();
$stmt = $pdo->prepare('SELECT * FROM auction WHERE listing_id = :listing_id');
$values = [
'listing_id' => $_GET['listing_id']
];
$stmt->execute($values);
return $stmt->fetch();
}
function populateCatSelect() {
$cats = fetchCats();
$output = '';
foreach ($cats as &$cat) {
$output .= '<option value="'. $cat['category_id'] .'">'. $cat['name'] .'</option>';
}
return $output;
}
2022-11-16 19:00:51 +00:00
?>