newest update to my search engine
Posted: Fri Feb 07, 2014 5:07 pm
my search engine now works, but there is some problem whit my pagination. Every time i click on "Next" the search bar gets an error saying that $k is undefined. And messes up the search result.
how do i fix it?
Code: Select all
<h1>Search:</h1>
<form action="search.php" method="GET" class="search">
<input type="text" name="k" size='50' value="<?php echo $_GET['k']; ?>" />
<input type="submit" name="submit" value='Search' />
</form>
<?php
$username = $_SESSION['username'];
if(isset($username))
{
include "navigator/navigator_login.php";
} else {
include "navigator/navigator.php";
}
?>
<hr />
<?php
include "connect.php";
$k = mysqli_real_escape_string($con, htmlentities(trim($_GET['k'])));
$terms = explode(" ", $k);
$query = "SELECT * FROM videos WHERE ";
$i = 0;
foreach($terms as $each) {
$i++;
if($i == 1) {
$query .= "description LIKE '%$each%' ";
} else {
$query .= "AND description LIKE '%$each%' ";
}
}
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$numrows = mysqli_num_rows($result);
$count_query = mysqli_query($con, "SELECT NULL FROM videos");
$count = mysqli_num_rows($count_query);
if(isset($_GET['search'])) {
$page = preg_replace("#[^0-9]#","",$_GET['search']);
} else {
$page = 1;
}
$perPage = 2;
$lastPage = ceil($count/$perPage);
if($page < 1) {
$page = 1;
} else {
if($page > $lastPage) {
$page = $lastPage;
}
}
$limit = "LIMIT " . ($page - 1) * $perPage . ", $perPage";
$query1 = mysqli_query($con, "SELECT * FROM videos ORDER BY id DESC $limit");
if($lastPage != 1) {
if($page != 1) {
$prev = $page - 1;
$pagination .= '<a href="search.php?search='.$prev.'">Previous</a> ';
}
if($page != $lastPage) {
$next = $page + 1;
$pagination .= '<a href="search.php?search='.$next.'">Next</a>';
}
}
while($row = mysqli_fetch_array($query1)) {
$title = $row['title'];
$url = $row['url'];
$description = $row['description'];
if($numrows > 0)
{
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$title = $row['title'];
$url = $row['url'];
$description = $row['description'];
if($k == "") {
echo "";
} else {
echo "<h3><a href='$url'>$title</a></h3>
$description";
}
}
} else {
echo "no results on $k";
}
}
echo $pagination;
mysqli_close($con);
?>