Alphabetical Search in PHP

First of all we create a table in database. Suppose we created ‘student’ table in ‘test’ database. Like as following image

search in php

 

 

 

 

 

 

Now  we create index.php file like as following code.

<html>
<body>
<p><center>
<h3>Alphabetical Search in PHP</h3>

<form action="index.php" method="post" name="search" onclick="submit">
<a href="index.php?letter=A">A</a> |
<a href="index.php?letter=B">B</a> |
<a href="index.php?letter=C">C</a> |
<a href="index.php?letter=D">D</a> |
<a href="index.php?letter=E">E</a> |
<a href="index.php?letter=F">F</a> |
<a href="index.php?letter=G">G</a> |
<a href="index.php?letter=H">H</a> |
<a href="index.php?letter=I">I</a> |
<a href="index.php?letter=J">J</a> |
<a href="index.php?letter=K">K</a> |
<a href="index.php?letter=L">L</a> |
<a href="index.php?letter=M">M</a> |
<a href="index.php?letter=N">N</a> |
<a href="index.php?letter=O">O</a> |
<a href="index.php?letter=P">P</a> |
<a href="index.php?letter=Q">Q</a> |
<a href="index.php?letter=R">R</a> |
<a href="index.php?letter=S">S</a> |
<a href="index.php?letter=T">T</a> |
<a href="index.php?letter=U">U</a> |
<a href="index.php?letter=V">V</a> |
<a href="index.php?letter=W">W</a> |
<a href="index.php?letter=X">X</a> |
<a href="index.php?letter=Y">Y</a> |
<a href="index.php?letter=Z">Z</a> |
<a href="index.php?letter=">View All</a>
</form>

<br />

</center></p>
</body>
</html>
<?php
if(isset($_GET['letter']))
{
$con=mysql_connect('localhost','root','');
if(!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test',$con);
$char=$_GET['letter'];

if($char)
{
$query = "SELECT * FROM student WHERE name LIKE '$char%' ";
$result = mysql_query($query);
$count=mysql_num_rows($result);
if($count >= 1)
{
?>
<center>
<table cellpadding="1" cellspacing="0" border="1" width="35%" >
<tr>
<th>Name</th>
<th>Class</th>
<th>Email</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{

?>
<tr>
<td align="center"><?php echo $row['name']; ?></td>
<td align="center"><?php echo $row['class']; ?></td>
<td align="center"><?php echo $row['email']; ?></td>
</tr>
<?php
}
?>
</table>
<center>
<?php
}
else
{
echo 'Records Not Found';
}
}
else
{
$query = "SELECT * FROM student";
$result = mysql_query($query);
?>
<center>
<table cellpadding="1" cellspacing="0" border="1" width="35%" >
<tr>
<th>Name</th>
<th>Class</th>
<th>Email</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{

?>
<tr>
<td align="center"><?php echo $row['name']; ?></td>
<td align="center"><?php echo $row['class']; ?></td>
<td align="center"><?php echo $row['email']; ?></td>
</tr>
<?php
}
?>
</table>
</center>
<?php
}
}
?>

Then we run the code and click on ‘A’ then display result of A. Like as following image

Search in PHP

If we click on ‘V’ then display result of ‘V’. Like as following image

Search in PHP

If we click on ‘View All’ then display full data from table.Like as following image

Search in PHP

Advertisements
Categories: PHP & Joomla | Tags: , , , , , , | Leave a comment

Post navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: