Pagination undefined index - php

Hi I have here a code which will search for a name then returns the result paginated. I'm having an error on lines 19 and 20 which is
Notice: Undefined index: submit in C:\xampp\htdocs\eventreg\trypaginate2.php on line 19 - NOW OK
and
Notice: Undefined index: search in C:\xampp\htdocs\eventreg\trypaginate2.php on line 20 - NOW OK
But it is working. When I turn to the next page it won't show any results just a blank page.
Here is my html code:
<html>
<head>
<title>Title of your search engine</title>
</head>
<body>
<form action='trypaginate2.php' method='POST'>
<center>
<h1>My Search Engine</h1>
<input type='text' size='90' name='search'></br></br>
<input type='submit' name='submit' value='Search source code' ></br></br></br>
</center>
</form>
</body>
</html>
Then php code here:
<?php
if(array_key_exists('search', $_GET))
{
echo "hello";
$search = $_GET['search'];
echo "$search";
}
if(isset($_POST['submit']))
{
if(array_key_exists('search', $_POST))
{
echo "hi";
$search = $_POST['search'];
}
}
if(strlen($search)<=1)
echo "Search term too short";
else
{
echo "You searched for <b>$search</b> <hr size='1'></br>";
include("dbcon.php");
$search_exploded = explode (" ", $search);
$x = "";
$construct = "";
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="name LIKE '%$search_each%'";
else
$construct .="AND name LIKE '%$search_each%'";
}
echo $construct;
$constructs ="SELECT * FROM reginformation WHERE $construct";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
meaning</br>3. Please check your spelling";
else
{
echo "$foundnum results found !<p>";
$per_page = 5;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$getquery = mysql_query("SELECT * FROM reginformation WHERE $construct LIMIT $start, $per_page");
/*while($runrows = mysql_fetch_assoc($getquery))
{
$title = $runrows ['regID'];
$desc = $runrows ['name'];
$url = $runrows ['address'];
echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'>$url</a><p>
";
}*/
if(mysql_num_rows($getquery)>0)
{
while ($row = mysql_fetch_assoc($getquery)) {
// echo data
echo "<tr onClick =window.location='infodetailsresp.php?id=$row[regID]'><td>$row[name]</td><td>$row[emailadd]</td><td>$row[contactno]</td><td>$row[event]</td><td>$row[date_register]</td></tr><br>";
} // end while
}
else
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('No record found.');
window.location.href='reglistresp.php';
</SCRIPT>");
}
//Pagination Starts
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
$adjacents = 3;
$last = $max_pages - 1;
if($max_pages > 1)
{
//previous button
if (!($start<=0))
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
//pages
if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start)
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start)
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start)
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//close to end; only hide early pages
else
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start)
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
}
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
}
?>
EDIT! Changed my php code now I'm getting an error on line 34. The error is Notice: Undefined variable: search in C:\xampp\htdocs\eventreg\trypaginate2.php on line 34

You shouldn't check whether a key exists in array (which is what $_POST and $_GET are) by blindly assigning variables to those values or even by using isset - even though I know that seems like it would make sense. That's the behavior that PHP is sending you a warning about. That error comes from trying to find the value of things like $_POST['search'] or $_GET['submit'] when those values aren't set in that array.
Instead, use array_key_exists('submit', $_GET) or array_key_exists('search', $_POST) to check whether those values are set. If that expression evaluates to true, then you can proceed with accessing those values in your code and you should not receive any notices in the logs.

Related

search result with pagination using Post method

I have a search result with pagination that I make using this http://www.findsourcecode.com/php/pagination-in-search-engine/ as a reference. The website is using mysql and GET method for search. I change mysql to mysqli and work well. Then, I try to change the Get method to Post method for search result. The Post method is more secure based on what I readed. The question is how to change the method and make it work? The problem I face is, the result do not show at the second page but the first page show the result like I wanted. Please Helpp!!
Below is my coding..
index.php(the search form)
<div class="container">
<img src="image/sbback.jpg">
<div class="center">
<form class="form-wrapper cf" action="page.php" method="POST">
<input type="text" placeholder="Search here..." name="search" required>
<button type="submit" name="submit" value="Search source code" ><img src="image/search1.png" width="30" height="30" vspace="11"></button>
</form>
</div>
</div>
page.php
<?php
include( 'session.php' );
header('Cache-Control: no cache'); //no cache
if(isset($_SESSION['user']['role'])){
$role=$_SESSION['user']['role'];
}
?>
<?php
if (isset($_POST['submit'])){
$search = mysqli_real_escape_string($connection, $_POST['search']);
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
$connection=mysqli_connect("localhost","root","","company");
$search_exploded = explode (" ", $search);
$x = "";
$construct = "";
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="title LIKE '%$search_each%' OR description LIKE '%$search_each%' OR author LIKE '%$search_each%' OR date LIKE '%$search_each%' OR time LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%' OR description LIKE '%$search_each%' OR author LIKE '%$search_each%' OR date LIKE '%$search_each%' OR time LIKE '%$search_each%'";
}
$constructs ="SELECT * FROM tbl_uploads WHERE $construct";
$run = mysqli_query($connection,$constructs);
$foundnum = mysqli_num_rows($run);
if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
meaning</br>3. Please check your spelling";
else
{
echo "$foundnum results found !<p>";
$per_page = 10;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$sql = "SELECT * FROM tbl_uploads WHERE $construct LIMIT $start,$per_page";
$getquery = mysqli_query($connection,$sql);
while($runrows = mysqli_fetch_assoc($getquery))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$author = $runrows ['author'];
$date = $runrows ['date'];
$time = $runrows ['time'];
echo '<a href="uploads/'.$runrows['file'].'">
'.$runrows['title'].'</a>
<p>'.$desc.'</p>
<p>'.$author.' / '.$date.' / '.$time.'</p>';
}
//Pagination Starts
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
$adjacents = 3;
$last = $max_pages - 1;
if($max_pages > 1)
{
//previous button
if (!($start<=0))
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
//pages
if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='page.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//close to end; only hide early pages
else
{
echo " <a href='page.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
}
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
}
}
?>

