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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frank_solo
Forum Newbie
Posts: 5
Joined: Wed Mar 09, 2011 4:56 pm

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

Post 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();
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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
frank_solo
Forum Newbie
Posts: 5
Joined: Wed Mar 09, 2011 4:56 pm

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

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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
jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

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

Post 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/
frank_solo
Forum Newbie
Posts: 5
Joined: Wed Mar 09, 2011 4:56 pm

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

Post by frank_solo »

Besides sorting could I Paginate on this script?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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!
jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

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

Post 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.
frank_solo
Forum Newbie
Posts: 5
Joined: Wed Mar 09, 2011 4:56 pm

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

Post 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.
Post Reply