-
Notifications
You must be signed in to change notification settings - Fork 0
/
shop.php
118 lines (95 loc) · 4.46 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
<?php
$title = 'Shop';
include './php/config.php';
session_start();
if(isset($_SESSION['admin_id'])){
$user_id = $_SESSION['admin_id'];
include './templates/admin_header.php';
} elseif(isset($_SESSION['user_id'])){
$user_id = $_SESSION['user_id'];
include './templates/user_header.php';
} else
include './templates/header.php';
if(isset($_POST['add_to_cart'])){
if (!isset($user_id)){
echo '<script>alert("Please login to add books to cart!");
window.location.href="login.php";</script>';
}else {
$book_id = $_POST['book_id'];
$book_title = $_POST['book_title'];
$book_price = $_POST['book_price'];
$book_image = $_POST['book_image'];
$book_quantity = $_POST['book_quantity'];
$sql = "SELECT * FROM cart WHERE title = '$book_id' AND user_id = '$user_id'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
echo '<script>alert("Book already added to cart!")</script>';
} else {
$sql = "INSERT INTO cart (user_id, title, price, quantity, image) VALUES ('$user_id', '$book_title', '$book_price', '$book_quantity', '$book_image')";
$result = mysqli_query($con, $sql);
if($result){
echo '<script>alert("Book Added to Cart Successfully!");
window.location.href="shop.php";</script>';
} else {
echo '<script>alert("Book not added to cart!")</script>';
}
}
}
}
?>
<link rel="stylesheet" href="./css/shop.css">
<div class="title-section">
<div class="title-section-container">
<h1><?php echo $title ?></h1>
<br>
<ul class="breadcrump">
<li><i class="fa-solid fa-house"></i> Home</li>
<li>> <?php echo $title ?></li>
</ul>
</div>
</div>
<div class="main-container" style="display:block;align-items: flex-start;">
<div class="book-container">
<?php
$select_books = mysqli_query($con, "SELECT * FROM books") or die('query failed');
$select_category= mysqli_query($con, "SELECT name FROM categories INNER JOIN books ON categories.id = books.category_id") or die('query failed');
if(mysqli_num_rows($select_books) > 0){
while(($fetch_books = mysqli_fetch_assoc($select_books)) AND ($fetch_category = mysqli_fetch_assoc($select_category))){
?>
<div class="book-card">
<form action="" method="post" class="cart-box">
<img class="image" src="./src/uploads/<?php echo $fetch_books['image'] ?>" alt="">
<div class="info">
<h3><?php echo $fetch_books['title'] ?></h3>
<p><?php echo $fetch_category['name'] ?></p>
<div class="price-qty">
<span>$<?php echo $fetch_books['price'] ?></span>
<input type="number" name="book_quantity" value="1" min="1" id="book_quantity" class="qty">
</div>
<input type="hidden" name="book_id" value="<?php echo
$fetch_books['id'] ?>">
<input type="hidden" name="book_title" value="<?php echo
$fetch_books['title'] ?>">
<input type="hidden" name="book_price" value="<?php echo
$fetch_books['price'] ?>">
<input type="hidden" name="book_image" value="<?php echo
$fetch_books['image'] ?>">
<?php
echo '<div class="btn-1">
<button type="submit" value="Add to cart" name="add_to_cart" class="btn"><img src="./src/cart.svg" alt="" style="width:25px; margin-right:10px;"> Add to cart</button>
</div>';
?>
</div>
</form>
</div>
<?php
}
} else {
echo '<p class="empty">No books added yet!</p>';
}
?>
</div>
<?php
include './templates/newsletter.php';
include './templates/footer.php';
?>