firestarter30 Posted August 4, 2010 Share Posted August 4, 2010 Hello to all phpfreaks out there I need help with sorting some database columns. I ve been searching all day but i didnt found something nice to implement (or my mind is too tired to think right at this specific time) I have a table and inside it i display a picture and some other data.Above the table i inserted a div and inside it i have 5 links which when clicking on them i want to change the display order of the data being displayed in the table, and if clicked again to change again the state for ASC to DESC and so on... Is there any short solution that do the trick? With an array maybe? PS:I have also a pagination which i would like to keep the sorted order from the above table so not to mess around with the results. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/209742-sorting-database-table-with-links-asc-desc/ Share on other sites More sharing options...
jcbones Posted August 4, 2010 Share Posted August 4, 2010 Without code, it is hard to help you with the specifics. <?php if($_GET['order'] == 'd') { $orderBy = 'DESC'; $link = 'a'; } else { $orderBy = 'ASC'; $link = 'd'; } $sql .= ' ORDER BY id ' . $orderBy; echo $sql; ?> <a href="?order=<?php echo $link; ?>">Change Order</a> Quote Link to comment https://forums.phpfreaks.com/topic/209742-sorting-database-table-with-links-asc-desc/#findComment-1094925 Share on other sites More sharing options...
firestarter30 Posted August 4, 2010 Author Share Posted August 4, 2010 ok here is the code : <?php require_once('Connections/db_conn.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_db_conn, $db_conn); $query_users = "SELECT user_id, user_name, age, height, weight FROM users"; $users = mysql_query($query_users, $db_conn) or die(mysql_error()); $row_users = mysql_fetch_assoc($users); $totalRows_users = mysql_num_rows($users); ?> <!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>Users</title> </head> <body> <div id="sort">Sort Users by: <a href="users.php">Name</a> | <a href="users.php">Height</a> | <a href="users.php">Weight</a> | <a href="users.php">Age</a></div> </body> </html> <?php mysql_free_result($users); ?> Quote Link to comment https://forums.phpfreaks.com/topic/209742-sorting-database-table-with-links-asc-desc/#findComment-1095042 Share on other sites More sharing options...
firestarter30 Posted August 4, 2010 Author Share Posted August 4, 2010 Ok problem solved by following this tutorial, http://www.dougv.com/blog/2009/06/13/sorting-your-mysql-results-set-in-php-using-jquery-and-a-more-traditional-approach/ be careful to use the same "case" as coloumn names if you dont want to use numbers like this one, and to give your surfers this way, a better experience. Quote Link to comment https://forums.phpfreaks.com/topic/209742-sorting-database-table-with-links-asc-desc/#findComment-1095193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.