简体   繁体   English

按Drupal 7模块中的内容排序

[英]Order by content in Drupal 7 module

I have 3 variable in my custom module: 我的自定义模块中有3个变量:

  $day = date('d',strtotime($nd->field_edate['und'][0]['value'],time()));

  $dayname = date('w',strtotime($nd->field_edate['und'][0]['value'],time()));

  $hours = date('H:i',strtotime($nd->field_edate['und'][0]['value'],time()));

And my content order by DESC. 而且我的内容由DESC订购。

$rs = $data->orderBy("n.created","desc")->execute();

But I want, it uses order by $day , $dayname or $hours . 但是我想要,它使用$day$dayname$hours $dayname

When I tried to like this: 当我试图喜欢这样的时候:

$rs = $data->orderBy("n.created","'$hours.'")->execute();

My pseudo code I want: 我想要的伪代码:

$rs = $data->orderBy("order by near date today")->execute();

It's not working. 没用

How can I fix it? 我该如何解决?

You can only order by database fields, not arbitrary PHP variables. 您只能按数据库字段排序,不能按任意PHP变量排序。

If you want to order by the field edate first you will need to join to that table. 如果要先按字段eded订购,则需要加入该表。

For this example I am assuming that edate is a field attached to a node of type article. 对于此示例,我假设edate是附加到article类型的节点的字段。

$data->join('field_data_field_edate', 'e', "e.entity_id = n.nid AND e.bundle = 'article'");

Now you can use: 现在您可以使用:

$data->orderBy("e.field_edate_value", "ASC");

This will order by whatever value is in the edate field. 这将按edate字段中的任何值排序。 If that is a timestamp, and you want to order by the dayname then you could use: 如果那是一个时间戳记,并且您想按日期名称排序,则可以使用:

$data->addExpression("DAYNAME(FROM_UNIXTIME(e.field_edate_value))", 'day_name');

And then you can order by your expression: 然后,您可以通过表达式进行排序:

$data->orderBy("day_name", "ASC");

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

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