Jump to content

[SOLVED] Results Sort


SpireLink

Recommended Posts

[code]as we all have saw in forums, like when you get the members list it allows you to select the way you want the data to be listed

Member Name DESC
Member Name ASC

Member ID DESC
Member ID ASC


I wanted  want to use this future in my site

the way i know to sort data is doing it in the query, like this

[code]$dinings = mysql_query("SELECT * FROM dinings ORDER BY dining_id DESC",$db);

 

I have thinked to it with functions

[/code]

function sort_name_desc

{

}

function sort_name_asc

{

}

function sort_id_desc

{

}

function sort_id_asc

{

}

[/code]

 

now even if am going to do it like this, i dont want the users to get to know what is the fucntion that I am using , or if there is a better and easier way to do it, because with functions my file will have the same code four times with differnt sort types

 

 

Link to comment
Share on other sites

because with functions my file will have the same code four times with differnt sort types

 

No need for a seperate function for each type. You simply need to make the sort by links pass a perameter via the url, then sort by that within your query. eg;

 

<?php

  if (isset($_GET['sortby'])) {
    $sortby = $_GET['sortby'];
  } else {
    $sortby = 'name'; // sort by name as default.
  }

  if (isset($_GET['sortorder'])) {
    $sortorder = $_GET['sortorder'];
  } else {
    $sortorder = 'DESC'; // order by DESC as default.
  }

  $sql = "SELECT * FROM users ORDER BY $sortby $sortorder";
  // go ahead and execute the query.

?>

 

PS: Don't forget to escape any data that may be manipulated by your users.

Link to comment
Share on other sites

because with functions my file will have the same code four times with differnt sort types

 

No need for a seperate function for each type. You simply need to make the sort by links pass a perameter via the url, then sort by that within your query. eg;

 

<?php

  if (isset($_GET['sortby'])) {
    $sortby = $_GET['sortby'];
  } else {
    $sortby = 'name'; // sort by name as default.
  }

  if (isset($_GET['sortorder'])) {
    $sortorder = $_GET['sortorder'];
  } else {
    $sortorder = 'DESC'; // order by DESC as default.
  }

  $sql = "SELECT * FROM users ORDER BY $sortby $sortorder";
  // go ahead and execute the query.

?>

 

PS: Don't forget to escape any data that may be manipulated by your users.

 

aha, you mean like tha pagenation way right ?

 

now regarding

PS: Don't forget to escape any data that may be manipulated by your users.

what do you mean by that ?? i didnt get you ..

Link to comment
Share on other sites

he meant use mysql_real_escape_string

Thanks for clarification, but at the moment I dont get data posted by users, its only the admins who will be able to post data,users section will be done after final testing of the program with the current services which doesnt include members section untile now

 

Any how, I have learned something new from you guys, thanks a bunch

 

 

Link to comment
Share on other sites

Another way is automate the sort order.

 

Store the current $sortby in a session var.

 

If the new $sortby is different from the session sortby, assume ASC by default.

 

If the new $sortby is the same as session value, toggle the sort order between ASC and DESC.

 

My biggest probleme is with sessions as am new to it, would you please explain how to do it with seasons?

Link to comment
Share on other sites

something like

 

<?php
session_start();   // required at top of any script using "$_SESSION"

if (isset($_GET['sortby'])) {
    $sortby = $_GET['sortby'];
  } else {
    $sortby = 'name'; // sort by name as default.
  }
  
$sortorder = 'ASC';   // default

if (isset($_SESSION['sortby'])) {
    if ($sortby == $_SESSION['sortby']) {
        $sortorder = ($_SESSION['sortorder'] == 'ASC') ? 'DESC' : 'ASC';
    }
}

/**
* save session vars
*/
$_SESSION['sortby'] = $sortby;
$_SESSION['sortorder'] = $sortorder;

$query = "SELECT * FROM tablename ORDER BY `$sortby` $sortorder";
?>

Link to comment
Share on other sites

because with functions my file will have the same code four times with differnt sort types

 

No need for a seperate function for each type. You simply need to make the sort by links pass a perameter via the url, then sort by that within your query. eg;

 

<?php

  if (isset($_GET['sortby'])) {
    $sortby = $_GET['sortby'];
  } else {
    $sortby = 'name'; // sort by name as default.
  }

  if (isset($_GET['sortorder'])) {
    $sortorder = $_GET['sortorder'];
  } else {
    $sortorder = 'DESC'; // order by DESC as default.
  }

  $sql = "SELECT * FROM users ORDER BY $sortby $sortorder";
  // go ahead and execute the query.

?>

 

PS: Don't forget to escape any data that may be manipulated by your users.

 

aha, you mean like tha pagenation way right ?

 

now regarding

PS: Don't forget to escape any data that may be manipulated by your users.

what do you mean by that ?? i didnt get you ..

 

Thanks, it works as I want it, now a small dout please , how can i make it toggle between ASC and DESC , like if

its the current USED link was users.php?sortby=id&sortorder= DESC the NEW link will be users.php?sortby=id&sortorder= ASC and vice versa

 

Thanks in advance

Link to comment
Share on other sites

you can do something like this :

 


if(isset($_GET['sortorder']) && $_GET['sortorder']=='ASC')
{
   echo'<a href="users.php?sortby=id&sortorder=DESC">Sort By Descending Order</a>';
}
else if(isset($_GET['sortorder']) && $_GET['sortorder']=='DESC')
{
   echo'<a href="users.php?sortby=id&sortorder=ASC">Sort By Ascending 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.