Jump to content

Sorting database table with links ASC & DESC


firestarter30

Recommended Posts

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? :shrug:

 

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 :)

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>

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);
?>

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. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.