Welcome Guest, Not a member yet? Register   Sign In
How to write model,view,controller when working with RDBs
#1

[eluser]foysal[/eluser]
Example: I have a teacher table and a student table. Every student is related to a teacher.so students id is kept in teacher table.(or doctor related to patient or floor related to building or pages related to book)

If I would like to get the result like below, then how should I write model, view, contoroller?

teacher_name(2)
-student_name
-student_name
teacher_name(4)
-student_name
-student_name
-student_name
-student_name
teacher_name(3)
-student_name
-student_name
-student_name
#2

[eluser]cristian.tantar[/eluser]
Hy,
tables: teachers,students,students2teachers

controller/getstudents/teacherid . method is getstudents

model:
Code:
$sql="select s.*,t.name as teacherName,t.id as teacherID from students s leftjoin students2teachers s2t on (s.id=s2t.student_id) left join teachers t on (s2t.teacher_id=t.id)"

$res=mysql_query($sql)
$class=array();
while($data=mysql_fetch_array($res)){
$class[$data['teacherName']][]=$data;
}

view
Code:
foreach($class as $teacher=>$students){
echo($teacher."<br>");
foreach($students as $student){
  echo("-".$student['studentName']."<br>");
}
should work.
#3

[eluser]InsiteFX[/eluser]
Please use the code tags when posting code!
Code:
// no spaces in within the tags!!!
[ code]
Your Code Here!
[ /code]




Theme © iAndrew 2016 - Forum software by © MyBB