-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshop.php
145 lines (119 loc) · 4.38 KB
/
shop.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
include 'config.php';
session_start();
$user_id = NULL;
if(isset($_SESSION['user_id'])){
$user_id = $_SESSION['user_id'];
}
if(isset($_POST['add_to_cart'])){
if($user_id == NULL){
header("location:login.php?product_id=". $_POST["product_id"]);
}
$product_name = $_POST['product_name'];
$product_price = $_POST['product_price'];
$product_image = $_POST['product_image'];
// $product_quantity = $_POST['product_quantity'];
$check_cart_numbers = mysqli_query($conn, "SELECT * FROM `cart` WHERE name = '$product_name' AND user_id = '$user_id'") or die('query failed');
if(mysqli_num_rows($check_cart_numbers) > 0){
$message[] = 'already added to cart!';
}else{
mysqli_query($conn, "INSERT INTO `cart`(user_id, name, price, image,quantity) VALUES('$user_id', '$product_name', '$product_price', '$product_image',1)") or die('query failed');
$message[] = 'product added to cart!';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>shop</title>
<!-- custom css file link -->
<link rel="stylesheet" href="usercss/style.css">
</head>
<body>
<?php include 'header.php'; ?>
<section class="products">
<h1 class="title">products</h1>
<div class="box-container">
<?php
$limit=9;
if(isset($_GET['page'])){
$page=$_GET['page'];
}else{
$page=1;
}
$offset=($page-1)*$limit;
$select_products = mysqli_query($conn, "SELECT * FROM `products` ORDER BY id DESC LIMIT {$offset},{$limit} ") or die('query failed');
if(mysqli_num_rows($select_products) > 0){
while($fetch_products = mysqli_fetch_assoc($select_products)){
?>
<form action="" method="post" class="box">
<a href="productdetail.php?id=<?php echo $fetch_products['id']?>">
<img class="image" src="uploaded_img/<?php echo $fetch_products['image']; ?>" alt="">
</a>
<div class="name"><?php echo $fetch_products['name']; ?></div>
<div class="price">NRS <?php echo $fetch_products['price']; ?>/-</div>
<div class="author">By: <?php echo $fetch_products['author']; ?></div>
<input type="hidden" name="product_name" value="<?php echo $fetch_products['name']; ?>">
<input type="hidden" name="product_price" value="<?php echo $fetch_products['price']; ?>">
<input type="hidden" name="product_image" value="<?php echo $fetch_products['image']; ?>">
<input type="submit" value="add to cart" name="add_to_cart" class="btn">
</form>
<?php
}
}else{
echo '<p class="empty">no products added yet!</p>';
}
?>
</div>
<style>
.pagination {
margin-left: 15rem;
margin-top: 20px;
}
.pagination span {
display: inline-block;
border: 1px solid #ff523b;
margin-left: 10px;
width: 40px;
height: 40px;
line-height: 40px;
text-align: center;
cursor: pointer;
}
.pagination span:hover {
background: #ff523b;
color: #fff;
}
.pagination a {
color: black;
}
</style>
<?php
$select_products = mysqli_query($conn, "SELECT COUNT(*) FROM `products`") or die('query failed');
$total_rows=mysqli_fetch_array($select_products)[0];
$total_page=ceil($total_rows/$limit);
echo'<div class="pagination">';
if($page>1){
echo'<a href=?page='.($page - 1).'><span>Prev</span></a>';
}
for($i=1; $i<=$total_page;$i++){
echo '<a href="?page='.$i.'"><span>'.$i.'</span></a>';
}
if($total_page>$page){
echo'<a href=?page='.($page + 1).'><span>Next</span></a>';
echo '</div>';
}
?>
<!-- <ul>
<a href="?page=1">first</a>
<a href="?page=<?php echo $total_page;?>">last</a>
</ul> -->
</section>
<?php include 'footer.php'; ?>
<!-- custom js file link -->
<script src="js/script.js"></script>
</body>
</html>