-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
249 lines (200 loc) · 10.9 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
// Include config file
require_once "config/db.php";
// Initialize the session
ob_start();
session_start();
// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Image Search API</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<style>
.padding{
padding:10rem !important;
}
body{
background-color: #f8fafe;
}
.domain-form .form-group {
border: 1px solid #9ff0c8;
padding: 20px;
}
.domain-form .form-group input {
height: 70px !important;
border: transparent;
}
.form-control {
height: 52px !important;
background: #fff !important;
color: #3a4348 !important;
font-size: 18px;
border-radius: 0px;
-webkit-box-shadow: none !important;
box-shadow: none !important;
}
.px-4 {
padding-left: 1.5rem !important;
}
.domain-form .form-group .search-domain {
background: #22d47b;
border: 2px solid #22d47b;
color: #fff;
-webkit-border-radius: 0;
-moz-border-radius: 0;
-ms-border-radius: 0;
border-radius: 0;
}
.domain-price span {
color: #3a4348;
margin: 0 10px;
}
.domain-price span small {
color: #24bdc9;
}
</style>
</head>
<body>
<!--including navbar-->
<?php include "navbar.php"; ?>
<div class="row justify-content-center">
<div class="col-md-8 ftco-animate fadeInUp ftco-animated">
<form action="#" class="domain-form" method="POST">
<div class="form-group d-md-flex">
<input type="text" name="search" class="form-control px-4" placeholder="Enter your search term..." >
<input type="submit" name="submit" class="search-domain btn btn-primary px-5" value="Search Image">
</div>
</form>
<p class="domain-price text-center">Example : <small>google chrome,</small> <small> dinosaur, </small><small>putin on a horse</small></p>
</div>
</div>
<?php
if(isset($_POST["submit"])) {
$sql = "SELECT hid, sterm, json FROM history WHERE sterm = ?"; // Checking sql to count the number of rows
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $_POST['search']);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0) { //If the record already exist!
//echo "existing record!";
$stmt->bind_result($hid, $sterm, $json);
$rows = [];
$stmt->fetch();
$rows[] = [
'hid' => $hid,
'search_term' => $sterm,
'json_data' => $json
];
$json_data_array = json_decode($rows[0]['json_data']); //getting only the 1st array index
//print_r($json_data_array);
?>
<div class="container">
<div class="alert alert-warning text-center" role="alert">
Calling from Database, no API is used!
</div>
<h5 class="card-title text-center">Search term: "<?php echo $json_data_array->image_name;?>"</h5>
<div class="row row-cols-1 row-cols-md-3">
<?php
foreach($json_data_array as $array) {
// print_r($array);
if (is_array($array) || is_object($array)) {
foreach($array as $key=>$value) {
// print_r($value);
?>
<div class="col mb-4">
<div class="card-deck" >
<div class="card">
<img src="<?php echo $value;?>" class="card-img-top img-fluid img-thumbnail" alt="...">
<div class="card-body">
<h5 class="card-title">Search term used: <?php echo $json_data_array->image_name;?></h5>
<p class="card-text"><small class="text-muted"><strong>IMG Source:</strong> <a href="<?php echo $value;?>" target="_blank"><?php echo $value;?> </a><pre></pre></small></p>
</div>
</div>
</div>
</div>
<?php
}
}
}//endforech
?>
</div>
</div>
<?php
} else { //insert new record here into db and display using API
?>
<?php
//create curl resource
$ch = curl_init();
$search_string = $_POST['search'];
$url = "https://imsea.herokuapp.com/api/1?q=".urlencode($search_string);
//set curl options
curl_setopt($ch, CURLOPT_URL, $url);
//For accessing the https sites
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//For saving the output in a variable (which we create during execution) , instead of displaying the webpage. To see that output , just echo it out.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//run curl (i.e. execute curl resource
$output = curl_exec($ch);
//Insert into DB
if(!empty($output)){
$stmt = $conn->prepare("INSERT INTO history(sterm, json) VALUES (?, ?)");
$stmt->bind_param('ss', $search_string, $output); // Bind $search to the parameter
/* Execute prepared statement */
$stmt->execute();
}
$data = json_decode($output);
//print_r($data);
?>
<div class="container">
<div class="alert alert-primary text-center" role="alert">
API has been used!
</div>
<h5 class="card-title text-center">Search term: "<?php echo $data->image_name;?>"</h5>
<div class="row row-cols-1 row-cols-md-3">
<?php
foreach($data as $array) {
// print_r($array);
if (is_array($array) || is_object($array)) {
foreach($array as $key=>$value) {
// print_r($value);
?>
<div class="col mb-4">
<div class="card-deck" >
<div class="card">
<img src="<?php echo $value;?>" class="card-img-top img-fluid img-thumbnail" alt="...">
<div class="card-body">
<h5 class="card-title">Search term: <?php echo $data->image_name;?></h5>
<p class="card-text"><small class="text-muted"><strong>IMG Source:</strong> <a href="<?php echo $value;?>" target="_blank"><?php echo $value;?> </a><pre></pre></small></p>
</div>
</div>
</div>
</div>
<?php
}
}
}
?>
</div>
</div>
<?php
//close curl resource
curl_close($ch);
} $stmt->close();
?>
<?php } //end submit?>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
<?php
} else { //redirect to login
header("location: index.php");
}
?>