简体   繁体   English

在对象内添加标题

[英]Adding headings within an object

I currently have an array of objects that I am trying to sort in to headings, for example at the moment i have a tabular output like this: 我目前有一个对象数组,试图将它们按标题排序,例如,当我现在具有如下表格输出时:

2014-02-01 14:15:00 14:50:00 2014-02-01 14:15:00 14:50:00

2014-02-01 14:50:00 15:25:00 2014-02-01 14:50:00 15:25:00

2014-02-02 11:20:00 12:30:00 2014-02-02 11:20:00 12:30:00

2014-02-02 12:30:00 13:05:00 2014-02-02 12:30:00 13:05:00

2014-02-04 17:10:00 17:40:00 2014-02-04 17:10:00 17:40:00

2014-02-04 17:45:00 18:20:00 2014-02-04 17:45:00 18:20:00

But what I'm looking for is a little something like this: 但是我正在寻找的东西有点像这样:

1st Febuary 2月1日

2014-02-01 14:15:00 14:50:00 2014-02-01 14:15:00 14:50:00

2014-02-01 14:50:00 15:25:00 2014-02-01 14:50:00 15:25:00

2nd Febuary 2月2日

2014-02-02 11:20:00 12:30:00 2014-02-02 11:20:00 12:30:00

2014-02-02 12:30:00 13:05:00 2014-02-02 12:30:00 13:05:00

4th febuary 2月4日

2014-02-04 17:10:00 17:40:00 2014-02-04 17:10:00 17:40:00

2014-02-04 17:45:00 18:20:00 2014-02-04 17:45:00 18:20:00

I'm using laravel to retrieve these objects from the database, and each one within the array looks a little something like this: 我正在使用laravel从数据库中检索这些对象,数组中的每个对象看起来都像这样:

object(Illuminate\Database\Eloquent\Collection)#922 (1) {
  ["items":protected]=>
  array(228) {
    [0]=>
    object(Lesson)#695 (20) {
      ["attributes":protected]=>
      array(16) {
        ["id"]=>
        string(4) "7312"
        ["date"]=>
        string(10) "2014-02-01"
        ["start_time"]=>
        string(8) "14:15:00"
        ["end_time"]=>
        string(8) "14:50:00"
      }
  }
  [1]=> etc...

Does anybody know if it is possible to organise the headings this way? 有人知道是否可以通过这种方式组织标题吗? Is this a simple function i'm overlooking? 这是我忽略的简单功能吗?

As far as I'm aware there's no simple function to do what you want (kind of a group by). 据我所知,没有简单的功能可以完成您想要的工作(有点像)。 Assuming you order by date though (or somehow similar dates are grouped together in the Collection), you can use code like the following: 假设您按日期排序(或类似的日期在集合中组合在一起),则可以使用如下代码:

// controller action
public function index()
{
    // get all dates, ordered by date
    $_dates = Date::orderby('date')->get();

    $dates = [];
    $date = new Collection; // not really required, just feels nicer
    $curDate = false;

    foreach ($_dates as $_date) {
        // if we have a new date, store current collection in master array,
        // re-instantiate a blank collection, and set the current date
        if ($_date->date != $curDate) {
            if ($curDate !== false) {
                $dates[$curDate] = $date;
            }
            $date = new Collection;
            $curDate = $_date->date;
        }
        $date->push($_date);
    }

    return View::make('index', compact('dates'));
}

// view
@foreach ($dates as $date => $items)
    <h3>{{{ $date }}}</h3>
    <ul>
        @foreach ($items as $item)
            <li>{{{ $item->date }}} {{{ join(' ', $item->times) }}}</li>
        @endforeach
    </ul>
@endforeach

Obviously you could extract that group-by logic to a helper function (and make the variable names much less confusing!) 显然,您可以将按组逻辑提取到辅助函数中(并使变量名不会混淆!)

Well yes - fetch object from the db,traverse and inject it into view, something like this: 是的-从数据库获取对象,遍历并将其注入视图,如下所示:

@foreach($collection as $object)
    <ul> 
    {{$date}}//or fetch date also
    <li>{{$object[0]["record"]}}</li>
    </ul>
@enforeach

@foreach($collection as $object)
    <ul> 
    {{$date}}//or fetch date also
    <li>{{$object[1]["record"]}}</li>
    </ul>
@enforeach

Sorry for beeing generic, but collection and object will be your collection and objects names/index. 很抱歉,beeing general,但是collection和object是您的collection和对象的名称/索引。

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

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