[英]Sorting a NON-SQL table column in a table containing data from SQL query
Below I have stripped down my code to a simplified version. 下面,我将代码简化为简化版本。 I am storing SQL SELECT results for:
我存储的SQL SELECT结果用于:
I have appended an additional field outside the SQL SELECT called 'days_on_list'. 我在SQL SELECT之外附加了一个名为“ days_on_list”的附加字段。 This field shows the number of days since the data was added to the database, making the table output 5 columns of user data.
此字段显示自将数据添加到数据库以来的天数,使表输出5列用户数据。 ALL 5 COLUMNS ARE TO BE SORTABLE.
所有5列都将是可排序的。
I am using server-side JSON and have successfully been able to display this to the table and perform sorting on 4 of the 5 columns. 我正在使用服务器端JSON,并且已成功将其显示到表中并在5列中的4列上进行了排序。 The problem is that I am unable to sort the 'days_on_list' field as the PHP file containing the SQL code only allows me to sort the 4 fields from the select query .
问题是我无法对“ days_on_list”字段进行排序,因为包含SQL代码的PHP文件仅允许我对select查询中的4个字段进行排序 。 Is there a way I can make 'days_on_list' column be sortable in the table?
有没有一种方法可以使“ days_on_list”列在表中可排序? I know I can add this field to the sql table, but I would have to run a scheduled event on the server to update this daily (which I am not comfortable with).
我知道我可以将此字段添加到sql表中,但是我将不得不在服务器上运行一个计划的事件来每天对此进行更新(我不满意)。
Is there another way to allow for this kind of flexible table sorting? 还有另一种方法可以进行这种灵活的表排序吗?
Sorry about the question title (may be confusing), I was having trouble putting this into a question. 很抱歉问题标题(可能会引起混淆),我在将此问题中遇到麻烦。
/*SQL CODE ABOVE HERE STORES SELECT RETURNS IN $result*/ $cart = array(); $i = 0; //index the entries // get variables from sql result. if ($num_rows > 0) { //if table is populated... while ($row = mysqli_fetch_assoc($result)) { //calculate days on list by getting the number of days from //the 'date_added' to today $date1 = date_create($row['date_added']); $today = date_create(date("dmY")); $interval = date_diff($date1, $today); $doty = $interval - > format("%a"); $cart[$i] = array( "dlname" => htmlspecialchars($row['dlname']), "category" => htmlspecialchars($row['category']), "date_added" => htmlspecialchars($row['date_added']), "client" => htmlspecialchars($row['client']), "days_on_list" => $doty, //date_added to now ); $i = $i + 1; //add next row } //encoding the PHP array $json_server_pagination_data = array( "total" => intval($num_rows), "rows" => $cart, //array data ); } echo json_encode($json_server_pagination_data);
Because days_on_list
is calculated by simply comparing date_added
to the current date, sorting by days_on_list
should have exactly the reverse effect as sorting by date_added
. 因为
days_on_list
是通过简单地将date_added
与当前日期进行比较来计算的,所以按days_on_list
排序应该与按date_added
排序完全相反。
In other words, you don't actually need to sort by days_on_list
. 换句话说,您实际上不需要按
days_on_list
排序。 If the user selects days_on_list
as the sort column, just use ORDER BY date_added
(in the opposite direction ASC/DESC). 如果用户选择
days_on_list
作为排序列,则只需使用ORDER BY date_added
(与ASC / DESC相反)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.