Click here to load reader
Upload
vineet-kumar-saini
View
4.313
Download
3
Embed Size (px)
DESCRIPTION
Using this article one can get easily understand alphabetical search in php.
Citation preview
PHP Tutorials By Vineet Kumar Saini
http://vineetsaini.wordpress.com
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
<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 />
PHP Tutorials By Vineet Kumar Saini
http://vineetsaini.wordpress.com
</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);
?>
PHP Tutorials By Vineet Kumar Saini
http://vineetsaini.wordpress.com
<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
If we click on ‘V’ then display result of ‘V’. Like as following image
PHP Tutorials By Vineet Kumar Saini
http://vineetsaini.wordpress.com
If we click on ‘View All’ then display full data from table.Like as following image