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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.