[Need Help - PHP/MySQL] - Clickable/sortable table headers
- kingdutka
- Newbie


- Joined: Mar 06, 2010
- Posts: 10
- Loc: USA
- Status: Offline
So, I followed this guide:
http://scriptplayground .com/tutorials/php/Printing-a-MySQL-table-to-a-dynamic-HTML-table-with-PHP/
And got myself a nice looking table for my database. Now I want it to be 'sortable'.
How would I make the column headings/titles 'clickable' so that the table is 'ORDER BY' that column? Also, I'd like to click a second time to change the order from ascending to descending or vice versa.
Example:
A table that has columns: Name, Age, Weight
The table lists out by 'Name', descending, by default. When I click on 'Name', the order changes from descending to ascending. When I click on 'Age', the table is re-ordered by Age, descending.
Any ideas?
http://scriptplayground .com/tutorials/php/Printing-a-MySQL-table-to-a-dynamic-HTML-table-with-PHP/
And got myself a nice looking table for my database. Now I want it to be 'sortable'.
How would I make the column headings/titles 'clickable' so that the table is 'ORDER BY' that column? Also, I'd like to click a second time to change the order from ascending to descending or vice versa.
Example:
A table that has columns: Name, Age, Weight
The table lists out by 'Name', descending, by default. When I click on 'Name', the order changes from descending to ascending. When I click on 'Age', the table is re-ordered by Age, descending.
Any ideas?
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
March 6th, 2010, 7:00 pm
- kingdutka
- Newbie


- Joined: Mar 06, 2010
- Posts: 10
- Loc: USA
- Status: Offline
How long does it usually take to get replies on this forum? I am new to OZZU, so I don't know much about it, but the forums I usually participate in only take an hour or so at most to get answers.... It's been 2 days since I started this topic... Doesn't anyone know how to do this stuff?
- SpooF
- ٩๏̯͡๏۶


- Joined: May 22, 2004
- Posts: 3415
- Loc: Richland, WA
- Status: Offline
- kingdutka
- Newbie


