-1

I have this method in my Laravel 4 controller:

public static function getDelegateData($id, $status)
{
    $event = Event::find($id);

    foreach ($event->delegates as $delegate) {
        $delegate->status = DelegateStatus::find($delegate->pivot->delegate_status_id)->status;

        if($delegate->status == $status) {
            $total[] = $delegate->id;
        }
    }

    $data = "There are currently a total of $status delegates booked on to this event.";

    $data .= '<ul>';
        foreach ($event->delegates as $delegate) {
            $delegate->status = DelegateStatus::find($delegate->pivot->delegate_status_id)->status;

            if($delegate->status == $status) {
                $data .= '<li>';
                    $data .= $delegate->contact->first_name . ' ' . $delegate->contact->last_name;
                $data .= '</li>';
            }
        }
    $data .= '</ul>';

    return $data;
}

I'm having trouble counting (using count()) the items in the $total array. How can I do this? Thanks.

4

1 回答 1

0

好的,伙计们,我使用以下方法修复了它:

public static function getDelegateData($id, $status)
{
    $event = Event::find($id);

    $total = array();

    foreach ($event->delegates as $delegate) {
        $delegate->status = DelegateStatus::find($delegate->pivot->delegate_status_id)->status;

        if ($delegate->status == $status) {
            $total[] = $delegate->status;
        }
    }

    $total = count($total);

    $data = "There are currently a total of $total $status delegates booked on to this event.";

    $data .= '<ul>';
        foreach ($event->delegates as $delegate) {
            $delegate->status = DelegateStatus::find($delegate->pivot->delegate_status_id)->status;

            if ($delegate->status == $status) {
                $data .= '<li>';
                    $data .= $delegate->contact->first_name . ' ' . $delegate->contact->last_name;
                $data .= '</li>';
            }
        }
    $data .= '</ul>';

    return $data;
}
于 2013-05-12T21:02:43.013 回答