Jump to content

Order by problem


justinh

Recommended Posts

Okay, I am displaying a table of clients in an HTML table.

 

I'm trying to get it to where when they click on the title of each column it sorts by that column.

 

Heres what I have

 

 

this is the function in functions.php

 
function viewClient($party, $sort = "none"){

if($party == "all"){
echo "<table border=0 cellpadding=2 cellspacing=5 width=%100><tr><td bgcolor=black><b><font color=white><a href=\"".$_SERVER['PHP_SELF']."?sort=client_id\">Client ID</a></font></B></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_firstname\">First Name</a></b></td>
<td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_lastname\">Last Name</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_address\">Address</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_city\">City</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_zip\">Zip Code</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_state\">State</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_email\">Email</a></b></td><td><b>
</b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_cell\">Cell Number</b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_home\">Home Number</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_fax\">Fax Number</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_partyid\">Party Name</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_under\">Under 18 years old</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_hunter\">Hunter</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_partycontact\">Party Contact</b></td></tr>";
if($sort != "none"){

$query = mysql_query("SELECT * FROM hrclienta ORDER BY '$sort'");

}else{

$query = mysql_query("SELECT * FROM hrclienta ORDER BY client_id");
}

*this is not the whole function, just didnt want to post a lot of code*

 

and this is the code im using to display it

 

 <?php viewClient(all, $_GET['sort']); ?>

 

 

When i click on the column title, nothing happens =/

 

thanks in advance :)

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/140841-order-by-problem/
Share on other sites

 

<?php 

function viewClient($party, $sort = "none"){

if($party == "all"){
echo "<table border=0 cellpadding=2 cellspacing=5 width=%100><tr><td bgcolor=black><b><font color=white><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_id\">Client ID</a></font></B></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_firstname\">First Name</a></b></td>
<td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_lastname\">Last Name</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_address\">Address</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_city\">City</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_zip\">Zip Code</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_state\">State</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_email\">Email</a></b></td><td><b>
</b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_cell\">Cell Number</b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_home\">Home Number</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_fax\">Fax Number</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_partyid\">Party Name</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_under\">Under 18 years old</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_hunter\">Hunter</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_partycontact\">Party Contact</b></td></tr>";
if($sort != "none"){

$query = mysql_query("SELECT * FROM hrclienta ORDER BY '$sort'");

}else{

$query = mysql_query("SELECT * FROM hrclienta ORDER BY cl_id");
}

$rowcount2 = 1;

while($viewclients = mysql_fetch_array($query)){

$cl_id = $viewclients['cl_id'];
$cl_firstname = $viewclients['cl_firstname'];
$cl_lastname = $viewclients['cl_lastname'];
$cl_address = $viewclients['cl_address'];
$cl_city = $viewclients['cl_city'];
$cl_zip = $viewclients['cl_zip'];
$cl_state = $viewclients['cl_state'];
$cl_email = $viewclients['cl_email'];
$cl_cell = $viewclients['cl_cell'];
$cl_home = $viewclients['cl_home'];
$cl_fax = $viewclients['cl_fax'];
$cl_partyid = $viewclients['cl_partyid'];
$cl_partyname = getPartyName($cl_partyid);
$cl_under = $viewclients['cl_under'];
$cl_hunter = $viewclients['cl_hunter'];
$cl_partycontact = $viewclients['cl_partycontact'];

if($rowcount2 == 3){
$rowcount2 = 1;
}
if($rowcount2 == 1){
echo "<tr bgcolor=\"#655E54\"><td>$cl_id</td><td>$cl_firstname</td><td>$cl_lastname</td><td>$cl_address</td><td>$cl_city</td><td>
$cl_zip</td><td>$cl_state</td><td>$cl_email</td><td>$cl_cell</td><td>$cl_home</td><td>$cl_fax</td><td>$cl_partyname</td><td>$cl_under</td><td>$cl_hunter</td><td>$cl_partycontact</td></tr>";
}
if($rowcount2 == 2){

echo "<tr bgcolor=\"#C1BAB1\"><td>$cl_id</td><td>$cl_firstname</td><td>$cl_lastname</td><td>$cl_address</td><td>$cl_city</td><td>
$cl_zip</td><td>$cl_state</td><td>$cl_email</td><td>$cl_cell</td><td>$cl_home</td><td>$cl_fax</td><td>$cl_partyname</td><td>$cl_under</td><td>$cl_hunter</td><td>$cl_partycontact</td></tr>";
}

++$rowcount2;
}
echo "</table>";

} else {

echo "<table border=0 cellpadding=5 cellspacing=5 width=300 bgcolor=white><tr><td><b>Client ID</B></td><td><b>First Name</b></td>
<td><b>Last Name</b></td><td><b>Address</b></td><td><b>City</b></td><td><b>Zip Code</b></td><td><b>State</b></td><td><b>Email</b></td><td><b>
</b></td><td><b>Home Number</b></td><td><b>Fax Number</b></td><td><b>Party Name</b></td><td><b>Under 18 years old</b></td><td><b>Hunter</b></td><td><b>Party Contact</b></td></tr>";

$query = mysql_query("SELECT * FROM hrclienta WHERE cl_partyid = $party ");
$rowcount = 1;
while($viewclients = mysql_fetch_array($query)){

$cl_id = $viewclients['cl_id'];
$cl_firstname = $viewclients['cl_firstname'];
$cl_lastname = $viewclients['cl_lastname'];
$cl_address = $viewclients['cl_address'];
$cl_city = $viewclients['cl_city'];
$cl_zip = $viewclients['cl_zip'];
$cl_state = $viewclients['cl_state'];
$cl_email = $viewclients['cl_email'];
$cl_cell = $viewclients['cl_cell'];
$cl_home = $viewclients['cl_home'];
$cl_fax = $viewclients['cl_fax'];
$cl_partyid = $viewclients['cl_partyid'];
$cl_partyname = getPartyName($cl_partyid);



$cl_under = $viewclients['cl_under'];
$cl_hunter = $viewclients['cl_hunter'];
$cl_partycontact = $viewclients['cl_partycontact'];


if($rowcount == 1){
echo "<tr bgcolor=\"#DDD2C3\">";
}
if($rowcount == 2){
echo "<tr bgcolor=\"#DDD2C8\">";
}

++$rowcount;
if($rowcount == 3){
$rowcount = 1;
}

echo "<td>$cl_id</td><td>$cl_firstname</td><td>$cl_lastname</td><td>$cl_address</td><td>$cl_city</td><td>
$cl_zip</td><td>$cl_state</td><td>$cl_email</td><td>$cl_cell</td><td>$cl_home</td><td>$cl_fax</td><td>$cl_partyname</td></tr>";

}
echo "</table>";
}
}
?>

that is the viewClient function.. and I'm calling it with

 

 <?php viewClient('all', $_GET['sort']); ?> 

 

Link to comment
https://forums.phpfreaks.com/topic/140841-order-by-problem/#findComment-737188
Share on other sites

You have alot of duplicate code and unnecessary code.

 

<?php 

function viewClient($party, $sort = "none"){
if($party == "all"){
	$tableEcho = "<table border=0 cellpadding=2 cellspacing=5 width=%100><tr><td bgcolor=black><b><font color=white><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_id\">Client ID</a></font></B></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_firstname\">First Name</a></b></td>
	<td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_lastname\">Last Name</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_address\">Address</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_city\">City</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_zip\">Zip Code</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_state\">State</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_email\">Email</a></b></td><td><b>
	</b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_cell\">Cell Number</b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_home\">Home Number</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_fax\">Fax Number</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_partyid\">Party Name</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_under\">Under 18 years old</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_hunter\">Hunter</a></b></td><td><b><a href=\"".$_SERVER['PHP_SELF']."?sort=cl_partycontact\">Party Contact</b></td></tr>";

	if($sort != "none"){
		$query = mysql_query("SELECT * FROM hrclienta ORDER BY '$sort'");
	}else{
		$query = mysql_query("SELECT * FROM hrclienta ORDER BY cl_id");
	}
	$tr1 = "<tr bgcolor=\"#655E54\">";
	$tr2 = "<tr bgcolor=\"#C1BAB1\">";
}else {
	$tableEcho = "<table border=0 cellpadding=5 cellspacing=5 width=300 bgcolor=white><tr><td><b>Client ID</B></td><td><b>First Name</b></td>
	<td><b>Last Name</b></td><td><b>Address</b></td><td><b>City</b></td><td><b>Zip Code</b></td><td><b>State</b></td><td><b>Email</b></td><td><b>
	</b></td><td><b>Home Number</b></td><td><b>Fax Number</b></td><td><b>Party Name</b></td><td><b>Under 18 years old</b></td><td><b>Hunter</b></td><td><b>Party Contact</b></td></tr>";
	$tr1 = "<tr bgcolor=\"#DDD2C3\">";
	$tr2 = "<tr bgcolor=\"#DDD2C8\">";
	$query = mysql_query("SELECT * FROM hrclienta WHERE cl_partyid = $party ");
}
echo $tableEcho;

$rowcount = 0;
while($viewclients = mysql_fetch_array($query)){
	$cl_id = $viewclients['cl_id'];
	$cl_firstname = $viewclients['cl_firstname'];
	$cl_lastname = $viewclients['cl_lastname'];
	$cl_address = $viewclients['cl_address'];
	$cl_city = $viewclients['cl_city'];
	$cl_zip = $viewclients['cl_zip'];
	$cl_state = $viewclients['cl_state'];
	$cl_email = $viewclients['cl_email'];
	$cl_cell = $viewclients['cl_cell'];
	$cl_home = $viewclients['cl_home'];
	$cl_fax = $viewclients['cl_fax'];
	$cl_partyid = $viewclients['cl_partyid'];
	$cl_partyname = getPartyName($cl_partyid);
	$cl_under = $viewclients['cl_under'];
	$cl_hunter = $viewclients['cl_hunter'];
	$cl_partycontact = $viewclients['cl_partycontact'];
	if(($rowcount%2) == 0){
		$trEcho = $tr1;
	}else {
		$trEcho = $tr2;
	}

	++$rowcount;
	echo $trEcho . "<td>$cl_id</td><td>$cl_firstname</td><td>$cl_lastname</td><td>$cl_address</td><td>$cl_city</td><td>
	$cl_zip</td><td>$cl_state</td><td>$cl_email</td><td>$cl_cell</td><td>$cl_home</td><td>$cl_fax</td><td>$cl_partyname</td></tr>";
}
echo "</table>";
}
?>

 

Indented properly and cleaned up alot of code. See if that fixes the issue or not/

Link to comment
https://forums.phpfreaks.com/topic/140841-order-by-problem/#findComment-737197
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.