Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. <?php
  2.  
  3. require_once __DIR__.'/Student.php';
  4. require_once __DIR__.'/db_connection.php';
  5.  
  6.  
  7.  
  8. // $db = new Database();
  9. // var_dump($db);
  10.  
  11. class StudentGateway {
  12.  
  13.     private $query;
  14.     private $sql;
  15.     private $db;
  16.     private $offset;
  17.     private $rows = 10;
  18.  
  19.     public function __construct($p) {
  20.         $this->db = new Database();
  21.         $this->offset = $p * $this->rows - $this->rows; //сверхформула для высчитывания оффсета, чтобы переходить по страницам вроде page=1, page=2 etc
  22.     }
  23.  
  24.     public function SelectStudent(){
  25.         // var_dump($this->db);
  26.         // var_dump($this->db);
  27.         // var_dump(StudentGateway);
  28.  
  29.  
  30.         $this->sql = 'SELECT first_name,last_name,group_num,exam_sum
  31.                        FROM students
  32.                        ORDER BY id DESC
  33.                        LIMIT '. $this->rows .' OFFSET '. $this->offset;
  34.  
  35.         $this->query = $this->db->getPdo()->prepare($this->sql);
  36.  
  37.         $this->query->execute();
  38.  
  39.         $listOfStudents = [];
  40.  
  41.         while ($row = $this->query->fetch(PDO::FETCH_ASSOC)) {
  42.                 $listOfStudents[] = $row;
  43.         }
  44.  
  45.         return $listOfStudents ;
  46.            
  47.             }
  48.  
  49.  
  50.  
  51.  
  52.     public function InsertStudent($s) {
  53.  
  54.         $this->sql = 'INSERT INTO students(
  55.                first_name,
  56.                second_name,
  57.                last_name,
  58.                group_num,
  59.                email,
  60.                birthday,
  61.                is_local,
  62.                exam_sum)
  63.                VALUES (
  64.                :firstName,
  65.                :secondName,
  66.                :lastName,
  67.                :group,
  68.                :mail,
  69.                :birthday,
  70.                :isLocal,
  71.                :examSum
  72.            )';
  73.            
  74.         $this->query = $this->db->getPdo()->prepare($this->sql);
  75.  
  76.  
  77.         $this->query->bindValue(':firstName', $s->getFirstName(), PDO::PARAM_STR);
  78.         $this->query->bindValue(':secondName', $s->getSecondName(), PDO::PARAM_STR);
  79.         $this->query->bindValue(':lastName', $s->getLastName(), PDO::PARAM_STR);
  80.         $this->query->bindValue(':group', $s->getGroup(), PDO::PARAM_STR);
  81.         $this->query->bindValue(':mail', $s->getEmail(), PDO::PARAM_STR);
  82.         $this->query->bindValue(':birthday', $s->getBirthday(), PDO::PARAM_STR);
  83.         $this->query->bindValue(':isLocal', $s->getLocal(), PDO::PARAM_STR);
  84.         $this->query->bindValue(':examSum', $s->getExamsum(), PDO::PARAM_STR);
  85.  
  86.  
  87.         $this->query->execute();
  88.  
  89.         // exit();
  90.     }
  91.  
  92.     public function getTotalRows() {
  93.  
  94.         $this->sql = 'SELECT COUNT(id) FROM students';
  95.  
  96.         $this->query = $this->db->getPdo()->prepare($this->sql);
  97.  
  98.         $this->query->execute();
  99.  
  100.         $pages = $this->query->fetch(PDO::FETCH_ASSOC);
  101.         $pages = intval($pages['COUNT(id)']);
  102.  
  103.         $pages = $pages / $this->rows;
  104.  
  105.  
  106.         return ceil($pages);
  107.     }
  108.  
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement