简体   繁体   English

在网站上存储列排序顺序的最佳方法是什么?

[英]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/ 这是我的表格: http//jsfiddle.net/Draven/kGtx7/14/

Each header cell is clickable to be able to sort by that field and by ASC / DESC. 每个标题单元格都是可单击的,以便能够按该字段和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 users表中创建sort_fieldsort_order字段
  2. Making a new table that has their userid along with sort_field , and sort_order fields 创建一个具有userid以及sort_fieldsort_order字段的新表
  3. Or Cookies, but I assume this is the worst option 或者Cookies,但我认为这是最糟糕的选择

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.. 1)向users表中添加两个字段将使调用更容易检索这些值,但这是一个更加丑陋的方法来解决问题。

..2) Relational databases are built to be used as such. ..2)构建关系数据库以便这样使用。 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. 3)Cookies是一种非常好的解决方案。 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. 它们可以在临时情况下使用,但如果您要稍后保存信息,则可以轻松删除或甚至不启用Cookie,此时您的网站可能会遭受巨大损失。

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. 步骤1:运行查询,而不是将排序值填充到2个会话变量中。 Step 2: Do something like this code. 第2步:做这样的代码。

$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 第3步:像你真棒一样微笑

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 但是,如果您只想暂时记住它们,则可以将gets放入会话变量中

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. 实际上,使用cookie来存储用户会话ID,然后将会话数据保存在数据库,平面文件或memcached中是解决此问题的常用方法。 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. 大多数网络应用都会使用Cookie来识别您的身份。 If you delete that cookie, it forgets you and you are logged out. 如果您删除该cookie,它会忘记您并且您已注销。

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. 如果您使用的是PHP,请查看Zend Session的想法。 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 如果您必须在浏览器,PC之间共享此设置,那就太好了
    case if user delete cookies in browser. 如果用户在浏览器中删除cookie。 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. 要将排序顺序保存到其配置文件,请在表单内部创建表,并input可排序的字段名称。 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. 当他们单击其中一个字段名称(例如按位置排序)时,让表单的action运行一个PHP片段,用于更新数据库中其配置文件中的字段。

Alternately, if able to use ajax, you could simply add the database updating to an ajax call and skip the form. 或者,如果能够使用ajax,您只需将数据库更新添加到ajax调用并跳过表单即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 存储网站文章更改历史记录的最佳方法是什么? - What is the best way to store change history of website articles? 使用php存储在网站上输入的长文本的最佳方法是什么 - what is the best way to store long text entered in the website using php 网站:存储大量用户变量的最佳方法是什么? - Website: What is the best way to store a large number of user variables? 在一个mysql列中存储键值列表的最佳方法是什么 - What is the best way to store a list of key values in one mysql column 在laravel 8中为您的网站以编程方式存储和更新全局变量的最佳方法是什么? - What is the best way to store AND update global variables programatically for your website in laravel 8? 处理电子商务网站更新的最佳方法是什么? - What is the best way to handle updates to an ecommerce website? 什么是在网站链接中编码参数的最佳方法 - What is the best way to encode parameters in website link 将HTML标记存储为字符串的最佳方法是什么 - What is the best way to store HTML markup as a string 存储cookie信息的最佳方法是什么? - What is the best way to store cookie information? 在数据库中存储大量标记的最佳方法是什么? - what is the best way to store a lot of markers in database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM