954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

print mysql table with client side sort function

0
By Alex Telford on Aug 4th, 2010 2:45 pm

View the full tutorial at http://www.effectivewebdesign.co.nz/tutorial.php
I have tested this many times and it works fine.
Please don't complain unless you really can't get it working, Just PM me and I'll fill in the blanks
Happy Coding :)

<?PHP
//connect info
$hostname = "localhost";
$database = "";
$username = "";
$password = "";
$conn = mysql_pconnect($hostname, $username, $password); 
$page = $_SERVER['PHP_SELF']; // Makes $page the current page e.g:query.php
//the query
if (!isset($sort)){
$sort = column1; // the one to sort by default
}
mysql_select_db($database, $conn);
$query = "SELECT * FROM table ORDER BY $sort";
$sql = mysql_query($query, $conn) or die(mysql_error());
//print the table
echo '<table><tr><td>column1</td><td>column2</td><td>column3</td></tr>';
while($row = mysql_fetch_assoc($sql)){
//echo the results
echo '<tr><td><a href="'.$page.'?sort=column1>"'.$row['column1'].'</a></td><td><a href="'.$page.'?sort=column2>"'.$row['column2'].'</a></td><td>a href="'.$page.'?sort=column3>"'.$row['column3'].'</a></td></tr>\n';
}
//close the table
echo '</table>';
?>

Don't you need a $sort=$_GET['sort']; somewhere. If that statement of code is not included $sort will never be set before the if (!isset($sort)) statement and the table will only sort based on the default value.

sudeepjd
Junior Poster
181 posts since Oct 2010
Reputation Points: 32
Solved Threads: 43
 

That depends on your server setting in your php ini
by habbit I usually forget it as All of my servers have this checked.
but if you don't then yes you should put that in

metalix
Posting Whiz in Training
218 posts since Mar 2010
Reputation Points: 13
Solved Threads: 34
 

where would you put the $sort=$_GET['sort']; in your code?

Rogueit
Newbie Poster
3 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: