简体   繁体   中英

What is the best way to store a column sort order on website?

Here is what my table looks like: http://jsfiddle.net/Draven/kGtx7/14/

Each header cell is clickable to be able to sort by that field and by ASC / DESC. I want to be able to store that sorting option somewhere, so it get's remembered, but am not sure the best way to do that.

Here are the options I was thinking...

  1. Making sort_field and sort_order fields in the users table
  2. Making a new table that has their userid along with sort_field , and sort_order fields
  3. Or Cookies, but I assume this is the worst option

This is left up to interpretation, but each case would have its own uses.

1) Adding two fields to your users table will make the calls to retrieve these values easier but it is a much uglier approach to the problem than..

..2) Relational databases are built to be used as such. I'm not sure in terms of performance, but I do know that using the power of relational databases can make your db easier to navigate and understand/manipulate. While you may need some more complex calls (ie joins and whatnot), I believe the tradeoff is worth it.

and 3) Cookies are a very meh solution. They could be used in temporary cases, but if you are trying to save info for later, cookies can easily be deleted or not even enabled, at which point your site can suffer drastically.

I don't think we are clear on what you want, but I think you are wanting something like this:

Step 1: Run a query than will populate the sorting values into 2 session variables. Step 2: Do something like this code.

$sortHeadClicked = $_SESSION['headClicked']
$sortReturnDirection = $_SESSION['returnDirection']

//TODO: validate data before query

if ($result = $mysqli->query("SELECT * FROM table ORDER BY $sortHeadClicked $sortReturnDirection)) 
{
    //TODO: Get results
}

Step 3: Smile like your awesome

Alternatively, you could use some sort of sub select query.

NOTE: This is the simplistic logic so they won't be remember with this example. However, you can put the gets into session variables if you only want them to be remember temporarily

However, if you want these remembered permanently, you need two columns in your user's table where you would either puts them into sessions or use a subselect query

Actually, using a cookie to store the user session id and then keeping session data in a database, flat file, or memcached is pretty common way to solve this. It would help to set up a reusable mechanism for this, like registry of sorts, that you can retrieve per user values at will. This only works if you have a user login of course. Otherwise there is no point in storing that data, as the users identity will be lost once they end the session (close the browser window). Most web apps will use cookies to identify you. If you delete that cookie, it forgets you and you are logged out.

Your first solution will suffer if you ever want to add another per user "preference" as you'd need to modify the underlying table.

Have a look at Zend Session for ideas if you are using PHP. If not the concepts still apply.

I'm afraid that depends on your needs. How I see this problem:

  1. good if you must share this settings between browsers, PCs, in
    case if user delete cookies in browser. But it is not flexible - if you will need to add another table, you will also add two additional fields
  2. the same as 1 in term of how it is shared between computers, browsers etc, but it is more flexible. You may add a column with table name easily.
  3. If this setting is not so important and you may allow to loose setting in some cases. This solution is simplest, but it may not work for you

To save the sorting order to their profile, make the table inside a form and have the sortable field names be input s. When they click one of the field names (sort by Location, for example), have the form's action run a PHP snip that updates a field in their profile on the database.

Alternately, if able to use ajax, you could simply add the database updating to an ajax call and skip the form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM