Hi, I am trying to workout how I can changed the buttons I have for sorting the fields in the table into hyperlinks. I have worked out how to do one, but what about if there are several forms on the same page?

Thanks

<?php
//Connect to server
$conn = mysql_connect ("localhost", "tatty27", "monkeyboy") or die ("Could not connect to database.");
//connect to database
mysql_select_db("tatty27_customers") or die ("Database does not exist.");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Database Exercise 2.2</title>
<style type="text/css">

body {font-family:Verdana, Geneva, sans-serif;
      font-size:16px;
}

</style>
</head>

<body>


<?php
//create table

echo '<table border="1">';
	echo '<tr>';
	 echo '<td nowrap>';
	 echo "<form method=\"post\" action = \"\">";
	 echo "<input type=\"submit\" value=\"Sort By Customer ID\" name=\"custID\" /><input type=\"submit\" value=\"Desc\" name=\"custDesc\" />";
	 echo "</form>";
	 echo '</td>';
	 echo '<td nowrap>';
	 echo "<form method=\"post\" action = \"\">";
	 echo "<input type=\"submit\" value=\"Sort By Name\" name=\"name\" /><input type=\"submit\" value=\"Desc\" name=\"nameDesc\" />";
	 echo "</form>";
	 echo '</td>';
	 echo '<td nowrap>';
	 echo "<form method=\"post\" action = \"\">";
	 echo "<input type=\"submit\" value=\"Sort By Address\" name=\"address\" /><input type=\"submit\" value=\"Desc\" name=\"addDesc\" />";
	 echo "</form>";
	 echo '</td>';
	 echo '<td nowrap>';
	 echo "<form method=\"post\" action = \"\">";
	 echo "<input type=\"submit\" value=\"Sort By Postcde\" name=\"postcode\" /><input type=\"submit\" value=\"Desc\" name=\"postDesc\" />";
	 echo "</form>";
	 echo '</td>';
	 echo '<td nowrap>';
	 echo "<form method=\"post\" action = \"\">";
	 echo "<input type=\"submit\" value=\"Sort By Telephone\" name=\"telephone\" /><input type=\"submit\" value=\"Desc\" name=\"teleDesc\" />";
	 echo "</form>";
	 echo '</td>';
	 echo '<td nowrap>';
	 echo "<form method=\"post\" action = \"\">";
	 echo "<input type=\"submit\" value=\"Sort By Email\" name=\"email\" /><input type=\"submit\" value=\"Desc\" name=\"emailDesc\" />";
	 echo "</form>";
	 echo '</td>';

$sql = "SELECT * FROM customers";

$result = mysql_query($sql);
   
   if (isset($_POST['custID'])){
	   $sql = "SELECT * FROM customers order by customerID";
   }else if(isset ($_POST['custDesc'])){
	   $sql = "SELECT * FROM customers order by customerID DESC";
   }else if(isset ($_POST['name'])){
	   $sql = "SELECT * FROM customers order by name";
   }else if(isset ($_POST['nameDesc'])){
	   $sql = "SELECT * FROM customers order by name DESC";
   }else if(isset ($_POST['address'])){
	   $sql = "SELECT * FROM customers order by address";
   }else if(isset ($_POST['addDesc'])){
	   $sql = "SELECT * FROM customers order by address DESC";
   }else if(isset ($_POST['postcode'])){
	   $sql = "SELECT * FROM customers order by postcode";
   }else if(isset ($_POST['postDesc'])){
	   $sql = "SELECT * FROM customers order by postcode DESC";
   }else if(isset ($_POST['telephone'])){
	   $sql = "SELECT * FROM customers order by telephone";
   }else if(isset ($_POST['teleDesc'])){
	   $sql = "SELECT * FROM customers order by telephone DESC";
   }else if(isset ($_POST['email'])){
	   $sql = "SELECT * FROM customers order by email";
   }else if(isset ($_POST['emailDesc'])){
	   $sql = "SELECT * FROM customers order by email DESC";
   }
   
   $result = mysql_query($sql);
   //repeat loop for each row of results returned
while ($row = mysql_fetch_array($result)or die(mysql_error())){
			//enter returned values into table					
			echo "<tr><td>$row[customerID]</td>".' '."<td>$row[name]</td>".' '."<td>$row[address]</td><td>$row[postcode]</td>".' '."<td>$row[telephone]</td>".' '."<td>$row[email]</td><br />";
			}
		echo '</td>';
	echo '</tr>';
echo'<table>';
   
   
   
   ?>
   
</body>
</html>

Recommended Answers

All 6 Replies

dunno, but style the input.submit

input.submit {background:none;border:0;color:#ff0000; }
<input type='submit' style="background:none;border:0;color:#ff0000;" value='something'>

(untested)

<?php
//Connect to server
$conn = mysql_connect ("localhost", "tatty27", "monkeyboy") or die ("Could not connect to database.");
//connect to database
mysql_select_db("tatty27_customers") or die ("Database does not exist.");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Database Exercise 2.2</title>
<style type="text/css">

body {font-family:Verdana, Geneva, sans-serif;
      font-size:16px;
}

</style>
</head>

<body>


<?php
//create table

$url=""; //Provide URL od this page here

echo '<table border="1">';
	echo '<tr>';
	 echo '<td nowrap>';
	 echo "<a href=\"".$url."?sortBy=customerID\">Sort By Customer ID</a>";
	 echo "<a href=\"".$url."?sortBy=customerID&desc=1\">Sort By Customer ID DESC</a>";
	 echo '</td>';
	 
	 echo '<td nowrap>';
	 echo "<a href=\"".$url."?sortBy=name\">Sort By Customer ID</a>";
	 echo "<a href=\"".$url."?sortBy=name&desc=1\">Sort By Customer ID DESC</a>";
	 echo '</td>';
	 
	 echo '<td nowrap>';
	 echo "<a href=\"".$url."?sortBy=address\">Sort By Address</a>";
	 echo "<a href=\"".$url."?sortBy=address&desc=1\">Sort By Address DESC</a>";
	 echo '</td>';
	 
	 echo '<td nowrap>';
	 echo "<a href=\"".$url."?sortBy=postcode\">Sort By Postcde</a>";
	 echo "<a href=\"".$url."?sortBy=postcode&desc=1\">Sort By Postcde DESC</a>";
	 echo '</td>';
	 
	 echo '<td nowrap>';
	 echo "<a href=\"".$url."?sortBy=telephone\">Sort By Telephone</a>";
	 echo "<a href=\"".$url."?sortBy=telephone&desc=1\">Sort By Telephone DESC</a>";
	 echo '</td>';
	 
	 echo '<td nowrap>';
	 echo "<a href=\"".$url."?sortBy=email\">Sort By Email</a>";
	 echo "<a href=\"".$url."?sortBy=email&desc=1\">Sort By Email DESC</a>";
	 echo '</td>';

$sql = "SELECT * FROM customers";



	$sql="SELECT * FROM customers";
	if(isset($_REQUEST['sortBy']))
	{
		$sql.=" ".$_REQUEST[['sortBy'];
		
		if(isset($_REQUEST['desc']))
		{
			$sql.=" DESC";
		}
	}
   

   $result = mysql_query($sql);
   //repeat loop for each row of results returned
while ($row = mysql_fetch_array($result)or die(mysql_error())){
			//enter returned values into table					
			echo "<tr><td>$row[customerID]</td>".' '."<td>$row[name]</td>".' '."<td>$row[address]</td><td>$row[postcode]</td>".' '."<td>$row[telephone]</td>".' '."<td>$row[email]</td><br />";
			}
		echo '</td>';
	echo '</tr>';
echo'<table>';
   
   
   
   ?>
   
</body>
</html>

If you want to do it that way(a tidy, browser independent, way to do it ) $url = $_SERVER['PHP_SELF'];

Thanks so much, both of you.

I spent 2 days looking for this but all I could find were solutions using javascript which seemed unnecessary. I knew there was a way to do it like this and you guys have confirmed it and shown me how to do it.

Thank you again

Tatty

If you want to do it that way(a tidy, browser independent, way to do it ) $url = $_SERVER['PHP_SELF'];

Yes ofcourse that is much better. My solution is not the perfacet one. Their are many security related risks in that also. I just tried to provide a quick solution to that.

Yes ofcourse that is much better. My solution is not the perfacet one. Their are many security related risks in that also. I just tried to provide a quick solution to that.

No bloke, apologies, i meant your method is tidy and browser independent,
css tweaks of standard items dont work in all browsers
the php_self code floats in my head all the time, its a convenient way to self reference a file, you already defined where to use it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.