-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsearch.php
115 lines (84 loc) · 3.79 KB
/
search.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
<?php
include_once('./includes/headerNav.php')
?>
<div class="dynamic-data-container-search">
<span><h3 style="color:grey;margin-left:2.4%">Search:<?php if(isset($_POST['search']))echo $_POST['search'] ?></h3></span>
<?php
//Very important SEO friendly code generated by myself (practices)
if( (!(empty($_POST['search']))) OR isset($_POST['catag']) ){
if(isset($_POST['catag'])){
$search_term = mysqli_real_escape_string($conn, $_POST['catag']);
}else{
$search_term = mysqli_real_escape_string($conn, $_POST['search']);
}
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page = 1;
}
include "includes/config.php";
/* define how much data to show in a page from database*/
$limit = 8;
//define from which row to start extracting data from database
$offset = ($page - 1) * $limit;
//making search_term string to array each value separated by space
$search_term_array = explode(" ",$search_term);
for($search=0; $search<sizeof($search_term_array); $search++){ //loop for search
$array_length = sizeof($search_term_array);
if($array_length >1){
$sql10 = "SELECT * FROM products
WHERE products.product_title LIKE '%{$search_term_array[$search]}%' AND products.product_catag LIKE '%{$search_term_array[$search+1]}%'
ORDER BY products.product_id DESC LIMIT {$offset},{$limit}";
}
if($array_length ==1){
$sql10 = "SELECT * FROM products
WHERE products.product_title LIKE '%{$search_term_array[$search]}%' OR products.product_catag LIKE '%{$search_term_array[$search]}%'
ORDER BY products.product_id DESC LIMIT {$offset},{$limit}";
}
break;
}
$result10 = $conn->query($sql10);
if ($result10->num_rows > 0) {
// output data of each row
while($row10 = $result10->fetch_assoc()) {
?>
<a href="product.php?id=<?php echo $row10['product_id']?>"><div class="product">
<img class='image' src="admin/upload/<?php echo $row10['product_img'] ?>" alt="product-img">
<div class="detail-cont">
<h5 class="title"><?php echo $row10['product_title'] ?> <p class="date"><?php echo $row10['product_date'] ?></p> </h5>
<p class="description"><?php echo $row10['product_desc'] ?>
<p class="price"><b>Rs.<?php echo $row10['product_price'] ?></b><br><span class="discount"><strike>5000</strike> -8%</span></p>
</div>
</div> </a>
<?php }}else { echo "<h4 style='color:red; margin-left:8%;border:1px solid aliceblue'>"."No record found"."</h4>";}
$conn->close();
?>
</div>
<!--Pagination-->
<div class="pag-cont-search">
<?php
include "includes/config.php";
// Pagination btn using php with active effects
$sql1 = "SELECT * FROM products";
$result1 = mysqli_query($conn, $sql1) or die("Query Failed.");
if(mysqli_num_rows($result1) > 0){
$total_products = mysqli_num_rows($result10);
$total_page = ceil($total_products / $limit);
echo "<div class='pagination'>";
for($i=1; $i<=$total_page; $i++){
//important this is for active effects that denote in which page you are in current position
if($page==$i){
$active = "active";
}else{
$active = "";
}
echo "<a href='search.php?page={$i}' class='{$active}'>".$i."</a>";
}
}
echo "</div>";
}//main if
else{
echo "<h4 style='color:red; margin-left:8%;border:1px solid aliceblue'>"."ERR_Insert on Search"."</h4>";
}
?>
</div>