1

I am trying to display my data in my View in this type of illustrated format. The items surrounded by the square brackets are the headers for the table.

[Lecture]

ECE 201 Section 1   ..   ..

ECE 201 Section 2   ..   ..

ECE 201 Section 3   ..   ..

[Lab]

ECE 201 Section 10   ..   ..

ECE 201 Section 11   ..   ..

ECE 201 Section 12   ..   ..

[Recitation]

ECE 201 Section 20   ..   ..

ECE 201 Section 21   ..   ..

ECE 201 Section 22   ..   ..

My JSON, well, for one class, looks like the following

   {
       year: "2015",
       term: "Summer",
       subject_code: "ECE",
       course_no: "201",
       instr_type: "Lecture",
       instr_method: "Face To Face",
       section: "003",
       crn: "42953",
       course_title: "Digital Logic",
       credits: "3.0",
       day: "R",
       time: "06:30 pm - 09:20 pm",
       instructor: "Teacher Name",
       campus: "University Building",
       max_enroll: "18",
       enroll: "18",
       building: "PLACE",
       room: null,
       description: "Introduction to logic for computing",
       pre_reqs: "",
       co_reqs: ""
   }

Essentially, I want to display the instr_type ONCE as a header of the table, and in the table, display the corresponding data. You can assume the data return is ordered by instr_type, so if you have multiple labs and lectures, it will be returned like this - lab, lab, lab, lecture, lecture, etc

I feel like what I want to do would be much easier to achieve if my data was in this format:

   {
       year: "2015",
       term: "Summer",
       subject_code: "ECE",
       course_no: "201",
       instr_type: "Lecture",
       info: [
       { 
         instr_method: "Face To Face",
         section: "003",
         crn: "42953",
         course_title: "Digital Logic",
         credits: "3.0",
         day: "R",
         time: "06:30 pm - 09:20 pm",
         instructor: "Teacher Name",
         campus: "University Building",
         max_enroll: "18",
         enroll: "18",
         building: "PLACE",
         room: null,
         description: "Introduction to logic for computing",
         pre_reqs: "",
         co_reqs: ""
       }, {
         instr_method: "Face To Face",
         section: "004",
         crn: "42954",
         ..
       }
       ]
   }

My question is how could I format my data within either my Controller or View so that I could achieve this effect? Apologies for the silly question; I feel like the answer to this question is very simple and I am just overthinking it.

1 Answer 1

3

I've found using two loops works best for this sort of thing.

// in your Controller
$classesByType = [];
foreach ($classes => $class) {
    $classesByType[$class['instr_type']][] = $class;
}
return view('my-view', ['classesByType' => $classesByType]);

// In your Blade template
@foreach ($classesByType as $type => $classes)
    <h1>{{ $type }}</h1>
    @foreach ($classes as $class)
        <p>{{ $class['subject_code'] }} {{ $class['course_code'] }} ...</p>
    @endforeach
@endforeach

If you want to categorize by additional elements, just add another loop/array level:

// in your Controller
$classesByLabelAndType = [];
foreach ($classes => $class) {
    $label = $class['subject_code'] . " " . $class['course_no'];
    $classesByLabelAndType[$label][$class['instr_type']][] = $class;
}
return view('my-view', ['classesByLabelAndType' => $classesByLabelAndType]);

// In your Blade template
@foreach ($classesByLabelAndType as $label => $classesByType)
    <h1>{{ $label }}</h1>
    @foreach ($classesByType as $type => $classes)
        <h2>{{ $type }}</h2>
        @foreach ($classes as $class)
            <p>{{ $class['course_code'] }} ...</p>
        @endforeach
    @endforeach
@endforeach
Sign up to request clarification or add additional context in comments.

10 Comments

Precisely what I wanted. Thank you =).
Glad to help! Not all of us have over a thousand reputation, so a green check mark would be appreciated! :)
I will accept in two minutes! StackOverflow limitations =)
Ah, didn't realize -- I'm new at this. Thanks!
Hey @Ben. How would I count the number of classes in total of the $classesByType array? I tried count($classesByType), but obviously, that only returns the number of instr_type
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.