Skip to content
Permalink
ae335aeb5c
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
256 lines (216 sloc) 12.4 KB
<?php
require("../dbCon.php");
//echo "Welcome " . $_SESSION["userName"] . "!";
?>
<html>
<head>
<link rel="stylesheet" href="../style.css">
</head>
<body>
<div id="header">
<ul id="menu" class="blue">
<li><a href="cDashboard.php">Home</a></li>
<li><a href="cEditProfile.php">Edit Profile</a></li>
<li class="active"><a href="cSearchProduct.php">Search Product</a></li>
<li><a href="cCreateOrder.php">Create Order</a></li>
<li><a href="cPayment.php">Payment</a></li>
<li><a href="cCart.php">Cart</a></li>
<li><a href="cPastOrders.php">Past Orders</a></li>
<li class='fRight'><a href="logout.php">Log Out</a></li>
</ul>
</div>
<div id="contentwrapper">
<div id='contentwrapperLeft'>
<form action = "cSearchProduct.php" method = "get">
<h1>Simple Search</h1>
<div class="form-group">
<input type="text" name="SimpleSearch" id="SimpleSearch" required value="<?php
if(isset($_GET['SimpleSearch'])){echo $_GET['SimpleSearch'];}?>">
<input type="submit" value="Search">
</div>
</form>
</div>
<div id="contentwrapperRight">
<form action = "cSearchProduct.php" method = "get">
<h1>Advanced Search</h1>
<div class="form-group">
<label>Product Name</label>
<input type="text" name="ProductName" id="ProductName" value="<?php
if(isset($_GET['ProductName'])){echo $_GET['ProductName'];}?>">
<label>Category</label>
<!-- <input type="text" name="Category" id="Category"> -->
<select name="Category" id="Category">
<option value=""></option>
<?php
$category = "";
if(isset($_GET['Category'])){
$category = $_GET['Category'];
}
$query ="SELECT * FROM categories";
$stmt = $con->prepare($query);
$stmt ->execute();
$result = $stmt->get_result();
while($row = mysqli_fetch_array($result))
{
echo "<option ";
if($row['CategoryName'] === $category){
echo "selected='selected'";
}
echo "value='" . $row['CategoryName'] . "'>" . $row['CategoryName'] . "</option>";
}
?>
</select>
<label>Company Name</label>
<input type="text" name="CompanyName" id="CompanyName" value="<?php
if(isset($_GET['CompanyName'])){echo $_GET['CompanyName'];}?>">
<label>Unit Price Greater Than</label>
<input type="text" name="UnitPriceLow" id="UnitPriceLow" value="<?php
if(isset($_GET['UnitPriceLow'])){echo $_GET['UnitPriceLow'];}?>">
<label>Unit Price Less Than</label>
<input type="text" name="UnitPriceHigh" id="UnitPriceHigh" value="<?php
if(isset($_GET['UnitPriceHigh'])){echo $_GET['UnitPriceHigh'];}?>">
<input type="submit" value="Search">
</div>
</form>
</div>
</div>
<hr>
<div id ='contentwrapper'>
<h1>Results</h1>
<script>
//Shopping cart is saved in form 'productID'_'quantity', seperated by spaces
function addToCart(buttonID) {
productID = buttonID.split("_")[1];
orderShoppingCart = document.getElementById('shoppingCart').value;
orders = orderShoppingCart.split(" ");
if (document.getElementById(buttonID).value === "Add") { //Add value to shopping cart
orderShoppingCart = (orderShoppingCart + " " + productID + "_1").trim(); //Add space and ID to cart list, default quantity of 1
document.getElementById(buttonID).value = "Remove";
}else{
orderShoppingCart = "";
for (index = 0; index < orders.length; ++index) {
if(orders[index].split("_")[0] === productID){
//This is the item to remove
}else{
orderShoppingCart = orderShoppingCart + orders[index] + " ";
}
}
document.getElementById(buttonID).value = "Add";
}
// orderShoppingCart = orderShoppingCart.replace(" " + productID + /_[0-9]+$/i, " " + productID + "_" + document.getElementById(quantityID).value);
document.getElementById('shoppingCart').value = orderShoppingCart.trim();
return 1;
}
</script>
<?php
if (empty($_GET)) { //Show all products by default
$query ="SELECT * FROM
((products as p LEFT JOIN suppliers as s ON p.SupplierID = s.SupplierID)
LEFT JOIN categories c ON p.CategoryID = c.CategoryID)
LEFT JOIN company co ON s.CompanyId = co.CompanyID";
$stmt = $con->prepare($query);
}elseif (isset($_GET["SimpleSearch"]) && !empty($_GET["SimpleSearch"])) { //Simple search
$query ="SELECT * FROM
((products as p LEFT JOIN suppliers as s ON p.SupplierID = s.SupplierID)
LEFT JOIN categories c ON p.CategoryID = c.CategoryID)
LEFT JOIN company co ON s.CompanyId = co.CompanyID
WHERE
CategoryName LIKE ? OR
ProductName LIKE ? OR
CompanyName LIKE ?";
$SimpleSearch = "%" . $_GET['SimpleSearch'] . "%";
$stmt = $con->prepare($query);
$stmt ->bind_param("sss", $SimpleSearch, $SimpleSearch, $SimpleSearch);
}else{ //Advanced search
$query ="SELECT * FROM
((products as p LEFT JOIN suppliers as s ON p.SupplierID = s.SupplierID)
LEFT JOIN categories c ON p.CategoryID = c.CategoryID)
LEFT JOIN company co ON s.CompanyId = co.CompanyID
WHERE
ProductName LIKE ? AND
CompanyName LIKE ? AND
CategoryName LIKE ? AND
UnitPrice > ? AND
UnitPrice < ?";
$ProductName = "%" . $_GET['ProductName'] . "%";
$CompanyName = "%" . $_GET['CompanyName'] . "%";
$Category = "%" . $_GET['Category'] . "%";
if (empty($_GET["UnitPriceLow"])){
$UnitPriceLow = 0.00;
}else{
$UnitPriceLow = number_format($_GET['UnitPriceLow'], 2);
}
if (empty($_GET["UnitPriceHigh"])){
$UnitPriceHigh = 10000000.00;
}else{
$UnitPriceHigh = number_format($_GET['UnitPriceHigh'], 2);
}
$stmt = $con->prepare($query);
$stmt ->bind_param("sssdd", $ProductName, $CompanyName, $Category, $UnitPriceLow, $UnitPriceHigh);
}
$stmt ->execute();
$result = $stmt->get_result();
//Build results table
echo "<table id='niceTable'>
<tr>
<th>Product</th>
<th>Category</th>
<th>Company</th>
<th>Quantity</th>
<th>Price</th>
<th>Stock</th>
<th>View</th>
<th>Cart</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
// echo "<tr onclick=\"window.document.location='cViewProduct.php?productID=" . $row['ProductID'] . "'\">";
echo "<tr>
<td>" . $row['ProductName'] . "</td>
<td>" . $row['CategoryName'] . "</td>
<td>" . $row['CompanyName'] . "</td>
<td>" . $row['QuantityPerUnit'] . "</td>
<td>" . "$" . number_format($row['UnitPrice'], 2) . "</td>
<td>" . $row['UnitsInStock'] . "</td>";
// echo "<td><a href='cCart.php?ProductID=" . $row['ProductID'] . "'>Add</a></td>";
// echo "<td><input type='button' value='Add' id='ProductID_" . $row['ProductID'] . "'></td>";
echo "<td>"
. "<input type='submit' value='View' "
. "name='ViewButton' "
. "id='VProductID_" . $row['ProductID'] . "' "
. "onClick=\"location.href = 'cViewProduct.php?productID=" . $row['ProductID'] . "';\">"
. "</td>";
if(strpos(" " . $_SESSION['shoppingCart'], " " . $row['ProductID'] . "_") !== false){ //Check if product is already in cart
$curVal = 'Remove';
}else{
$curVal = 'Add';
}
echo "<td>"
. "<input type='submit' value='" . $curVal . "' "
. "name='ProductButton' "
. "id='ProductID_" . $row['ProductID'] . "' "
. "onClick='addToCart(this.id)'>"
. "</td>";
/* echo "<td>"
. "<form action = 'cSearchProduct.php' method = 'get'>"
. "<input type='submit' value='Add' "
. "id='ProductID_" . $row['ProductID'] . "' "
. "onClick='addToCart(this.id)'>"
. "</form>"
. "</td>";
*/
echo "</tr>";
}
echo "</table>";
?>
<form action = "cSearchProduct_save.php" method = "get">
<div class="form-group">
<?php //$_SESSION['orderShoppingCart'] = ""; ?>
<!-- <input type="text" name='shoppingCart' id='shoppingCart' value="" style='display: none'/> -->
<input type="text" name='shoppingCart' id='shoppingCart' value='<?php echo $_SESSION['shoppingCart'];?>' style='display: none'>
<input type="submit" id="submit" value="Send selected items to order">
</div>
</form>
</div>
</body>
</html>