Page 1 of 1

Is there a way to sort the output on this HTML_Table?

Posted: Wed Mar 09, 2011 5:00 pm
by frank_solo
I'm trying to figure out how to sort the output the data on this HTML_Table. Can someone please help me? Thanks in advance.

Code: Select all

<?php

if ($_POST){

	$county = $_POST['county'];
	$rooms = $_POST['type'];
	$rent = $_POST['rent'];
	$rent_min = $rent - 500;
}
	
	$dbase = mysql_connect ( ' ', ' ', ' ' );
	mysql_select_db ( ' ', $dbase );
			
	if($county){
		$sql = "SELECT * FROM `apartments` WHERE `county` = '".$county."' AND `rooms` = '".$rooms."' AND `rent` < '".$rent."' AND `rent` > '".$rent_min."' ";
	}else{
		$sql = "SELECT * FROM `apartments`";
	}			
	//include the class
require_once("/home/genesis/php/HTML/Table.php");

//set table attributes
$attributes = array("width"=>"600","border"=>"1", "align"=>"center");

//create the table class
$table = new HTML_Table($attributes);

//build our first row
$contents = array("County", "Town", "Phone Number", "Rooms", "Baths", "Rent");
$attributes = array("bgcolor"=>"#336699");
$table->addRow($contents, $attributes, "TH");

//loop through and add our data
$attributes = array("bgcolor"=>"#COCOCO");

	$res = mysql_query($sql, $dbase);
	
		while($row = mysql_fetch_assoc($res)) {
        $contents = array($row['county'], $row['town'], $row['phone'], $row['rooms'], $row['bath'], $row['rent'],);
        $table->addRow($contents, $attributes);
}
$table->altRowAttributes(1, null, array("class"=>"alt"));
$table->display();
?>

Re: Is there a way to sort the output on this HTML_Table?

Posted: Wed Mar 09, 2011 6:40 pm
by califdon
Sort it on what? Sorting is done in the SQL Select statement by adding an ORDER BY clause at the end. Read this: http://www.w3schools.com/php/php_mysql_order_by.asp

Re: Is there a way to sort the output on this HTML_Table?

Posted: Wed Mar 09, 2011 7:49 pm
by frank_solo
Sorry for being so vague, but what I'm trying to do is for the user to be able to click each header and be able to sort by column.

Re: Is there a way to sort the output on this HTML_Table?

Posted: Wed Mar 09, 2011 8:27 pm
by califdon
There are many ways to do that, either by calling the script with a different url string, or using AJAX, or various other options. Take a look at http://www.htmlforums.com/databasing/t- ... 04187.html

Re: Is there a way to sort the output on this HTML_Table?

Posted: Wed Mar 09, 2011 9:33 pm
by jim.barrett
This isn't a php solution, but I've used it several times in the past and it works well and easy to implement.

http://www.kryogenix.org/code/browser/sorttable/

Re: Is there a way to sort the output on this HTML_Table?

Posted: Wed Mar 09, 2011 10:39 pm
by frank_solo
Besides sorting could I Paginate on this script?

Re: Is there a way to sort the output on this HTML_Table?

Posted: Wed Mar 09, 2011 10:44 pm
by Jonah Bron
Yes, you can. Here's a really good, easy to understand example of pagination.

viewtopic.php?f=1&t=99545&p=537019#p537019

Re: Is there a way to sort the output on this HTML_Table?

Posted: Thu Mar 10, 2011 5:21 pm
by califdon
jim.barrett wrote:This isn't a php solution, but I've used it several times in the past and it works well and easy to implement.

http://www.kryogenix.org/code/browser/sorttable/
Hey! That's very cool! Thanks, Jim, for posting that. I could have saved myself a good bit of coding a couple of years ago if I had known about that!

Re: Is there a way to sort the output on this HTML_Table?

Posted: Thu Mar 10, 2011 10:09 pm
by jim.barrett
califdon wrote:
jim.barrett wrote:This isn't a php solution, but I've used it several times in the past and it works well and easy to implement.

http://www.kryogenix.org/code/browser/sorttable/
Hey! That's very cool! Thanks, Jim, for posting that. I could have saved myself a good bit of coding a couple of years ago if I had known about that!
Yeah, it's a very neat, very simple script that has made my life easier several times hahaha.

Re: Is there a way to sort the output on this HTML_Table?

Posted: Fri Mar 11, 2011 8:38 am
by frank_solo
Yes I have used it. It is fantastic with a table, but how would I use it with pear's HTML_Table? It asks to use the table class = "sortable" HTML_Table has $Klass, not sure how to use it.