CodeIgniter - basic question

/ Published in: PHP
Save to your folder(s)

I am having a bit of trouble figuring out what goes where in my application. I guess this is something of a fundamental lack of understanding I have, and I'm hoping somebody might be able to clear things up. This is all very much pseudo code.

In this example let's assume that I'm writing a program that keeps track of students grades, classroom attendance, etc. A school has a collection of classrooms, and a classroom has a collection of students. I'm going to create a basic controller, model, as well as another class I'll call Student.

Please note that the point of this is that I'm not sure if the Student class should be there, or if the Student model should be all I use, or if the Student class should have it's own ability to access the DB. In this simple example I'm simply looking at the student, but the same questions could apply to the Classroom, the School, etc. AGAIN, THIS IS PSEUDO CODE SO THERE ARE A FEW PLACES THAT I AM WRITING GARBAGE CODE TO ASK A QUESTION!

Copy this code and paste it in your HTML
  1. //The basic example controller. This is the basis for the different examples
  2. Report_Controller {
  4. public function list_classroom($classroomNumber){
  6. $data["classroomInfo"] = $this->classroom_model->getClassroomInfo($classroomNumber);
  8. }
  10. }
  12. Classroom_Model{
  13. //We're going to do this a few different ways!! Exciting!
  14. $this->load->model("student_model");
  15. $this->load->library("student_library");
  17. public function getClassroomInfo($classroomNumber){
  18. $classroomInfo = array();
  19. $classroomInfo["classTitle"] = $this->db->query("SELECT....")....;
  20. $classroomInfo["teacher"] = $this->db->query("SELECT...")...;
  22. //FIRST WAY
  23. $classroomInfo["students"] = $this->student_model->getStudentsInClass($classroomNumber);
  25. //SECOND WAY!!
  26. $studens = array();
  27. $studentIDs = $this->db->query("SELECT * FROM students_table WHERE classroom = $classroomNumber");
  28. foreach($studentIDs as $studentID){
  29. $students[] = new Student($studentID);
  30. }
  32. $classroomInfo["students"] = $students;
  34. return $classroomInfo;
  35. }
  37. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.