-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
120 lines (103 loc) · 2.92 KB
/
index.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
<!--
"Demo Shopping Cart" is a non comprehensive e-commerce website developed only for academic purposes. This is just a demonstration of a simple shopping cart and it may contain bugs and errors.
Design and development by Rasan Samarasinghe. (c) 2015 All Rights Reserved.
-->
<?php
include "core.php";
include "dbconnection.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Home</title>
<link href="styles/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="page">
<div class="header">
<?php
showHeading();
?>
</div>
<div class="wrapper">
<div class="navigation">
<?php
mainMenu();
?>
</div>
<div class="contents">
<h2>Browse Our Products!</h2>
<table width="100%" border="1" cellpadding="5" cellspacing="0">
<?php
//add item to the cart
if(isset($_GET['pid'])){
if(isset($_SESSION['uid'])){
$stmt = $con->prepare("INSERT INTO tblcart (uid, pid) VALUES (?, ?)");
$stmt->bind_param("ii", $_SESSION['uid'], validateInput($_GET['pid']));
if($stmt->execute()){
echo "Item added to the cart successfully!<br/>";
}else{
echo "Error add item to the cart<br/>";
}
}else{
echo "You must login to add this product into cart<br/>";
}
}
//get items per page
$page = null;
$items_per_page = 4; //items per page
if (isset($_GET["page"])){ $page = validateInput($_GET["page"]); }
if($page=="" || $page<=0){$page=1;}
$result = $con->query("SELECT COUNT(*) AS num FROM tblproduct");
$row = $result->fetch_assoc();
$num_items = $row['num'];
$num_pages = ceil($num_items/$items_per_page);
if(($page > $num_pages) && $page != 1){$page = $num_pages;}
$limit_start = ($page-1) * $items_per_page;
//end get items per page
//show products
$sql = "SELECT * FROM tblproduct ORDER BY pid DESC LIMIT $limit_start, $items_per_page";
$result = $con->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td width=\"100\" ><img src=\"" . $row['imgurl'] . "\" height=\"100\" width=\"100\" /></td>";
echo "<td>" . $row['productname'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td width=\"100\"><a href=\"index.php?pid=" . $row['pid'] . "&page=$page\">Add To Cart</a></td>";
echo "</tr>";
}
}
?>
</table>
<?php
//page navigation links
if($num_pages>1)
{
echo "<p>";
if($page>1)
{
$ppage = $page-1;
echo "<a href=\"index.php?page=$ppage\">«Prev</a> ";
}
echo "$page/$num_pages";
if($page<$num_pages)
{
$npage = $page+1;
echo " <a href=\"index.php?page=$npage\"> Next»</a>";
}
echo "</p>";
}
?>
</div>
</div>
<div class="footer">
<?php
showFooter();
?>
</div>
</div>
</body>
</html>