- Joined: Mar 06, 2010
- Posts: 10
- Loc: USA
- Status: Offline
I'll give you everything I have 
Wii.php
SQLTables.php
<?php
function display_db_query($query_string, $connection, $image, $table_params) {
$result_id = mysql_query($query_string, $connection) or die("display_db_query:" . mysql_error());
$column_count = mysql_num_fields($result_id) or die("display_db_query:" . mysql_error());
// Here the table attributes from the $table_params variable are added
print("<TABLE $table_params >\n");
// Print Headers
print("<TR>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
$field_name = mysql_field_name($result_id, $column_num);
echo ("<TH>$field_name</TH>");
}
print("</TR>\n");
// Print the body
while($row = mysql_fetch_row($result_id)) {
print("<TR ALIGN=LEFT VALIGN=TOP>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
if($column_num==0 && $image){
print("<TD><img src=imageLoader.php?image=amf_bowling</TD>\n");
}
else{
print("<TD>$row[$column_num]</TD>\n");
}
}
print("</TR>\n");
}
print("</TABLE>\n");
}
function display_db_table($tablename, $sort, $connection, $image, $table_params) {
$query_string = "SELECT * FROM $tablename ORDER BY $sort";
display_db_query($query_string, $connection, $image, $table_params);
}
?>
Wii.php
Code: [ Select ]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 .org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3 .org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Nintendo Wii Game Lists</title>
</head>
<?php
include("./SQLTables.php");
?>
<body>
<div align="center">
<h1>Nintendo Wii Games Lists</h1>
</div>
<div align="center">
<h2>Wish List</h2>
<p><?php
//Connection
$global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
$database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
$table = "wish_list";
$sort = "Name";
display_db_table($table, $sort, $global_dbh, FALSE, "border='5'");
?></p>
<div align="center">
<h2>Owned List</h2>
<p><?php
//Connection
$global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
$database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
$table = "owned_list";
$sort = "Name";
display_db_table($table, $sort, $global_dbh, FALSE, "border='5'");
?></p>
</div>
</body>
</html>
<html xmlns="http://www.w3 .org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Nintendo Wii Game Lists</title>
</head>
<?php
include("./SQLTables.php");
?>
<body>
<div align="center">
<h1>Nintendo Wii Games Lists</h1>
</div>
<div align="center">
<h2>Wish List</h2>
<p><?php
//Connection
$global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
$database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
$table = "wish_list";
$sort = "Name";
display_db_table($table, $sort, $global_dbh, FALSE, "border='5'");
?></p>
<div align="center">
<h2>Owned List</h2>
<p><?php
//Connection
$global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
$database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
$table = "owned_list";
$sort = "Name";
display_db_table($table, $sort, $global_dbh, FALSE, "border='5'");
?></p>
</div>
</body>
</html>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 .org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3 .org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Nintendo Wii Game Lists</title>
- </head>
- <?php
- include("./SQLTables.php");
- ?>
- <body>
- <div align="center">
- <h1>Nintendo Wii Games Lists</h1>
- </div>
- <div align="center">
- <h2>Wish List</h2>
- <p><?php
- //Connection
- $global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
- $database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
- $table = "wish_list";
- $sort = "Name";
- display_db_table($table, $sort, $global_dbh, FALSE, "border='5'");
- ?></p>
- <div align="center">
- <h2>Owned List</h2>
- <p><?php
- //Connection
- $global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
- $database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
- $table = "owned_list";
- $sort = "Name";
- display_db_table($table, $sort, $global_dbh, FALSE, "border='5'");
- ?></p>
- </div>
- </body>
- </html>
SQLTables.php
Code: [ Select ]
<?php
function display_db_query($query_string, $connection, $image, $table_params) {
$result_id = mysql_query($query_string, $connection) or die("display_db_query:" . mysql_error());
$column_count = mysql_num_fields($result_id) or die("display_db_query:" . mysql_error());
// Here the table attributes from the $table_params variable are added
print("<TABLE $table_params >\n");
// Print Headers
print("<TR>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
$field_name = mysql_field_name($result_id, $column_num);
echo ("<TH>$field_name</TH>");
}
print("</TR>\n");
// Print the body
while($row = mysql_fetch_row($result_id)) {
print("<TR ALIGN=LEFT VALIGN=TOP>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
if($column_num==0 && $image){
print("<TD><img src=imageLoader.php?image=amf_bowling</TD>\n");
}
else{
print("<TD>$row[$column_num]</TD>\n");
}
}
print("</TR>\n");
}
print("</TABLE>\n");
}
function display_db_table($tablename, $sort, $connection, $image, $table_params) {
$query_string = "SELECT * FROM $tablename ORDER BY $sort";
display_db_query($query_string, $connection, $image, $table_params);
}
?>
- <?php
- function display_db_query($query_string, $connection, $image, $table_params) {
- $result_id = mysql_query($query_string, $connection) or die("display_db_query:" . mysql_error());
- $column_count = mysql_num_fields($result_id) or die("display_db_query:" . mysql_error());
- // Here the table attributes from the $table_params variable are added
- print("<TABLE $table_params >\n");
- // Print Headers
- print("<TR>");
- for($column_num = 0; $column_num < $column_count; $column_num++) {
- $field_name = mysql_field_name($result_id, $column_num);
- echo ("<TH>$field_name</TH>");
- }
- print("</TR>\n");
- // Print the body
- while($row = mysql_fetch_row($result_id)) {
- print("<TR ALIGN=LEFT VALIGN=TOP>");
- for($column_num = 0; $column_num < $column_count; $column_num++) {
- if($column_num==0 && $image){
- print("<TD><img src=imageLoader.php?image=amf_bowling</TD>\n");
- }
- else{
- print("<TD>$row[$column_num]</TD>\n");
- }
- }
- print("</TR>\n");
- }
- print("</TABLE>\n");
- }
- function display_db_table($tablename, $sort, $connection, $image, $table_params) {
- $query_string = "SELECT * FROM $tablename ORDER BY $sort";
- display_db_query($query_string, $connection, $image, $table_params);
- }
- ?>
- SpooF
- ٩๏̯͡๏۶


