Posted: Fri Jun 04, 2004 2:34 pm
I understand this part
Actually I'm trying to implement the following.
1. Display the results from the table( Done)
2. Sort the contents of the table by clicking the header in the table( Done)
3. SOrt the contents of the table by selecting an item from the list ( DOne in a crude way i.e, by using the above one)
4. Put a search box and when the user enters somethingh( eg: A ) and hit the submit button, i should get all the rows that start with A. ( TRYING TO DO---)
Actually i wanted all this functionality in One page. SO am Struggling----------Here is my code
I have the codes for different functions , but donot know how to integrate them.
Code: Select all
$result = mysql_query("SELECT Name,Phone FROM contact LIMIT $start, $rows_per_page");
$rows = mysql_num_rows($result);
$sort = $_POST['sort']; <==== this used to be GET now changed to POST
$orderby = " order by $sort";
$result = $result.$orderby;1. Display the results from the table( Done)
2. Sort the contents of the table by clicking the header in the table( Done)
3. SOrt the contents of the table by selecting an item from the list ( DOne in a crude way i.e, by using the above one)
4. Put a search box and when the user enters somethingh( eg: A ) and hit the submit button, i should get all the rows that start with A. ( TRYING TO DO---)
Actually i wanted all this functionality in One page. SO am Struggling----------Here is my code
I have the codes for different functions , but donot know how to integrate them.
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="<?php $_SERVER['PHP_SELF'].?search=name ?>">
<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
// Connection to Host and Database
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);
//SEARCH FUNCTION
if(isset($_POST['search']))
{
$search = ($_POST['search']);
}
$sql = "select * from contact where name LIKE '%$search%' ";
$results = mysql_query($sql) or die ("Can't query because ".mysql_error());
if ($results){
//show the results
}
if (!isset($_GET['screen']))
{
$screen = 0; // Everything was being reset to 0
} else{
$screen = $_GET['screen'];
}
$start = $screen * $rows_per_page;
//Search Function
//End of Search Function
$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>