sovinjav

classes.php

Oct 6th, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.98 KB | None | 0 0
  1. <?php
  2.     class Stundent {
  3.         private $student_id = NULL;
  4.         private $first_name;
  5.         private $last_name;
  6.         private $gender;
  7.         private $email;
  8.         private $birth_date;
  9.         private $specialty;
  10.         private $spec_id;
  11.         private $exam_results = [];
  12.         public function addToDB($db_conn, $exams) {
  13.             $sqlquery = "INSERT INTO students (first_name, last_name, email, gender, birth_date, spec_id)
  14.                                         VALUES ('{$this->first_name}', '{$this->last_name}', '{$this->email}',
  15.                                     '{$this->gender}', '{$this->birth_date}', {$this->spec_id})";
  16.             if (!mysqli_query($db_conn,$sqlquery)) {
  17.                 echo 'Error: Cannot add to database</br>';
  18.             };
  19.             $res = mysqli_query($db_conn, "SELECT student_id FROM students
  20.                                                     WHERE first_name = '{$this->first_name}' AND last_name = '{$this->last_name}' AND email = '{$this->email}'");
  21.             $row = mysqli_fetch_assoc($res);
  22.             $this->student_id = $row["student_id"];
  23.             foreach ($exams as $exam_id => $exam_name) {
  24.                 $sqlquery = "INSERT INTO exam_results (student_id, exam_id, exam_points)
  25.                                             VALUES ('{$this->student_id}', {$exam_id}, '{$this->exam_results[$exam_id][1]}')";
  26.                 $res = mysqli_query($db_conn, $sqlquery);
  27.             }
  28.         }
  29.         public function getFromDB($db_conn, $exams) {
  30.             $res = mysqli_query($db_conn, "SELECT gender, birth_date, spec_id, student_id FROM students
  31.             WHERE first_name = '{$this->first_name}' AND last_name = '{$this->last_name}' AND email = '{$this->email}'");
  32.             $row = mysqli_fetch_assoc($res);
  33.             $this->gender=$row["gender"];
  34.             $this->birth_date=$row["birth_date"];
  35.             $this->spec_id = $row["spec_id"];
  36.             $this->student_id = $row["student_id"];
  37.             $res = mysqli_query($db_conn, "SELECT name FROM specialties WHERE spec_id = {$this->spec_id}");
  38.             $row = mysqli_fetch_assoc($res);
  39.             $this->specialty = $row["name"];
  40.             foreach ($exams as $exam_id => $exam_name) {
  41.                 $res = mysqli_query($db_conn, "SELECT exam_points FROM exam_results WHERE student_id = {$this->student_id} AND exam_id = {$exam_id}");
  42.                 $row = mysqli_fetch_assoc($res);
  43.                 $ex = [$exam_name, $row['exam_points']];
  44.                 $this->exam_results[$exam_id] = $ex;
  45.             }
  46.         }
  47.        
  48.         public function getFromDBAll($db_conn, $exams) {
  49.             $res = mysqli_query($db_conn, "SELECT * FROM students");
  50.             $row = mysqli_fetch_assoc($res);
  51.             while ($row) {
  52.                 $this->student_id = $row["student_id"];
  53.                 $this->first_name = $row["first_name"];
  54.                 $this->last_name = $row["last_name"];
  55.                 $this->email = $row["email"];
  56.                 $this->gender=$row["gender"];
  57.                 $this->birth_date=$row["birth_date"];
  58.                 $this->spec_id = $row["spec_id"];
  59.                 $res = mysqli_query($db_conn, "SELECT name FROM specialties WHERE spec_id = {$this->spec_id}");
  60.                 $row = mysqli_fetch_assoc($res);
  61.                 $this->specialty = $row["name"];
  62.                 foreach ($exams as $exam_id => $exam_name) {
  63.                     $res = mysqli_query($db_conn, "SELECT exam_points FROM exam_results WHERE student_id = {$this->student_id} AND exam_id = {$exam_id}");
  64.                     $row = mysqli_fetch_assoc($res);
  65.                     $ex = [$exam_name, $row['exam_points']];
  66.                     $this->exam_results[$exam_id] = $ex;
  67.                 }
  68.                 $this->printStudent();
  69.             }
  70.         }
  71.        
  72.         public function saveExamResults($exam_id, $exam_name, $exam_points) {
  73.             $ex = [];
  74.             array_push($ex, $exam_name, $exam_points);
  75.             $this->exam_results[$exam_id] = $ex;
  76.         }
  77.        
  78.         public function printStudent () {
  79.             //foreach ($this->exams as $exam_id => $exam_name) {
  80.             //}
  81.             echo $this->first_name . " " . $this->last_name . " " . $this->email . " " . $this->gender .
  82.             " " . $this->birth_date . " " . $this->specialty;
  83.         }
  84.        
  85.         public function __construct($first_name, $last_name, $email,
  86.                                                                 $gender, $birth_date, $specialty, $specialties) {
  87.             $this->first_name = $first_name;
  88.             $this->last_name = $last_name;
  89.             $this->email = $email;
  90.             $this->gender = $gender;
  91.             $this->birth_date = $birth_date;
  92.             if ($specialty) {
  93.                 $this->specialty = $specialty;
  94.                 $flip_specialties = array_flip($specialties);
  95.                 $this->spec_id = $flip_specialties[$specialty];
  96.             }
  97.         }
  98.     }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment