HI all i have an issue echoing out my data.
I have three tables, one for students, classes and student_classes.
I’ve queried the db to joint the all three so i can see all students, whether they have enrolled in a class or not with their class. the sql is
SELECT students.id, students.student_name, students.student_number,
section_student.ss_id, section_student.section_number,sections.subject
FROM students
LEFT JOIN student_classes
ON students.student_number=student_classes.student_number
LEFT JOIN classes
ON classes.class_number=student_classes.class_number
WHERE students.status=1
This produces a list of students with classes if they have them. for those students who have enrolled in more than one course there name shows twice in the list.
When i print_r the data i get
Array
(
[0] => stdClass Object
(
[class_number] =>
[student_name] => john smith
[student_number] => 432100351
[id] => 2
[ss_id] =>
[subject] =>
[user_email] => john@email.com
[created_at] => 0000-00-00 00:00:00
[updated_at] => 0000-00-00 00:00:00
)
[1] => stdClass Object
(
[class_number] => 43223
[student_name] => Paul jones
[student_number] => 432100312
[id] => 3
[ss_id] => 40
[subject] => Maths
[user_email] => Paul@email.com
[created_at] => 0000-00-00 00:00:00
[updated_at] => 2012-11-24 08:33:23
)
[2] => stdClass Object
(
[class_number] => 21331
[student_name] => Paul jones
[student_number] => 432100312
[id] => 3
[ss_id] => 39
[subject] => Physics
[user_email] => Paul@email.com
[created_at] => 0000-00-00 00:00:00
[updated_at] => 2012-11-24 08:33:23
)
When i do a for each in a table to show the students and their class I want to loop through the classes that a student has in one line. eg
john ID:432100351 | N/A
Paul ID:432100312 | 43223 = Maths , 21331 = Physics
When i do my loops i get
ID:432100351 john | john@email.com | N/A
ID:432100312 Paul | paul@email.com | 43223 = Maths , 21331 = Physics
ID:432100312 Paul | paul@email.com | 43223 = Maths , 21331 = Physics
Im not sure if the problem is with my sql or my loop
Here is the code
@foreach ($students as $student)
<tr>
<td> {{ $key++ }}</td>
<td> {{ HTML::link('students/edit/' .$student->id, $student->student_number) }} </td>
<td> {{ HTML::link('students/edit/' .$student->id, $student->student_name) }} </td>
<td> {{ $student->user_email }} </td>
<td>
@if(!isset($student->ss_id))
{{ HTML::link('add_student_section.php?student_id='.$student->id,'Add to section'); }}
@else
@foreach($students as $section)
{{ HTML::link('edit_student_section.php?ss_id=', $section->section_number . " " . $section->subject . " ") ;}}
@endforeach
@endif
</td>
<td> {{ date("d-M-Y H:i",strtotime($student->created_at )) }} </td>
<td>
@if($student->updated_at == $student->created_at)
N/A
@else
{{ date("d-M-Y H:i",strtotime($student->updated_at )) }}
@endif
</td>
<td> {{ HTML::link('students/delete/' .$student->id, 'Delete Student') }} </td>
</tr>
@endforeach
Any help would be appreciated. Thanks
You need to use GROUP BY in conjunction with GROUP_CONCAT