Quick PHP Pagination not working

Found a working code on PHP pagination here and i am trying to integrate this code into my search engine. I tried but the pagination function doesn't seem to work please can anybody tell me what's wrong with my code and any possible fixes.
<?php
if(isset($_GET['search']))
{
$search = $_GET['search'];
$condition = '';
$query = explode(" ", $_GET["search"]);
foreach($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR keywords LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$sql_query = "SELECT * FROM questions WHERE " . $condition;
$result = $db->query($sql_query);
echo $sql_query;
$queryCount = "SELECT COUNT(*) as count FROM questions WHERE " . $condition;
$countRet = $db->querySingle($queryCount);
if ($countRet > 0)
{
$perpage = 1;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages= ceil($countRet / $perpage);
if (!$start) {
$start = 0;
$getQuery = $db->query("SELECT * FROM questions WHERE $condition LIMIT $start, $perpage");
while($row = $getQuery->fetchArray(SQLITE3_ASSOC))
{
echo '<tr><td>'.$row["question"].'</td></tr>';
}
//Pagination Codes
echo "<center>";
$prev = $start - $perpage;
$next = $start + $perpage;
$adjacent = 3;
$last = $max_pages - 1;
if ($max_pages > 1) {
//prev button
if (!$start <= 0)
{
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
}
if ($max_pages < 7 + ($adjacent * 2)) {
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacent * 2); $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
elseif ($max_pages - ($adjacent * 2) > ($start / $perpage) && ($start / $perpage) > ($adjacent * 2)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage)+1; $counter < ($start /$perpage) + $adjacent + 2; $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage) + 1; $counter <= $max_pages; $counter++) {
if ($i = $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
}
//next button
if (!($start >= $countRet - $perpage)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
}
else
{
echo '<label>No data found</label>';
}
}
?>
The code above doesn't seem to work for some reason. I have tried to debug but cant't seem to find the error or errors. Any reasonable help would do with this. Thanks in advance.
First you set $start in your ternary logic:
$start = isset($_GET['start']) ? $_GET['start']: '';
After this, you check whether $start exists or not, and if it doesn't, you set it 0:
if (!$start) {
$start = 0;
$getQuery ...
while($row = $getQuery ...
The problem is all of your query and pagination logic is inside of this if conditional; if $GET['start'] is set, then your query and pagination logic will never trigger. And on top of this, all of your outputted <a> links do indeed send the start parameter:
<a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>
To resolve this, simply set your ternary to 0 for the failure condition, and omit the if conditional entirely:
$start = isset($_GET['start']) ? $_GET['start']: 0;
This can be seen in the following:
$start = isset($_GET['start']) ? $_GET['start']: 0;
$max_pages= ceil($countRet / $perpage);
$getQuery = $db->query("SELECT * FROM questions WHERE $condition LIMIT $start, $perpage");
while($row = $getQuery->fetchArray(SQLITE3_ASSOC))
{
echo '<tr><td>'.$row["question"].'</td></tr>';
}
//Pagination Codes
echo "<center>";
$prev = $start - $perpage;
$next = $start + $perpage;
$adjacent = 3;
$last = $max_pages - 1;
if ($max_pages > 1) {
//prev button
if (!$start <= 0)
{
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
}
if ($max_pages < 7 + ($adjacent * 2)) {
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacent * 2); $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
elseif ($max_pages - ($adjacent * 2) > ($start / $perpage) && ($start / $perpage) > ($adjacent * 2)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage)+1; $counter < ($start /$perpage) + $adjacent + 2; $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage) + 1; $counter <= $max_pages; $counter++) {
if ($i = $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
}
//next button
if (!($start >= $countRet - $perpage)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";

php search with pagination

i get undefined index: start at the line when i use $start = $_GET['start'];
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","");
mysql_select_db("page");
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{$x=0;
$construct="";
$x++;
if($x==1)
$construct .="name LIKE '%$search_each%'";
else
$construct .="AND name LIKE '%$search_each%'";
}
$constructs ="SELECT * FROM info WHERE $construct";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry";
else
{
echo "$foundnum results found !<p>";
$per_page = 1;
$start = $_GET['start'];
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$getquery = mysql_query("SELECT * FROM info WHERE $construct LIMIT $start, $per_page");
while($runrows = mysql_fetch_assoc($getquery))
{
$id = $runrows ['id'];
$name = $runrows ['name'];
$email = $runrows ['email'];
echo " $id $name<br>";
}
//Pagination Starts
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
$adjacents = 3;
$last = $max_pages - 1;
if($max_pages > 1)
{
//previous button
if (!($start<=0))
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
//pages
if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='index.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//close to end; only hide early pages
else
{
echo " <a href='index.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
}
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
?>
Try this.
$start = (int) (!empty($_GET['start'])) ? $_GET['start'] : 0;

adding search filters in php

This is a search engine & pagination with one filter which is according to a word in "search", I want to add two more search filters: role & search between two dates.
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","");
mysql_select_db("page");
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{$x=0;
$construct="";
$x++;
if($x==1)
$construct .="name LIKE '%$search_each%'";
}
$constructs ="SELECT * FROM info WHERE $construct";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry";
else
{
echo "$foundnum results found !<p>";
$per_page = 1;
$start = $_GET['start'];
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$getquery = mysql_query("SELECT * FROM info WHERE $construct LIMIT $start, $per_page");
while($runrows = mysql_fetch_assoc($getquery))
{
$name = $runrows ['name'];
}
//Pagination Starts
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
$adjacents = 3;
$last = $max_pages - 1;
if($max_pages > 1)
{
//previous button
if (!($start<=0))
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
//pages
if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='index.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//close to end; only hide early pages
else
{
echo " <a href='index.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
}
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
?>
You should rewrite you loop like this
//by name
$construct="true";
foreach($search_exploded as $search_each) {
$construct .= " AND name LIKE '%$search_each%'";
}
//by role
if($role) {
$construct .= " AND role = '$role'";
}
//by date
if($start) {
$construct .= " AND the_date >= '$start'";
}
if($end) {
$construct .= " AND the_date <= '$end'";
}
Otherwise you constantly reset the values of $x (which is not necessary anyway) and $construct.
Of course you would have to GET the dates/role and format them properly.

How to add Autocomplete on a search engine

may I ask how can I add an autocomplete for my simple search engine.
Below is the Index.php
<html>
<head>
<title>Title of your search engine</title>
</head>
<body>
<form action='search.php' method='GET'>
<center>
<h1>My Search Engine</h1>
<input type='text' size='90' name='search'></br></br>
<input type='submit' name='submit' value='Search source code' ></br></br></br>
</center>
</form>
</body>
</html>
And here's the search.php
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
$m_Host = "localhost";
$m_User = "root";
$m_Pass = "Kyrene";
$m_Db = "ensemble";
$connect = mysql_connect($m_Host,$m_User,$m_Pass) or die(mysql_error());
echo "<br />";
mysql_select_db($m_Db,$connect) or die(mysql_error());
error_reporting(error_reporting()&~E_NOTICE);
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="title LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%'";
}
$constructs ="SELECT * FROM test WHERE $construct";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
meaning</br>3. Please check your spelling";
else
{
echo "$foundnum results found !<p>";
$per_page = 1;
$start = $_GET['start'];
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$getquery = mysql_query("SELECT * FROM test WHERE $construct LIMIT $start, $per_page");
while($runrows = mysql_fetch_assoc($getquery))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];
echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'>$url</a><p>
";
}
//Pagination Starts
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
$adjacents = 3;
$last = $max_pages - 1;
if($max_pages > 1)
{
//previous button
if (!($start<=0))
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
//pages
if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//close to end; only hide early pages
else
{
echo " <a href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
}
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
}
?>
I don't really have idea how should i start it. Any help is greatly appreciated. Thanks!
I will give you the general shpill of how to do it. 1. Use ajax to fetch data. 2. present data. if you are using jQuery
$("#search").autocomplete(
{
source : function(request, response) {
$.ajax({
type : 'get',
url : "http://hostname.com/suggest.php",
data : {
search : document.getElementById("search").value
},
success : function(result) {
response(result.list);
},
dataType : 'json',
global : false
});
}
});
in your suggest.php
<?php
$str = $_GET['search'];
//query database, get results
echo json_encode(array('list'=>$mydata));
?>
and the id='search' to your search input tag

Categories