I'm displaying the contents from the table on this web page.
When I click the header in the table, all the rows get sorted accordingly.
I want a list box with ( name, phone,email) options, so that when i select 'name' all the rows should get sorted with the 'name'.
I implemented it, but not sure if it is correct.
I want a search button, so that If i type 'A', in it i should get all the rows displayed starting with 'A'.
Your help is really appreciated and thanx you in Advance
Code: Select all
<html>
<head>
<title>View the Contents of the Table</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" alink="#FF0000" topmargin="1" marginheight="1">
<table width="730" align="center" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><font size="2" face="Arial, Helvetica, sans-serif"><strong><br>
<font size="1">Sort by Selection</font></strong></font></td>
</tr>
<tr>
<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif"> </font>
<form name="frm1" action="POST">
<select name="select" size="1" OnChange="location.href=this.options[this.selectedIndex].value" style="font-family: Arial, Helvetica, sans-serif; font-size:11px">
<option selected >---------</option>
<option value="view1.php?sort=name">Name</option>
<option value="view1.php?sort=phone">Phone</option>
<option value="view1.php?sort=email">Email</option>
</select></form> </td>
</tr>
<tr>
<td><font size="2" face="Arial, Helvetica, sans-serif"> <strong>Search</strong></font></td>
</tr>
<tr>
<td>
<form name="form1" method="post" action="">
<input type="search" name="textfield"><br><br>
<input name="Submit" type="button" value="Go">
<br>
</form></td>
</tr>
<tr>
<td><font size="2" face="Arial, Helvetica, sans-serif"> </font></td>
</tr>
<tr>
<td><font size="2" face="Arial, Helvetica, sans-serif"> </font></td>
</tr>
</table></td>
<!-- Paste here -->
<td valign="top">
<?php require_once('Connections/mysql_conn.php'); ?>
<?php
$rows_per_page = 5;
$result = mysql_query("SELECT Name FROM contact ");
$total_records = mysql_num_rows($result);
$pages = ceil($total_records / $rows_per_page);
mysql_free_result($result);
if (!isset($_GET['screen']))
{
$screen = 0; // Everything was being reset to 0
} else{
$screen = $_GET['screen'];
}
$start = $screen * $rows_per_page;
//$result = mysql_query("SELECT Name,Phone FROM contact LIMIT $start, $rows_per_page");
$query = "SELECT Name,Phone,Email FROM contact ";
if(isset ($_GET['sort']))
{
$sort = $_GET['sort'];
$query.= " ORDER BY $sort";
}
$query .= " LIMIT $start, $rows_per_page";
$result = mysql_query($query) or die("Query failed: ". mysql_error());
$rows = mysql_num_rows($result);
echo "<table border="1">";
echo "<tr><td>";
echo "<font size="2" face="Arial, Helvetica, sans-serif"><a href="".$_SERVER['PHP_SELF']."?sort=name"><b>Name</b></a></font></td>";
echo "<td><font size="2" face="Arial, Helvetica, sans-serif"><a href="".$_SERVER['PHP_SELF']."?sort=phone"><b>Phone</b></a></font></td>";
echo "<td><font size="2" face="Arial, Helvetica, sans-serif"><a href="".$_SERVER['PHP_SELF']."?sort=email"><b>Email</b></a></font></td></tr>";
//$row_num = 0;
while ($row = mysql_fetch_array($result))
{
//$color = ($row_num % 2)?"white":"black";
// $row_num++;
// echo '<tr style="background-color:'.$color.'">';
$Name = $row['Name'] ;
$Phone = $row['Phone'];
$Email = $row['Email'];
//make a display block to display the results on a html table row at a time
echo "<tr><td width="25%"><font size="2" face="Arial, Helvetica, sans-serif">$Name</font></td>";
echo "<td width="25%"><font size="2" face="Arial, Helvetica, sans-serif">$Phone</font></td>" ;
echo "<td width="25%"><font size="2" face="Arial, Helvetica, sans-serif">$Email</font></td> </tr>";
}
echo "</font></table>";
echo "<p><hr></p>";
// let's create the dynamic links now
if ($screen > 0) {
$url = "view1.php?screen=" . ($screen - 1);
echo "<a href="$url"><font size="2" face="Arial, Helvetica, sans-serif">Previous</font></a>\n";
}
// page numbering links now
for ($i = 0; $i < $pages; $i++) {
$url = "view1.php?screen=" . $i;
echo " | <a href="$url"><font size="2" face="Arial, Helvetica, sans-serif">$i</font></a> |";
}
if ((($screen+1)*$rows_per_page)< $total_records)
{
if ($screen < $pages)
{
$url = "view1.php?screen=" . ($screen + 1);
echo "<a href="$url"><font size="2" face="Arial, Helvetica, sans-serif">Next</font></a>\n";
}
}
?>
</td>
</tr>
</table>
</body>
</html>