-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
146 lines (121 loc) · 5.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
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
<?php
include("includes/header.inc.php");
echo header1("Home Page");
include("includes/nav.inc.php");
//connection to the database
include("config.php");
$conn=ConnectionFactory::connect();
$dosearch = false;
$results = array();
$search_term = "";
if ( isset( $_GET['search_for'] ) ) {
$search_term = $_GET['search_for']; // getting the value from the user
}
?>
<div class="container">
<div id="search">
<form action="" method="GET">
<h2> Search by Book Name - ISBN or Author </h2>
<label for="search-field">Search</label>
<input type="search" name="search_for" placeholder="Enter your search term..." results="5" value="<?php echo $search_term; ?>">
<p class="error">
<?php
if ( isset( $_GET['search_for'] ) ) {
$search_term = $_GET['search_for']; // getting the value from the user
$count = mb_strlen( $search_term );
if ($search_term == "")
{
$dosearch = false;
echo "please enter a book title, ISBN or author name to start search";
}
else if ($count < 3)
{
echo "please make sure you write more than 2 characters";
$dosearch = false;
}
else {
$dosearch = true;
}
}
?>
</p>
<input type="submit" class ="btn-1" value="Search">
<br/>
</form>
</div>
<div id="result"> <?php
//Check if search data was submitted
// ******************************************************************************** getting the value from the database **********************************************************************
if ( isset( $_GET['search_for'] ) ) {
// Include the search class
require_once( "classes/books.class.php");
if ($dosearch == true) {
// Store search term into a variable
$search_term = $_GET['search_for']; // getting the value from the user
// How many items to list per page
$limit = 2;
// Find out how many items are in the table
$total = $conn->query("SELECT COUNT(*) AS num FROM book
INNER JOIN authorbook ON book.isbn = authorbook.isbn
INNER JOIN author ON authorbook.author_id = author.author_id
WHERE book.title LIKE '%$search_term%'
OR book.isbn LIKE '%$search_term%'
OR author.f_name LIKE '%$search_term%'
OR author.l_name LIKE '%$search_term%'")->fetchColumn();
// working out How many pages
$pages = ceil($total / $limit);
// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
// Calculate the offset for the query
$offset = ($page - 1) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
// select book table and join the rest of tables to book table.
$query = "SELECT DISTINCT * FROM book
INNER JOIN authorbook ON book.isbn = authorbook.isbn
INNER JOIN author ON authorbook.author_id = author.author_id
WHERE book.title LIKE :search_term OR book.isbn LIKE :search_term OR author.f_name LIKE :search_term OR author.l_name LIKE :search_term
ORDER BY title DESC
LIMIT :limit
OFFSET :offset
";
$stmt = $conn->prepare($query);
$stmt->bindValue(':search_term',"%$search_term%"); //bind the value to search tearm like
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
if ($total == 0) {
echo "<p class='error'> No result found, please try another searh term</p>";
}
else {
echo "<p class='found'> we found $total books matchs our record </p>"; // show number of books matchs found
}
while ($result1 = $stmt->fetchObject()) { // generate the link for each record
echo "<div class='item'>";
echo "<p><a href=details.php?id=$result1->isbn> <img src=images/books/$result1->isbn> $result1->title </a> </p>";
echo "</div>";
}
if ($pages > 1 ) { // if the number of pages is more than 1 show the paging mechanisem
// The "back" link
$prevlink = ($page > 1) ? '<a href="?page=1&search_for='.$search_term.'" title="First page">«</a> <a href="?page=' . ($page - 1). ' & search_for='.$search_term.'" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '&search_for='.$search_term.'" title="Next page">›</a> <a href="?page=' . $pages . '&search_for='.$search_term.'" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';
// Display the paging information back - next
echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages', $nextlink, ' </p></div>';
}
}
} //end of do search
// ******************************************************************************** end of getting the value from the database **********************************************************************
?></div>
</div>
<?php
include("includes/footer.inc.php");
?>
</body>
</html>