Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once __DIR__.'/Student.php';
- require_once __DIR__.'/db_connection.php';
- // $db = new Database();
- // var_dump($db);
- class StudentGateway {
- private $query;
- private $sql;
- private $db;
- private $offset;
- private $rows = 10;
- public function __construct($p) {
- $this->db = new Database();
- $this->offset = $p * $this->rows - $this->rows; //сверхформула для высчитывания оффсета, чтобы переходить по страницам вроде page=1, page=2 etc
- }
- public function SelectStudent(){
- // var_dump($this->db);
- // var_dump($this->db);
- // var_dump(StudentGateway);
- $this->sql = 'SELECT first_name,last_name,group_num,exam_sum
- FROM students
- ORDER BY id DESC
- LIMIT '. $this->rows .' OFFSET '. $this->offset;
- $this->query = $this->db->getPdo()->prepare($this->sql);
- $this->query->execute();
- $listOfStudents = [];
- while ($row = $this->query->fetch(PDO::FETCH_ASSOC)) {
- $listOfStudents[] = $row;
- }
- return $listOfStudents ;
- }
- public function InsertStudent($s) {
- $this->sql = 'INSERT INTO students(
- first_name,
- second_name,
- last_name,
- group_num,
- email,
- birthday,
- is_local,
- exam_sum)
- VALUES (
- :firstName,
- :secondName,
- :lastName,
- :group,
- :mail,
- :birthday,
- :isLocal,
- :examSum
- )';
- $this->query = $this->db->getPdo()->prepare($this->sql);
- $this->query->bindValue(':firstName', $s->getFirstName(), PDO::PARAM_STR);
- $this->query->bindValue(':secondName', $s->getSecondName(), PDO::PARAM_STR);
- $this->query->bindValue(':lastName', $s->getLastName(), PDO::PARAM_STR);
- $this->query->bindValue(':group', $s->getGroup(), PDO::PARAM_STR);
- $this->query->bindValue(':mail', $s->getEmail(), PDO::PARAM_STR);
- $this->query->bindValue(':birthday', $s->getBirthday(), PDO::PARAM_STR);
- $this->query->bindValue(':isLocal', $s->getLocal(), PDO::PARAM_STR);
- $this->query->bindValue(':examSum', $s->getExamsum(), PDO::PARAM_STR);
- $this->query->execute();
- // exit();
- }
- public function getTotalRows() {
- $this->sql = 'SELECT COUNT(id) FROM students';
- $this->query = $this->db->getPdo()->prepare($this->sql);
- $this->query->execute();
- $pages = $this->query->fetch(PDO::FETCH_ASSOC);
- $pages = intval($pages['COUNT(id)']);
- $pages = $pages / $this->rows;
- return ceil($pages);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement