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