-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_functions.php
More file actions
135 lines (125 loc) · 5.08 KB
/
common_functions.php
File metadata and controls
135 lines (125 loc) · 5.08 KB
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
<?php
$servername = "localhost";
$db_username = "your_db_username";
$db_password = "your_db_password";
$dbname = "mbzstore";
$conn = mysqli_connect('localhost', 'root', '', 'mbzstore');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
};
if (!function_exists('getIPAddress')) {
// get ip address function
function getIPAddress()
{
//whether ip is from the share internet
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
//whether ip is from the proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//whether ip is from the remote address
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}
// $ip = getIPAddress();
// echo 'User Real IP Address - '.$ip;
//cart function
if (!function_exists('cart')) {
function cart()
{
if (isset($_GET['add_to_cart'])) {
global $conn;
$get_ip_address = getIPAddress();
$get_product_id = $_GET['add_to_cart'];
$select_query = "select * from `cart_details` where ip_address= '$get_ip_address' and product_id=$get_product_id";
$result = mysqli_query($conn, $select_query);
$num_of_rows = mysqli_num_rows($result);
if ($num_of_rows > 0) {
echo "<script>alert('This item is already present in your cart')</script>";
echo "<script>window.open('prices.php','_self')</script>";
} else {
$insert_query = "INSERT INTO `cart_details`(product_id,ip_address,quantity) VALUES ($get_product_id,'$get_ip_address',0)";
$result = mysqli_query($conn, $insert_query);
echo "<script>alert('Item added to cart')</script>";
echo "<script>window.open('prices.php','_self')</script>";
}
}
}
}
//function to get cart item numbers
if (!function_exists('cart_item')) {
function cart_item()
{
if (isset($_GET['add_to_cart'])) {
global $conn;
$get_ip_address = getIPAddress();
$select_query = "select * from `cart_details` where ip_address= '$get_ip_address'";
$result = mysqli_query($conn, $select_query);
$count_cart_items = mysqli_num_rows($result);
} else {
global $conn;
$get_ip_address = getIPAddress();
$select_query = "select * from `cart_details` where ip_address= '$get_ip_address'";
$result = mysqli_query($conn, $select_query);
$count_cart_items = mysqli_num_rows($result);
}
echo $count_cart_items;
}
}
//total price function
if (!function_exists('total_cart_price')) {
function total_cart_price()
{
global $conn;
$get_ip_address = getIPAddress();
$total_price = 0;
$cart_query = "select * from `cart_details` where ip_address='$get_ip_address'";
$result = mysqli_query($conn, $cart_query);
while ($row = mysqli_fetch_array($result)) {
$product_id = $row['product_id'];
$select_products = "select * from `products` where product_id='$product_id'";
$result_products = mysqli_query($conn, $select_products);
while ($row_product_price = mysqli_fetch_array($result_products)) {
$product_price = array($row_product_price['product_price']);
$product_values = array_sum($product_price);
$total_price += $product_values;
}
}
echo $total_price;
}
}
//get user order details
if (!function_exists('get_user_order_details')) {
function get_user_order_details()
{
global $conn;
$username = $_SESSION['username'];
$get_details = "Select * from `user_table` where username='$username'";
$result_query = mysqli_query($conn, $get_details);
while ($row_query = mysqli_fetch_array($result_query)) {
$user_id = $row_query['user_id'];
if (!isset($_GET['edit_account'])) {
if (!isset($_GET['my_orders'])) {
if (!isset($_GET['delete_account'])) {
$get_orders = "Select * from `user_orders` where user_id=$user_id and order_status='pending'";
$result_orders_query = mysqli_query($conn, $get_orders);
$row_count = mysqli_num_rows($result_orders_query);
if ($row_count > 0) {
echo "<p class='text-center'>You have $row_count pending order(s)</p>
<p class='text-center text-success o'><a href='profile.php?my_orders'>View Order Details</a></p>";
} else {
echo "<p class='text-center'>You have 0 pending order(s)</p>
<p class='text-center text-success o'><a href='index.php?my_orders'>View More Services</a></p>";
}
}
}
}
}
}
}
?>