I have NO php knowledge.. somehow I pieced this together.
How can I make sure this query do not show all result, If input is empty and there are no matches.
If no name is entered or there is no match I want to display "No records found" and do not want to display prev and next page either.
You can change and clean if you like.
Thank you VERY MUCH for your help in advance.
<?php
//
if(isset($_POST['Last_Name']) && isset($_POST['First_Name'])) {
$search = trim($_POST['Last_Name']);
$search1 = trim($_POST['First_Name']);
} else {
$search = trim($_GET['Last_Name']);
$search1 = trim($_GET['First_Name']);
}
if(!empty($search) && !empty($search1)) {
// do whatever you want since this is not empty [B]"I DO NOT KNOW WHAT TO DO"[/B]
}
// whatever else you want to do [B]"I DO NOT KNOW WHAT TO DO"[/B]
}
//
// how many rows to show per page
$rowsPerPage = 5;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
//
// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM example WHERE last LIKE '%$search%' AND first LIKE
'$search1%' LIMIT $offset, $rowsPerPage ")or die(mysql_error());
//
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['last'];
echo "</td><td>";
echo $row['first'];
echo "</td><td>";
echo $row['phone'];
echo "</td></tr>";
}
//
// how many rows we have in database
$numresults = mysql_query("SELECT * FROM example WHERE last LIKE '%$search%' AND first LIKE
'$search1%'")or die(mysql_error());
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
//
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link
// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page&fname=$search&lname=$search1\">[Prev]</a> ";
$first = " <a href=\"$self?page=1&fname=$search&lname=$search1\">[First Page]</a> ";
}
else
{
$prev = ' [Prev] '; // we're on page one, don't enable 'previous' link
$first = ' [First Page] '; // nor 'first page' link
}
// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page&fname=$search&lname=$search1\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage&fname=$search&lname=$search1\">[Last Page]
</a> ";
}
else
{
$next = ' [Next] '; // we're on the last page, don't enable 'next' link
$last = ' [Last Page] '; // nor 'last page' link
}
// print the page navigation link
//echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong>
//
//pages " . $next . $last;
echo "<tr><td colspan=3>";
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong>
pages " . $next . $last;
echo "</td></tr>";
//
//
?>