2022-10-22 15:03:47 +00:00
< ? php
2022-11-20 14:20:17 +00:00
session_start ();
2022-11-20 13:25:26 +00:00
require_once '../functions.php' ;
2022-10-22 15:03:47 +00:00
$pageTitle = 'iBuy - Product Listing' ;
2022-11-20 15:38:33 +00:00
$listing = getListing ();
$pdo = startDB ();
if ( isset ( $_POST [ 'bidSubmit' ])) {
$stmt = $pdo -> prepare ( ' INSERT INTO bids ( amount , user_id , listing_id )
VALUES ( : amount , : user_id , : listing_id ) ' );
$values = [
'amount' => $_POST [ 'bid' ],
'user_id' => $_SESSION [ 'loggedin' ],
'listing_id' => $listing [ 'listing_id' ]
];
$stmt -> execute ( $values );
}
else if ( isset ( $_POST [ 'reviewSubmit' ])) {
$stmt = $pdo -> prepare ( 'SELECT * FROM users WHERE email = :email' );
$values = [
'email' => $listing [ 'email' ]
];
$stmt -> execute ( $values );
$user = $stmt -> fetch ();
$stmt = $pdo -> prepare ( ' INSERT INTO review ( review_user , review_date , review_contents , user_id )
VALUES ( : review_user , : review_date , : review_contents , : user_id ) ' );
$values = [
'review_user' => $_SESSION [ 'loggedin' ],
'review_date' => date ( 'Y-m-d' ),
'review_contents' => $_POST [ 'reviewtext' ],
'user_id' => $user [ 'user_id' ]
];
$stmt -> execute ( $values );
}
2022-10-22 15:03:47 +00:00
$pageContent = ' < h1 > Product Page </ h1 >
2022-11-20 15:38:33 +00:00
< article class = " product " > '. populateContent($listing) .' </ article > ' ;
2022-11-15 15:30:12 +00:00
require '../layout.php' ;
2022-11-20 14:44:18 +00:00
checkListing ();
2022-11-15 15:30:12 +00:00
2022-11-20 15:38:33 +00:00
function populateContent ( $listing ) {
2022-11-20 13:20:58 +00:00
$pdo = startDB ();
2022-11-15 15:38:46 +00:00
2022-11-16 19:17:35 +00:00
$stmt = $pdo -> prepare ( 'SELECT * FROM category WHERE category_id = :category_id' );
2022-11-15 15:38:46 +00:00
$values = [
2022-11-16 19:17:35 +00:00
'category_id' => $listing [ 'categoryId' ]
2022-11-15 15:38:46 +00:00
];
$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 = [
2022-11-16 19:17:35 +00:00
'email' => $listing [ 'email' ]
2022-11-15 15:38:46 +00:00
];
$stmt -> execute ( $values );
$user = $stmt -> fetch ();
2022-11-15 15:30:12 +00:00
$output = ' < img src = " product.png " alt = " product name " >
< section class = " details " >
2022-11-16 19:25:32 +00:00
< h2 > '. $listing[' title '] .' </ h2 >
2022-11-16 19:17:35 +00:00
< h3 > '. $category[' name '] .' </ h3 >
2022-11-15 15:38:46 +00:00
< p > Auction created by < a href = " # " > '. $user[' first_name '].$user[' last_name '] .' </ a ></ p >
< p class = " price " > Current bid : '. $bid[' MAX ( amount ) '] .' </ p >
2022-11-20 14:44:18 +00:00
< time > Time left : '. round((strtotime($listing[' endDate ']) - strtotime(date(' Y - m - d H : i : s ')))/60/60,1 ) .' Hours </ time >
2022-11-20 15:38:33 +00:00
< form action = " listing.php?listing_id='. $listing['listing_id'] .' " class = " bid " method = " POST " >
< input type = " number " step = " 0.1 " name = " bid " value = " '. $bid['MAX(amount)'] .' " />
< input name = " bidSubmit " type = " submit " value = " Place Bid " />
2022-11-15 15:30:12 +00:00
</ form >
</ section >
< section class = " description " >
2022-11-16 19:17:35 +00:00
< p > '. $listing[' description '] .' </ p >
2022-11-15 15:30:12 +00:00
2022-11-20 15:38:33 +00:00
</ section > ' ;
2022-11-15 15:30:12 +00:00
2022-11-20 15:38:33 +00:00
$output .= ' < section class = " reviews " >
< h2 > Reviews of '. $user[' first_name '].$user[' last_name '].' </ h2 >
< ul > '. getReviews($user[' user_id ']) .' </ ul >
2022-11-15 15:30:12 +00:00
2022-11-20 15:38:33 +00:00
< form action = " listing.php?listing_id='. $listing['listing_id'] .' " method = " POST " >
2022-11-15 15:30:12 +00:00
< label > Add your review </ label > < textarea name = " reviewtext " ></ textarea >
2022-11-20 15:38:33 +00:00
< input type = " submit " name = " reviewSubmit " value = " Add Review " />
2022-11-15 15:30:12 +00:00
</ form >
</ section > ' ;
2022-11-20 15:38:33 +00:00
2022-11-20 15:48:33 +00:00
if ( isset ( $_SESSION [ 'loggedin' ])) {
if ( $user [ 'user_id' ] === $_SESSION [ 'loggedin' ]) {
$output .= '<a href ="account/editAuction.php?listing_id=' . $listing [ 'listing_id' ] . '">edit</a>' ;
}
2022-11-20 14:20:17 +00:00
}
2022-11-15 15:30:12 +00:00
return $output ;
}
2022-11-20 15:38:33 +00:00
function getReviews ( $user_id ) {
$pdo = startDB ();
$output = '' ;
$stmt = $pdo -> prepare ( 'SELECT * FROM review WHERE user_id = :user_id' );
$values = [
'user_id' => $user_id
];
$stmt -> execute ( $values );
$reviews = $stmt -> fetchAll ();
foreach ( $reviews as & $review ) {
$stmt = $pdo -> prepare ( 'SELECT * FROM users WHERE user_id = :user_id' );
$values = [
'user_id' => $review [ 'review_user' ]
];
$stmt -> execute ( $values );
$user = $stmt -> fetch ();
2022-11-20 15:48:33 +00:00
$output .= '<li><strong>' . $user [ 'first_name' ] . $user [ 'last_name' ] . ' said </strong>' . $review [ 'review_contents' ] . ' <em>' . $review [ 'review_date' ] . '</em></li>' ;
2022-11-20 15:38:33 +00:00
}
2022-11-20 15:48:33 +00:00
return $output ;
2022-11-20 15:38:33 +00:00
}
2022-11-15 17:04:26 +00:00
?>
//TODO: add bid history