Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Stundent {
- private $student_id = NULL;
- private $first_name;
- private $last_name;
- private $gender;
- private $email;
- private $birth_date;
- private $specialty;
- private $spec_id;
- private $exam_results = [];
- public function addToDB($db_conn, $exams) {
- $sqlquery = "INSERT INTO students (first_name, last_name, email, gender, birth_date, spec_id)
- VALUES ('{$this->first_name}', '{$this->last_name}', '{$this->email}',
- '{$this->gender}', '{$this->birth_date}', {$this->spec_id})";
- if (!mysqli_query($db_conn,$sqlquery)) {
- echo 'Error: Cannot add to database</br>';
- };
- $res = mysqli_query($db_conn, "SELECT student_id FROM students
- WHERE first_name = '{$this->first_name}' AND last_name = '{$this->last_name}' AND email = '{$this->email}'");
- $row = mysqli_fetch_assoc($res);
- $this->student_id = $row["student_id"];
- foreach ($exams as $exam_id => $exam_name) {
- $sqlquery = "INSERT INTO exam_results (student_id, exam_id, exam_points)
- VALUES ('{$this->student_id}', {$exam_id}, '{$this->exam_results[$exam_id][1]}')";
- $res = mysqli_query($db_conn, $sqlquery);
- }
- }
- public function getFromDB($db_conn, $exams) {
- $res = mysqli_query($db_conn, "SELECT gender, birth_date, spec_id, student_id FROM students
- WHERE first_name = '{$this->first_name}' AND last_name = '{$this->last_name}' AND email = '{$this->email}'");
- $row = mysqli_fetch_assoc($res);
- $this->gender=$row["gender"];
- $this->birth_date=$row["birth_date"];
- $this->spec_id = $row["spec_id"];
- $this->student_id = $row["student_id"];
- $res = mysqli_query($db_conn, "SELECT name FROM specialties WHERE spec_id = {$this->spec_id}");
- $row = mysqli_fetch_assoc($res);
- $this->specialty = $row["name"];
- foreach ($exams as $exam_id => $exam_name) {
- $res = mysqli_query($db_conn, "SELECT exam_points FROM exam_results WHERE student_id = {$this->student_id} AND exam_id = {$exam_id}");
- $row = mysqli_fetch_assoc($res);
- $ex = [$exam_name, $row['exam_points']];
- $this->exam_results[$exam_id] = $ex;
- }
- }
- public function getFromDBAll($db_conn, $exams) {
- $res = mysqli_query($db_conn, "SELECT * FROM students");
- $row = mysqli_fetch_assoc($res);
- while ($row) {
- $this->student_id = $row["student_id"];
- $this->first_name = $row["first_name"];
- $this->last_name = $row["last_name"];
- $this->email = $row["email"];
- $this->gender=$row["gender"];
- $this->birth_date=$row["birth_date"];
- $this->spec_id = $row["spec_id"];
- $res = mysqli_query($db_conn, "SELECT name FROM specialties WHERE spec_id = {$this->spec_id}");
- $row = mysqli_fetch_assoc($res);
- $this->specialty = $row["name"];
- foreach ($exams as $exam_id => $exam_name) {
- $res = mysqli_query($db_conn, "SELECT exam_points FROM exam_results WHERE student_id = {$this->student_id} AND exam_id = {$exam_id}");
- $row = mysqli_fetch_assoc($res);
- $ex = [$exam_name, $row['exam_points']];
- $this->exam_results[$exam_id] = $ex;
- }
- $this->printStudent();
- }
- }
- public function saveExamResults($exam_id, $exam_name, $exam_points) {
- $ex = [];
- array_push($ex, $exam_name, $exam_points);
- $this->exam_results[$exam_id] = $ex;
- }
- public function printStudent () {
- //foreach ($this->exams as $exam_id => $exam_name) {
- //}
- echo $this->first_name . " " . $this->last_name . " " . $this->email . " " . $this->gender .
- " " . $this->birth_date . " " . $this->specialty;
- }
- public function __construct($first_name, $last_name, $email,
- $gender, $birth_date, $specialty, $specialties) {
- $this->first_name = $first_name;
- $this->last_name = $last_name;
- $this->email = $email;
- $this->gender = $gender;
- $this->birth_date = $birth_date;
- if ($specialty) {
- $this->specialty = $specialty;
- $flip_specialties = array_flip($specialties);
- $this->spec_id = $flip_specialties[$specialty];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment