Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Quantity Selection, Total Price Display, and User Name on Navigation Bar #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Database/store.sql
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,22 @@ CREATE TABLE `users_items` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
`quantity` int(5) NOT NULL,
`status` enum('Added to cart','Confirmed') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users_items`
--

INSERT INTO `users_items` (`id`, `user_id`, `item_id`, `status`) VALUES
(7, 3, 3, 'Added to cart'),
(8, 3, 4, 'Added to cart'),
(9, 3, 5, 'Added to cart'),
(10, 3, 11, 'Added to cart'),
(11, 1, 9, 'Added to cart'),
(12, 1, 2, 'Added to cart'),
(13, 1, 8, 'Added to cart');
INSERT INTO `users_items` (`id`, `user_id`, `item_id`, `quantity`, `status`) VALUES
(7, 3, 3, 1, 'Added to cart'),
(8, 3, 4, 1, 'Added to cart'),
(9, 3, 5, 1, 'Added to cart'),
(10, 3, 11, 1, 'Added to cart'),
(11, 1, 9, 1, 'Added to cart'),
(12, 1, 2, 1, 'Added to cart'),
(13, 1, 8, 1, 'Added to cart');

--
-- Indexes for dumped tables
Expand Down
206 changes: 135 additions & 71 deletions LifestyleStore/cart.php
Original file line number Diff line number Diff line change
@@ -1,82 +1,146 @@
<?php
session_start();
require 'connection.php';
if(!isset($_SESSION['email'])){
header('location: login.php');
}
$user_id=$_SESSION['id'];
$user_products_query="select it.id,it.name,it.price from users_items ut inner join items it on it.id=ut.item_id where ut.user_id='$user_id'";
$user_products_result=mysqli_query($con,$user_products_query) or die(mysqli_error($con));
$no_of_user_products= mysqli_num_rows($user_products_result);
$sum=0;
if($no_of_user_products==0){
//echo "Add items to cart first.";
require 'connection.php';
session_start();
if (!isset($_SESSION['email'])) {
header('location: login.php');
}
$user_id = $_SESSION['id'];
$user_products_query = "select it.id, it.name, it.price, ut.quantity from users_items ut inner join items it on it.id=ut.item_id where ut.user_id='$user_id'";
$user_products_result = mysqli_query($con, $user_products_query) or die(mysqli_error($con));
$no_of_user_products = mysqli_num_rows($user_products_result);
$sum = 0;

if (isset($_SESSION['email'])) {
$user_id = $_SESSION['id'];
$display_cart_count_query = "SELECT COUNT(DISTINCT item_id) AS item_count FROM users_items WHERE user_id = $user_id";
$cart_count_result = mysqli_query($con, $display_cart_count_query) or die(mysqli_error($con));
$row = mysqli_fetch_assoc($cart_count_result);
$item_count = $row['item_count'];
}

if ($no_of_user_products == 0) {
//echo "Add items to cart first.";
?>
<script>

<script>
window.alert("No items in the cart!!");
</script>
</script>
<?php
}else{
while($row=mysqli_fetch_array($user_products_result)){
$sum=$sum+$row['price'];
}
} else {
while ($row = mysqli_fetch_array($user_products_result)) {
$sum = $sum + ($row['price'] * $row['quantity']);
}
}

?>

<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="img/lifestyleStore.png" />
<title>Lifestyle Store</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- latest compiled and minified CSS -->
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css">
<!-- jquery library -->
<script type="text/javascript" src="bootstrap/js/jquery-3.2.1.min.js"></script>
<!-- Latest compiled and minified javascript -->
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<!-- External CSS -->
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div>
<?php
require 'header.php';
?>
<br>
<div class="container">
<table class="table table-bordered table-striped">
<tbody>

<head>
<link rel="shortcut icon" href="img/lifestyleStore.png" />
<title>Lifestyle Store</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- latest compiled and minified CSS -->
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css">
<!-- jquery library -->
<script type="text/javascript" src="bootstrap/js/jquery-3.2.1.min.js"></script>
<!-- Latest compiled and minified javascript -->
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<!-- External CSS -->
<link rel="stylesheet" href="css/style.css" type="text/css">

<script type="text/javascript">
$(document).ready(function () {
$('.quantity').change(function () {
var id = $(this).attr('data-id');
var quantity = $(this).val();
$.ajax({
url: "update_quantity.php",
method: "POST",
data: {
item_id: id,
quantity: quantity
},
success: function (data) {
window.location.reload(true);
}
});
});
});
</script>

</head>

<body>
<div>
<?php
require 'header.php';
?>
<br>
<div class="container">
<table class="table table-bordered table-striped">
<tbody>
<tr>
<th>Item Number</th>
<th>Item Name</th>
<th>Price</th>
<th>Quantity</th>
<th></th>
</tr>
<?php
$user_products_result = mysqli_query($con, $user_products_query) or die(mysqli_error($con));
$no_of_user_products = mysqli_num_rows($user_products_result);
$counter = 1;
while ($row = mysqli_fetch_array($user_products_result)) {

?>
<tr>
<th>Item Number</th><th>Item Name</th><th>Price</th><th></th>
<th>
<?php echo $counter ?>
</th>
<th>
<?php echo $row['name'] ?>
</th>
<th>
<?php echo $row['price'] ?>
</th>
<th><input type="number" value="<?php echo $row['quantity'] ?>" min="1" max="10"
data-id="<?php echo $row['id'] ?>" class="quantity"></th>
<th><a href='cart_remove.php?id=<?php echo $row['id'] ?>' class="btn btn-primary">Remove</a>
</th>
</tr>
<?php
$user_products_result=mysqli_query($con,$user_products_query) or die(mysqli_error($con));
$no_of_user_products= mysqli_num_rows($user_products_result);
$counter=1;
while($row=mysqli_fetch_array($user_products_result)){

?>
<tr>
<th><?php echo $counter ?></th><th><?php echo $row['name']?></th><th><?php echo $row['price']?></th>
<th><a href='cart_remove.php?id=<?php echo $row['id'] ?>'>Remove</a></th>
</tr>
<?php $counter=$counter+1;}?>
<tr>
<th></th><th>Total</th><th>Rs <?php echo $sum;?>/-</th><th><a href="success.php?id=<?php echo $user_id?>" class="btn btn-primary">Confirm Order</a></th>
</tr>
</tbody>
</table>
</div>
<br><br><br><br><br><br><br><br><br><br>
<footer class="footer">
<div class="container">
<center>
<p>Copyright &copy Lifestyle Store. All Rights Reserved. | Contact Us: +91 90000 00000</p>
<p>This website is developed by Sajal Agrawal</p>
</center>
</div>
</footer>
<?php $counter = $counter + 1;
} ?>
<tr>
<th></th>
<th>Total</th>
<th>Rs
<?php echo $sum; ?>/-
</th>
<th><a href="success.php?id=<?php echo $user_id ?>" class="btn btn-primary">Confirm Order</a>
</th>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<br><br><br><br><br><br><br><br><br><br>
<footer class="footer">
<div class="container">
<center>
<p>Copyright &copy Lifestyle Store. All Rights Reserved. | Contact Us: +91 90000 00000</p>
<p>This website is developed by Sajal Agrawal</p>
</center>
</div>
</footer>
</div>

<script type="text/javascript">
const cartCountElement = document.getElementById("cartcount");
cartCountElement.innerHTML = '<?php echo $item_count; ?>';
</script>

</body>

</html>
5 changes: 3 additions & 2 deletions LifestyleStore/cart_add.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
require 'connection.php';
//require 'header.php';
require 'header.php';

session_start();
$item_id=$_GET['id'];
$user_id=$_SESSION['id'];
$add_to_cart_query="insert into users_items(user_id,item_id,status) values ('$user_id','$item_id','Added to cart')";
$add_to_cart_query="insert into users_items(user_id,item_id,quantity,status) values ('$user_id','$item_id',1,'Added to cart')";
$add_to_cart_result=mysqli_query($con,$add_to_cart_query) or die(mysqli_error($con));
header('location: products.php');
?>
2 changes: 1 addition & 1 deletion LifestyleStore/check_if_added.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function check_if_added_to_cart($item_id){
//session_start();
require 'connection.php';
$user_id=$_SESSION['id'];
$product_check_query="select * from users_items where item_id='$item_id' and user_id='$user_id' and status='Added to cart'";
$product_check_query="select * from users_items where item_id='$item_id' and user_id='$user_id'";
$product_check_result=mysqli_query($con,$product_check_query) or die(mysqli_error($con));
$num_rows=mysqli_num_rows($product_check_result);
if($num_rows>=1)return 1;
Expand Down
6 changes: 3 additions & 3 deletions LifestyleStore/connection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
$con=mysqli_connect("localhost","id2538044_sajalagrawal","******","id2538044_store") or die(mysqli_error($con));
//$con=mysqli_connect("localhost","root","","store") or die(mysqli_error($con));
?>
$con = mysqli_connect("localhost", "id2538044_sajalagrawal", "******", "id2538044_store") or die(mysqli_error($con));
// $con=mysqli_connect("localhost","root","","store") or die(mysqli_error($con));
?>
5 changes: 4 additions & 1 deletion LifestyleStore/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
<?php
if(isset($_SESSION['email'])){
?>
<li><a href="cart.php"><span class="glyphicon glyphicon-shopping-cart"></span> Cart</a></li>
<li><a href="index.php"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li><a href="#"><span class="glyphicon glyphicon-user"></span> <span><?php echo $_SESSION['name'] ?></span></a></li>
<li><a href="cart.php"><span class="glyphicon glyphicon-shopping-cart"></span> Cart <span id="cartcount"></span></a></li>
<li><a href="settings.php"><span class="glyphicon glyphicon-cog"></span> Settings</a></li>
<li><a href="logout.php"><span class="glyphicon glyphicon-log-out"></span> Logout</a></li>
<?php
}else{
?>
<li><a href="index.php"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li><a href="signup.php"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li><a href="login.php"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
<?php
Expand Down
Loading