Sort from headers in table



I hope someone can help me.

I have a webpage that posts a list of hostnames to a php script (below) and this script works fine and displays in a table format.

My goal is to have this result table be able to be sorted from clicking on the headers of the table.

I have read many web pages on doing this with static select statements, But my code is a little different in that it runs a select statement on every hostname entered in the textarea.

Can someone point me in the right direction??? Thanks Up Front…

<?php include '/var/www/'; include '/var/www/'; //from $table_fields = split(",", $fields); //Clean up list of //hostnames coming //over from textarea $list = $_POST['hostt']; $list = str_replace("r","",$list); $list = str_replace("c","",$list); $list = ereg_replace("(n)+","n",$list); $list = trim($list); $host = explode("n",$list); //Dont allow an empty textarea if(empty($list)){ exit("Host name field cannot be empty."); } //Prints table headings and this is //what I want to click on to sort!! print ""; $table_len = sizeof($table_fields); print ""; for ($j=0;$j<$table_len;$j++) { //This creates links ok, //but when clicked I get the //error from the if empty? print ""; #print ""; } print ""; //function to time the querys $start_time = getmicrotime(); //the loop that querys for each hostname // foreach ($host as $value){ $result = mysql_query("select " . $fields . " from devregdump where machine_name like '$value%' order by machine_name", $connect) or die ("Query '$result' failed with error message: "" . mysql_error () . '"'); // tried below and get erros //$result1 = mysql_query("select " . $fields . " from devregdump where machine_name like '$value%' order by $_GET['sort']", $connect) //or die ("Query '$result' failed with error message: "" . mysql_error () . '"'); //this stores hostnames that have no entry // to print later $numrows = mysql_num_rows($result); if ($numrows == 0) { $novaluehst[] = $value; } //this prints results to table //for each hostname while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print "tn"; foreach ($line as $col_value) { print "ttn"; } print "tn"; } } print "
"; //end of time function $end_time = getmicrotime(); print "Query executed in ".(substr($end_time-$start_time,0,5))." seconds.".'

'; //To count and print if hostname is not in the db if(count($novaluehst) == 0){ exit; } elseif(count($novaluehst) == 1){ echo "". count( $novaluehst )." : Host not in Database." .'
'; } else{ echo "". count( $novaluehst )." : Hosts not in Database." .'
'; } foreach( $novaluehst as $hst ) { echo $hst .'
'; } mysql_free_result($result); mysql_close($connect); ?>



I’m using a little bit different format for SQL, but it the same any way…

Try to run your script without global variable first, also %$value% (not $value%, you should have percent sign in front too)

Here is an example of my SQL:
$sql = “select * from faq WHERE subject LIKE ‘%$sword%’ ORDER BY $sort_by”;