Jump to content

[SOLVED] How can I tell which way my database is sorting?


piznac

Recommended Posts

OK I did some searching on this and between being lazy and not really seeing an answer here I am ;D

 

Ive got some mysql results,.. and I have wrote a simple script to sort them by column headers. Little link and url var to tell the sql which way to sort. My problem is how to I go about assigning ASC or DESC? Now that seemed real simple to me,. but then the question came up. How is it sorted in the first place?

 

If I could find out how it is sorted then it would be fairly simple to do an if statement to change the var in the ORDER BY clause. Anyone know how to tell which way the results are sorted to begin with?

Link to comment
Share on other sites

Anyone know how to tell which way the results are sorted to begin with?

 

Sorry, but your question is a little unclear. The results are not sorted to begin with. You can see how your results will come out if not run with an ORDER BY statement by simply using...

 

SELECT fld FROM tbl;

 

Then... to sort by a specific field use....

 

SELECT fld FROM tbl ORDER BY fld DESC;

Link to comment
Share on other sites

Ok yes,. I have that. Now what if I wanted to also sort by ASC or DESC. Like if it is sorted by column A upon clicking the link by that column and by ASC. Then when clicking the link again it would be sorted by the column and by DESC.

 

<?php

  if (isset($_GET['order'])) {
    $order = mysql_real_escape_string($_GET['order']);
    $sort = mysql_real_escape_string($_GET['sort']);
    $sql = "SELECT fld FROM tbl ORDER BY $order $sort";
  }

?>

 

Sort being either ASC or DESC depending on how many times the link is clicked or the sort in the first place. I hope this makes sense and sorry if Im over confusing it.

Link to comment
Share on other sites

Something like?

 

<?php

  if (isset($_GET['order'])) {
    $order = mysql_real_escape_string($_GET['order']);
    $sql = "SELECT fld FROM tbl ORDER BY $order $sort";

    if ($order == 'desc') {
      $order = 'asc';
    } else {
      $order = 'desc';
    }
    echo "<a href='?order=$order'>sort $order</a>";

  }

?>

Link to comment
Share on other sites

Ok now Im really confused. How would I define my variable then? Like $sort which would come from the link:

 

<a href=\"$_SERVER[php_SELF]?order=or_date&n=$SESSION[MM_Username]&$sort=ASC\">Order Date$pos</a>

 

So like this the results will always be DESC because the $sort var is always ASC. See what Im saying?

Link to comment
Share on other sites

How would I define my variable then?

 

Dont hard code it into the link. You can actually run this script, it will echo your query so you can see whats going on.

 

<?php

  if (isset($_GET['order'])) {
    $order = mysql_real_escape_string($_GET['order']);
  } else {
    $order = 'desc'; // set a default.
  }

  $sql = "SELECT fld FROM tbl ORDER BY $order";
  // just here for debugging, remove later.
  echo $sql."<br />";

  // run your query here.

  // form your link.
  if ($order == 'desc') {
    $order = 'asc';
  } else {
    $order = 'desc';
  }
  echo "<a href='?order=$order'>sort $order</a>";

?>

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.