Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search Contacts</title>
</head>
<p><body>
<h3>Search Contacts Details</h3>
<p>You may search either by first or last name</p>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="name">
</form>
<p><a href="?by=A">A</a> | <a href="?by=B">B</a> | <a href="?by=C">C</a> | <a href="?by=D">D</a> | <a href="?by=E">E</a> | <a href="?by=F">F</a> | <a href="?by=G">G</a> | <a href="?by=H">H</a> | <a href="?by=I">I</a> | <a href="?by=J">J</a> | <a href="?by=K">K</a> | <a href="?by=L">L</a> | <a href="?by=M">M</a> | <a href="?by=N">N</a> | <a href="?by=O">O</a> | <a href="?by=P">P</a> | <a href="?by=Q">Q</a> | <a href="?by=R">R</a> | <a href="?by=S">S</a> | <a href="?by=T">T</a> | <a href="?by=U">U</a> | <a href="?by=V">V</a> | <a href="?by=W">W</a> | <a href="?by=X">X</a> | <a href="?by=Y">Y</a> | <a href="?by=Z">Z</a></p>
</body>
</html>
Code: Select all
if(isset($_GET['by'])){
$letter=$_GET['by'];
//connect to the database
$db=mysql_connect ("servername", "username", "password") or die ('Could not connect to database ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("yourDatabase");
//-query the database table
$sql="SELECT ID, FirstName, LastName FROM Contacts WHERE Lastname LIKE '%" . $letter . ";
//-run the query against the mysql query function
$result=mysql_query($sql);
$numrows=mysql_num_rows($result);
echo "<p>" .$numrows . " results found for " . $letter . "</p>";
while($row=mysql_fetch_array($result)){
$FirstName =$row['FirstName'];
$LastName=$row['LastName'];
$ID=$row['ID'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<a href=\"search.php?id=$ID\">" .$FirstName . " " . $LastName . "</a></li>\n";
echo "</ul>";
}
}
Priya