print mysql table with client side sort function

metalix 0 Tallied Votes 910 Views Share

View the full tutorial at
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>';
?>

Dani AI

Generated

A few concrete fixes and safety notes that will clear up the problems raised by , and and make the table sortable on the client or server side.

First, fix the broken anchor markup in the row output (missing quote/angle and closing </a> will stop the links). Also explicitly read the incoming sort parameter early (do not rely on old register_globals behavior), validate it against a whitelist of allowed column names, then build the ORDER BY from that validated value. Example whitelist pattern:

$allowed = ['column1','column2','column3'];
$sort = 'column1';
$requested = filter_input(INPUT_GET, 'sort', FILTER_SANITIZE_STRING);
if ($requested !== null && in_array($requested, $allowed, true)) {
    $sort = $requested;
}

Why that matters: ORDER BY cannot be parameterized, so using a whitelist prevents SQL injection. Always escape HTML output from the database when printing cells and escape the script name used in links. A simple safe-link pattern looks like:

$script = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8');
$cell   = htmlspecialchars($row['column1'], ENT_QUOTES, 'UTF-8');
echo "<td><a href=\"{$script}?sort=column1\">{$cell}</a></td>";

If you want client-side sorting instead of server-side: output a semantic table with <thead> and <tbody>, mark numeric cells or dates with a data-type or data-value attribute, and use a small JS sorter (or a plugin) to provide instant sorting. Keep server-side sorting as a fallback for very large result sets and for accessibility/SEO. Finally, move away from old mysql_* functions — use mysqli or PDO, set the connection charset (utf8mb4), and avoid persistent connections unless you understand their implications.

sudeepjd 19 Junior Poster

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.

metalix 0 Posting Whiz in Training

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

Rogueit 0 Newbie Poster

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.