CSY2028-assignment-1/public/index.php

63 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';
}
2022-11-20 13:20:58 +00:00
require_once '../functions.php';
2022-10-22 15:03:47 +00:00
2022-11-20 14:10:05 +00:00
$pageContent = '<a href="account/addAuction.php">post auction</a>
<h1>'.$pageHeading.'</h1>
<ul class="productList">'.populateList($pageHeading).'</ul>';
2022-10-22 15:03:47 +00:00
require '../layout.php';
2022-11-19 14:58:22 +00:00
function populateList($category) {
2022-11-20 13:20:58 +00:00
$pdo = startDB();
2022-10-22 15:03:47 +00:00
$output = '';
if ($category === 'Latest Listings') {
2022-11-20 17:51:10 +00:00
$stmt = $pdo->prepare('SELECT * FROM auction WHERE endDate > "'. date("Y-m-d H:i:s"). '" ORDER BY endDate ASC');
$stmt->execute();
$listings = $stmt->fetchAll();
2022-11-20 19:58:30 +00:00
$count = 10;
}
else {
2022-11-16 19:17:35 +00:00
$stmt = $pdo->prepare('SELECT * FROM auction WHERE categoryId = (SELECT category_id FROM category WHERE name = :listing_category)');
$values = [
'listing_category' => $category
];
$stmt->execute($values);
$listings = $stmt->fetchAll();
}
foreach ($listings as &$listing) {
2022-11-20 18:51:17 +00:00
$listCat = getFirstAllMatches('category', 'category_id', $listing['categoryId'])['name'];
$bid = getFirstMatch('bids','MAX(amount)', 'listing_id', $listing['listing_id']);
2022-10-22 15:03:47 +00:00
$output .= '<li>
2022-11-20 19:58:30 +00:00
<img src="'.$listing['imgUrl'].'" alt="product name">
2022-10-22 15:03:47 +00:00
<article>
2022-11-16 19:25:32 +00:00
<h2>'. $listing['title'] .'</h2>
2022-11-20 18:51:17 +00:00
<h3>'. $listing['categoryId'] .'</h3>
2022-11-16 19:17:35 +00:00
<p>'. $listing['description'] .'</p>
2022-11-20 18:51:17 +00:00
<p class="price">Current bid:'. $bid['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-11-20 19:58:30 +00:00
if ($category === 'Latest Listings') {
$count -= 1;
if ($count <= 0) {
break;
}
}
2022-10-22 15:03:47 +00:00
}
return $output;
}
?>