- Joined: May 22, 2004
- Posts: 3415
- Loc: Richland, WA
- Status: Offline
What your doing is a little complicated since its completely dynamic.
Basically what your going to what to do is for each field you will want to make the name a link and send what field you want to sort by and attached the direction (asc,desc)
Your $sort will change depending on whats currently set.
Basically what your going to what to do is for each field you will want to make the name a link and send what field you want to sort by and attached the direction (asc,desc)
Code: [ Select ]
for($column_num = 0; $column_num < $column_count; $column_num++) {
$field_name = mysql_field_name($result_id, $column_num);
echo ("<TH><a href="page.php?order_by=$field_name&sorting=$sort</TH>");
}
$field_name = mysql_field_name($result_id, $column_num);
echo ("<TH><a href="page.php?order_by=$field_name&sorting=$sort</TH>");
}
- for($column_num = 0; $column_num < $column_count; $column_num++) {
- $field_name = mysql_field_name($result_id, $column_num);
- echo ("<TH><a href="page.php?order_by=$field_name&sorting=$sort</TH>");
- }
Your $sort will change depending on whats currently set.
Code: [ Select ]
$order_by = (isset($_GET['order_by'])) ? $_GET['order_by'] : 'Name';
$sorting = (isset($_GET['sorting'])) ? $_GET['sorting'] : 'desc';
$sorting = (isset($_GET['sorting'])) ? $_GET['sorting'] : 'desc';
- $order_by = (isset($_GET['order_by'])) ? $_GET['order_by'] : 'Name';
- $sorting = (isset($_GET['sorting'])) ? $_GET['sorting'] : 'desc';
Code: [ Select ]
switch($sorting) {
case "asc":
$sort = 'desc';
break;
case "desc":
$sort = 'asc';
break;
}
case "asc":
$sort = 'desc';
break;
case "desc":
$sort = 'asc';
break;
}
- switch($sorting) {
- case "asc":
- $sort = 'desc';
- break;
- case "desc":
- $sort = 'asc';
- break;
- }
#define NULL (::rand() % 2)
- kingdutka
- Newbie


- Joined: Mar 06, 2010
- Posts: 10
- Loc: USA
- Status: Offline
Seriously... THANKS!!!
Wii.php
SQLTables.php
I'm a BIG fan of dynamic code
This all works fine and good, but... I have 2 tables on the page and it is reloading both of them using the 'click' from one table. So, if I click on the 'Name' column in the wish list, both the wish list and the owned list are getting organized by name. I can almost live with that, but if you click the 'price' column, the 'owned list' table disappears because I don't have a 'price' column in the 'owned_list' database table, so that one gives me an error.
Not really sure how I can get around this one... I only want the one table to get reloaded when the heading links are clicked...
Wii.php
Code: [ Select ]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 .org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3 .org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Nintendo Wii Game Lists</title>
</head>
<?php
include("./SQLTables.php");
?>
<body>
<div align="center">
<h1>Nintendo Wii Games Lists</h1>
</div>
<div align="center">
<h2>Wish List</h2>
<p><?php
//Connection
$global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
$database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
$table = "wish_list";
$page = "Wii";
display_db_table($page, $table, $global_dbh, FALSE, "border='5'");
?></p>
<div align="center">
<h2>Owned List</h2>
<p><?php
//Connection
$global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
$database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
$table = "owned_list";
$page = "Wii";
display_db_table($page, $table, $global_dbh, FALSE, "border='5'");
?></p>
</div>
</body>
</html>
<html xmlns="http://www.w3 .org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Nintendo Wii Game Lists</title>
</head>
<?php
include("./SQLTables.php");
?>
<body>
<div align="center">
<h1>Nintendo Wii Games Lists</h1>
</div>
<div align="center">
<h2>Wish List</h2>
<p><?php
//Connection
$global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
$database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
$table = "wish_list";
$page = "Wii";
display_db_table($page, $table, $global_dbh, FALSE, "border='5'");
?></p>
<div align="center">
<h2>Owned List</h2>
<p><?php
//Connection
$global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
$database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
$table = "owned_list";
$page = "Wii";
display_db_table($page, $table, $global_dbh, FALSE, "border='5'");
?></p>
</div>
</body>
</html>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 .org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3 .org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Nintendo Wii Game Lists</title>
- </head>
- <?php
- include("./SQLTables.php");
- ?>
- <body>
- <div align="center">
- <h1>Nintendo Wii Games Lists</h1>
- </div>
- <div align="center">
- <h2>Wish List</h2>
- <p><?php
- //Connection
- $global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
- $database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
- $table = "wish_list";
- $page = "Wii";
- display_db_table($page, $table, $global_dbh, FALSE, "border='5'");
- ?></p>
- <div align="center">
- <h2>Owned List</h2>
- <p><?php
- //Connection
- $global_dbh = mysql_connect('localhost','root','mangos') or die("Unable to connect: " . mysql_error() . "<br>");
- $database = mysql_select_db("wii game list") or die( "Unable to select database<br>");
- $table = "owned_list";
- $page = "Wii";
- display_db_table($page, $table, $global_dbh, FALSE, "border='5'");
- ?></p>
- </div>
- </body>
- </html>
SQLTables.php
Code: [ Select ]
<?php
function display_db_query($page, $query_string, $connection, $sort, $image, $table_params) {
$result_id = mysql_query($query_string, $connection) or die("display_db_query:" . mysql_error());
$column_count = mysql_num_fields($result_id) or die("display_db_query:" . mysql_error());
// Here the table attributes from the $table_params variable are added
print("<TABLE $table_params >\n");
// Print Headers
print("<TR>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
$field_name = mysql_field_name($result_id, $column_num);
echo ("<TH><a href=\"$page.php?order_by=$field_name&sorting=$sort\">$field_name</a></TH>");
}
print("</TR>\n");
// Print the body
while($row = mysql_fetch_row($result_id)) {
print("<TR ALIGN=LEFT VALIGN=TOP>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
if($column_num==0 && $image){
print("<TD><img src=imageLoader.php?image=amf_bowling</TD>\n");
}
else{
print("<TD>$row[$column_num]</TD>\n");
}
}
print("</TR>\n");
}
print("</TABLE>\n");
}
function display_db_table($page, $tablename, $connection, $image, $table_params) {
$order_by = (isset($_GET['order_by'])) ? $_GET['order_by'] : 'Name';
$sorting = (isset($_GET['sorting'])) ? $_GET['sorting'] : 'desc';
switch($sorting){
case "asc":
$sort = 'desc';
break;
case "desc":
$sort = 'asc';
break;
}
$query_string = "SELECT * FROM $tablename ORDER BY $order_by"." $sort";
display_db_query($page, $query_string, $connection, $sort, $image, $table_params);
}
?>
function display_db_query($page, $query_string, $connection, $sort, $image, $table_params) {
$result_id = mysql_query($query_string, $connection) or die("display_db_query:" . mysql_error());
$column_count = mysql_num_fields($result_id) or die("display_db_query:" . mysql_error());
// Here the table attributes from the $table_params variable are added
print("<TABLE $table_params >\n");
// Print Headers
print("<TR>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
$field_name = mysql_field_name($result_id, $column_num);
echo ("<TH><a href=\"$page.php?order_by=$field_name&sorting=$sort\">$field_name</a></TH>");
}
print("</TR>\n");
// Print the body
while($row = mysql_fetch_row($result_id)) {
print("<TR ALIGN=LEFT VALIGN=TOP>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
if($column_num==0 && $image){
print("<TD><img src=imageLoader.php?image=amf_bowling</TD>\n");
}
else{
print("<TD>$row[$column_num]</TD>\n");
}
}
print("</TR>\n");
}
print("</TABLE>\n");
}
function display_db_table($page, $tablename, $connection, $image, $table_params) {
$order_by = (isset($_GET['order_by'])) ? $_GET['order_by'] : 'Name';
$sorting = (isset($_GET['sorting'])) ? $_GET['sorting'] : 'desc';
switch($sorting){
case "asc":
$sort = 'desc';
break;
case "desc":
$sort = 'asc';
break;
}
$query_string = "SELECT * FROM $tablename ORDER BY $order_by"." $sort";
display_db_query($page, $query_string, $connection, $sort, $image, $table_params);
}
?>
- <?php
- function display_db_query($page, $query_string, $connection, $sort, $image, $table_params) {
- $result_id = mysql_query($query_string, $connection) or die("display_db_query:" . mysql_error());
- $column_count = mysql_num_fields($result_id) or die("display_db_query:" . mysql_error());
- // Here the table attributes from the $table_params variable are added
- print("<TABLE $table_params >\n");
- // Print Headers
- print("<TR>");
- for($column_num = 0; $column_num < $column_count; $column_num++) {
- $field_name = mysql_field_name($result_id, $column_num);
- echo ("<TH><a href=\"$page.php?order_by=$field_name&sorting=$sort\">$field_name</a></TH>");
- }
- print("</TR>\n");
- // Print the body
- while($row = mysql_fetch_row($result_id)) {
- print("<TR ALIGN=LEFT VALIGN=TOP>");
- for($column_num = 0; $column_num < $column_count; $column_num++) {
- if($column_num==0 && $image){
- print("<TD><img src=imageLoader.php?image=amf_bowling</TD>\n");
- }
- else{
- print("<TD>$row[$column_num]</TD>\n");
- }
- }
- print("</TR>\n");
- }
- print("</TABLE>\n");
- }
- function display_db_table($page, $tablename, $connection, $image, $table_params) {
- $order_by = (isset($_GET['order_by'])) ? $_GET['order_by'] : 'Name';
- $sorting = (isset($_GET['sorting'])) ? $_GET['sorting'] : 'desc';
- switch($sorting){
- case "asc":
- $sort = 'desc';
- break;
- case "desc":
- $sort = 'asc';
- break;
- }
- $query_string = "SELECT * FROM $tablename ORDER BY $order_by"." $sort";
- display_db_query($page, $query_string, $connection, $sort, $image, $table_params);
- }
- ?>
I'm a BIG fan of dynamic code
This all works fine and good, but... I have 2 tables on the page and it is reloading both of them using the 'click' from one table. So, if I click on the 'Name' column in the wish list, both the wish list and the owned list are getting organized by name. I can almost live with that, but if you click the 'price' column, the 'owned list' table disappears because I don't have a 'price' column in the 'owned_list' database table, so that one gives me an error.
Not really sure how I can get around this one... I only want the one table to get reloaded when the heading links are clicked...
- SpooF
- ٩๏̯͡๏۶


- Joined: May 22, 2004
- Posts: 3415
- Loc: Richland, WA
- Status: Offline
- kingdutka
- Newbie


- Joined: Mar 06, 2010
- Posts: 10
- Loc: USA
- Status: Offline
SpooF wrote:
You can use a different variable name in the url to differ from each table.
Ya, I figured that much, but.., wouldn't I need to track all the tables on the page and make sure each one is listed out by the last heading that was clicked? I'd need this to be dynamic and expandable to %i number of tables on a single page...
- SpooF
- ٩๏̯͡๏۶


- Joined: May 22, 2004
- Posts: 3415
- Loc: Richland, WA
- Status: Offline
heres an example of using sessions to store your data for each table.
This requires that your links give a table name, field for sorting and how you want it to be sorted.
PHP Code: [ Select ]
<?
// begin the session
session_start();
$table_name = (isset($_GET['table'])) ? $_GET['table'] : NULL;
$order_by = (isset($_GET['order_by'])) ? $_GET['order_by'] : 'id';
$sorting = (isset($_GET['sorting'])) ? $_GET['sorting'] : 'desc';
// Check to see if we have some data saved in our session
if(isset($_SESSION['table_info']))
{
// Get data in session
$table_info = $_SESSION['table_info'];
}
else
{
// There's no data in the session so lets initialize an array
$table_info = array();
}
// If there is no table supplied we can't do much
if($table_name != NULL)
{
// Check if the table already has data in our session
if(isset($table_info[$table_name]))
{
// Get table data from session
$table = $table_info[$table_name];
}
else
{
// There's no data for the table so lets initialize an array for the table
$table = array();
}
// Update table data (these should always be given in the url so we wont worry about not updating them if they are not set)
$table['order_by'] = $order_by;
$table['sorting'] = $sorting;
// Put the table back into our array
$table_info[$table_name] = $table;
}
// Update session with new data
$_SESSION['table_info'] = $table_info;
echo '<pre>';
print_r($table_info);
echo '</pre>';
?>
// begin the session
session_start();
$table_name = (isset($_GET['table'])) ? $_GET['table'] : NULL;
$order_by = (isset($_GET['order_by'])) ? $_GET['order_by'] : 'id';
$sorting = (isset($_GET['sorting'])) ? $_GET['sorting'] : 'desc';
// Check to see if we have some data saved in our session
if(isset($_SESSION['table_info']))
{
// Get data in session
$table_info = $_SESSION['table_info'];
}
else
{
// There's no data in the session so lets initialize an array
$table_info = array();
}
// If there is no table supplied we can't do much
if($table_name != NULL)
{
// Check if the table already has data in our session
if(isset($table_info[$table_name]))
{
// Get table data from session
$table = $table_info[$table_name];
}
else
{
// There's no data for the table so lets initialize an array for the table
$table = array();
}
// Update table data (these should always be given in the url so we wont worry about not updating them if they are not set)
$table['order_by'] = $order_by;
$table['sorting'] = $sorting;
// Put the table back into our array
$table_info[$table_name] = $table;
}
// Update session with new data
$_SESSION['table_info'] = $table_info;
echo '<pre>';
print_r($table_info);
echo '</pre>';
?>
- <?
- // begin the session
- session_start();
- $table_name = (isset($_GET['table'])) ? $_GET['table'] : NULL;
- $order_by = (isset($_GET['order_by'])) ? $_GET['order_by'] : 'id';
- $sorting = (isset($_GET['sorting'])) ? $_GET['sorting'] : 'desc';
- // Check to see if we have some data saved in our session
- if(isset($_SESSION['table_info']))
- {
- // Get data in session
- $table_info = $_SESSION['table_info'];
- }
- else
- {
- // There's no data in the session so lets initialize an array
- $table_info = array();
- }
- // If there is no table supplied we can't do much
- if($table_name != NULL)
- {
- // Check if the table already has data in our session
- if(isset($table_info[$table_name]))
- {
- // Get table data from session
- $table = $table_info[$table_name];
- }
- else
- {
- // There's no data for the table so lets initialize an array for the table
- $table = array();
- }
- // Update table data (these should always be given in the url so we wont worry about not updating them if they are not set)
- $table['order_by'] = $order_by;
- $table['sorting'] = $sorting;
- // Put the table back into our array
- $table_info[$table_name] = $table;
- }
- // Update session with new data
- $_SESSION['table_info'] = $table_info;
- echo '<pre>';
- print_r($table_info);
- echo '</pre>';
- ?>
This requires that your links give a table name, field for sorting and how you want it to be sorted.
#define NULL (::rand() % 2)
- SpooF
- ٩๏̯͡๏۶


- Joined: May 22, 2004
- Posts: 3415
- Loc: Richland, WA
- Status: Offline
Heres some out put from a few tests:
Code: [ Select ]
// Link: http://192.168.1.200/table_session.php?table=Data&order_by=Field1&sorting=desc
// Output
Array
(
[Data] => Array
(
[order_by] => Field1
[sorting] => desc
)
)
// Link: http://192.168.1.200/table_session.php?table=Data&order_by=Field2&sorting=desc
// Output
Array
(
[Data] => Array
(
[order_by] => Field2
[sorting] => desc
)
)
// Link: http://192.168.1.200/table_session.php?table=Users&order_by=Field1&sorting=asc
// Output
Array
(
[Data] => Array
(
[order_by] => Field2
[sorting] => desc
)
[Users] => Array
(
[order_by] => Field1
[sorting] => asc
)
)
// Output
Array
(
[Data] => Array
(
[order_by] => Field1
[sorting] => desc
)
)
// Link: http://192.168.1.200/table_session.php?table=Data&order_by=Field2&sorting=desc
// Output
Array
(
[Data] => Array
(
[order_by] => Field2
[sorting] => desc
)
)
// Link: http://192.168.1.200/table_session.php?table=Users&order_by=Field1&sorting=asc
// Output
Array
(
[Data] => Array
(
[order_by] => Field2
[sorting] => desc
)
[Users] => Array
(
[order_by] => Field1
[sorting] => asc
)
)
- // Link: http://192.168.1.200/table_session.php?table=Data&order_by=Field1&sorting=desc
- // Output
- Array
- (
- [Data] => Array
- (
- [order_by] => Field1
- [sorting] => desc
- )
- )
- // Link: http://192.168.1.200/table_session.php?table=Data&order_by=Field2&sorting=desc
- // Output
- Array
- (
- [Data] => Array
- (
- [order_by] => Field2
- [sorting] => desc
- )
- )
- // Link: http://192.168.1.200/table_session.php?table=Users&order_by=Field1&sorting=asc
- // Output
- Array
- (
- [Data] => Array
- (
- [order_by] => Field2
- [sorting] => desc
- )
- [Users] => Array
- (
- [order_by] => Field1
- [sorting] => asc
- )
- )
#define NULL (::rand() % 2)
- kingdutka
- Newbie


- Joined: Mar 06, 2010
- Posts: 10
- Loc: USA
- Status: Offline
Holy crap... LOL! I just started learning PHP this past Sunday and I am very busy with work atm, so it will take me a little while to digest all this and get something setup.
Thanks a ton for all the help so far! Now my family can bask in my geekness and maybe buy me a Wii game while they're at it!
Thanks a ton for all the help so far! Now my family can bask in my geekness and maybe buy me a Wii game while they're at it!
- kingdutka
- Newbie


- Joined: Mar 06, 2010
- Posts: 10
- Loc: USA
- Status: Offline
So I've got everything working, but now I need to add in a "secondary sort". I understand how it works in SQL, but not in PHP. I'm looking at one line of code in particular:
What would I add to the PHP URL to give a secondary sort like you see in example #4 here:
techonthenet . com/sql/order_by.php
Code: [ Select ]
echo ("<TH><a href=\"$page.php?order_by=$field_name&sorting=$sort&tablename=$tablename\">$field_name</a></TH>");
What would I add to the PHP URL to give a secondary sort like you see in example #4 here:
techonthenet . com/sql/order_by.php
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 13 posts
- Users browsing this forum: No registered users and 250 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum

