Help me assemble an array as a result of a foreach with php

Asked

Viewed 124 times

1

I created a list of students who have their birthday this month, follow the coding: In the coding below the code is searching directly from the database of the student table the students of all the classes that make birthday in the current month. Class_id shows in the table the student’s class and Section_id shows the student’s class according to the foreach.

<?php
 $where = "MONTH(STR_TO_DATE(birthday, '%d/%m/%Y')) = MONTH(NOW()) ORDER BY `student`.`birthday` ASC";
 $this->db->where($where);
 $students = $this->db->get('student')->result_array();
 foreach ($students as $row):

 $class_id = $this->db->get_where('enroll', array('student_id' => 
 $row['student_id']))->row()->class_id;
 $section_id = $this->db->get_where('enroll', array('student_id' => 
 $row['student_id']))->row()->section_id;
?>

Is working perfectly:

inserir a descrição da imagem aqui

I created this code for the admin view, now I need to modify it to the teacher view. (Then it is necessary to show only the students of the teacher’s class who is logged in.

The table that identifies which class and class of the teacher is the 'Section': Inside it is the class_id, section_id and teacher_id that makes the proper relation.

This code the system shows which teacher is logged in:

$teachers = $this->db->get_where('teacher', array('teacher_id' => $this->session->userdata('teacher_id')))->result_array();
foreach ($teachers as $teacher):

Already this code shows the class belonging to the teacher who is logged in:

<?php
 $sections = $this->db->get_where('section', array('teacher_id' => $this->session->userdata('teacher_id')))->result_array();
foreach ($sections as $section):

 $class = $this->db->get_where('class', array('class_id' => $section['class_id']))->row();
?>

With this command below, the name of the teacher and the corresponding class is shown:

<?php echo $teacher['name']; ?> <?php echo $section['name'];?>

My difficulty is here!!

I need the code to search only the students of the teacher’s class who is logged in. The school year field of the above table (image) will come out. Thus remaining:

inserir a descrição da imagem aqui

So I need the foreach to search only students in the teacher’s class logged in (code above) and not all classes.

UPDATING OBTAINED RESULT FOR POSSIBLE SOLUTION

I added the $student_id variable here:

<?php
  $where = "MONTH(STR_TO_DATE(birthday, '%d/%m/%Y')) = MONTH(NOW()) ORDER BY `student`.`birthday` ASC";
  $this->db->where($where);
  $students = $this->db->get('student')->result_array();
  foreach ($students as $row):

 $student_id = $this->db->get_where('enroll', array('student_id' => $row['student_id']))->row()->student_id;
 $class_id = $this->db->get_where('enroll', array('student_id' => $row['student_id']))->row()->class_id;
 $section_id = $this->db->get_where('enroll', array('student_id' => $row['student_id']))->row()->section_id;
?>

inserir a descrição da imagem aqui

As you can see now, it only shows the name of the student in the class,So I thought if I make a condition similar to the condition below that shows the name of the student of the class within the foreach (he manages to fetch only the students of the class in question, the problem is that the variables use $Row, then how to create the condition?

Condition to show only the name of the class birthday student;

<?php if ($student_id ==  $row['student_id'] && $section_id ==  $section['section_id'])
echo $row['name']; ?>

I managed to solve, thank you all: What I did?!

<?php
 $where = "MONTH(STR_TO_DATE(birthday, '%d/%m/%Y')) = MONTH(NOW()) ORDER BY `student`.`birthday` ASC";
 $this->db->where($where);
 $students = $this->db->get('student')->result_array();
 foreach ($students as $row){

  $student_id = $this->db->get_where('enroll', array('student_id' => $row['student_id']))->row()->student_id;
  $class_id = $this->db->get_where('enroll', array('student_id' => $row['student_id']))->row()->class_id;
  $section_id = $this->db->get_where('enroll', array('student_id' => $row['student_id']))->row()->section_id;

  if ($student_id ==  $row['student_id'] && $section_id ==  $section['section_id']) break;
 }
?>
  • The session stores what?

  • @Lucas Antonio -> Stores the teacher’s id.

  • And the teacher is logged right?

  • Yeah. @Lucas Antonio.

  • @Lucas Antonio, I believe I should filter the variable $Tudents taken from her the student_id and comparing with the student_id of the Enroll table (which stores all students by assigning the class (class_id) and class (section_id) before the foreach.

  • Like this: $birthday girls = this .. student_id where student_id is equal to student_id of Enroll table which is also equal to class_id and section_id of the logged in teacher.

  • Your problem is simple friend

  • Recovers the id of Session and makes a Where where it lists the students, of course the students with relationship with the teacher and in Where you take id of the teacher equal to that of the session and then return students of the teacher.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.