CSY2028-assignment-1/public/index.php

64 lines
2.0 KiB
PHP
Raw Normal View History

2022-10-22 15:03:47 +00:00
<?php
2022-11-16 13:27:51 +00:00
session_start();
2022-10-22 15:03:47 +00:00
//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'])) {
$pageHeading = $_GET['pageHeading'];
}
else {
$pageHeading = 'Latest Listings';
}
$pageContent = '<h1>'.$pageHeading.'</h1>
<ul class="productList">'.populateList($pageHeading).'</ul>';
2022-10-22 15:03:47 +00:00
require '../layout.php';
2022-11-15 15:30:12 +00:00
function populateList($category) { //TODO: This will need to be updated to populate from the database
2022-10-22 15:03:47 +00:00
$output = '';
$server = 'mysql';
$username = 'student';
$password = 'student';
$schema = 'ibuy';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
if ($category === 'Latest Listings') {
2022-11-15 15:30:12 +00:00
$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 {
2022-11-15 15:43:46 +00:00
$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);
2022-10-22 15:03:47 +00:00
$output .= '<li>
<img src="assets/product.png" alt="product name">
<article>
<h2>'. $listing['listing_name'] .'</h2>
<h3>'. $listing['listing_category'] .'</h3>
<p>'. $listing['listing_description'] .'</p>
2022-11-15 15:43:46 +00:00
<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>
2022-10-22 15:03:47 +00:00
</article>
</li>';
2022-10-22 15:03:47 +00:00
}
2022-10-22 15:03:47 +00:00
return $output;
}
?>