<?php
require_once "/lib/pdo.php";
$mapper = new DataMapper($DBH);
if(isset($_COOKIE['studentscookie']['id'])){ $id = $_COOKIE['studentscookie']['id'];
}
else{
setcookie("studentscookie[id]", $mapper->getLastId()+1, time()+(7*24*60*60*42), "/"); }
$students = $mapper->showAllStudents();
if(isset($_POST['submit'])){ $profile=new Profile;
$profile->setFields($_POST);
$profile2=(array)$profile;
// foreach ($profile2 as $t):
// echo $t."<br>";
// endforeach;
// echo $profile->showGroupnum();
$mapper->addStudent($profile);
}
// foreach ($students as $student):
// echo $student->showName()."<br>";
// endforeach;
include "template.html";
?>
<?php//pdo
require_once "/lib/login.php";
require_once "/lib/Profile.php";
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
class DataMapper
{
public $DBH;
public function __construct(PDO $DBH)
{
$this->DBH = $DBH;
}
public function showAllStudents()
{
$STH = $this->DBH->prepare("SELECT * FROM students");
$STH->execute();
$result = $STH->fetchAll(PDO::FETCH_CLASS, "profile");
return $result;
}
public function addStudent(Profile $profile)
{
$STH = $this->DBH->prepare("INSERT INTO students (name, surname, sex, groupnum, email, points, year, place)
VALUES (:name, :surname, :sex, :groupnum, :email, :points, :year, :place)");
$STH->bindparam(":name", $name);
$STH->bindparam(":surname", $surname);
$STH->bindparam(":sex", $sex);
$STH->bindparam(":groupnum", $groupnum);
$STH->bindparam(":email", $email);
$STH->bindparam(":points", $points);
$STH->bindparam(":year", $year);
$STH->bindparam(":place", $place);
$name = $profile->showName();
$surname = $profile->showSurname();
$sex = $profile->showSex();
$groupnum = $profile->showGroupnum();
$email = $profile->showEmail();
$points = $profile->showPoints();
$year = $profile->showYear();
$place = $profile->showPlace();
$STH->execute();
}
public function editProfile(Profile $profile)
{
$STH = $this->DBH->prepare("UPDATE students SET name=:name, surname=:surname, sex=:sex, groupnum=:groupnum, email=:email, points=:points, year=:year, place=:place WHERE id=:id");
$STH->bindparam(":name", $name);
$STH->bindparam(":surname", $surname);
$STH->bindparam(":sex", $sex);
$STH->bindparam(":groupnum", $groupnum);
$STH->bindparam(":email", $email);
$STH->bindparam(":points", $points);
$STH->bindparam(":year", $year);
$STH->bindparam(":place", $place);
$STH->bindparam(":id", $id);
$name = $profile->showName();
$surname = $profile->showSurname();
$sex = $profile->showSex();
$groupnum = $profile->showGroupnum();
$email = $profile->showEmail();
$points = $profile->showPoints();
$year = $profile->showYear();
$place = $profile->showPlace();
$id = $profile->showID();
$STH->execute();
}
public function getLastID()
{
$STH = $this->DBH->query("SELECT COUNT(*) FROM students");
$result = $STH->fetchColumn();
$id = $result;
return $id;
}
}
?>
<?php
class Profile{
protected $name;
protected $surname;
protected $sex;
protected $groupnum;
protected $email;
protected $points;
protected $year;
protected $place;
public function setFields($data)
{
foreach ($data as $key => $value) {
$data[$key] = trim($value); }
$this->name = $data['name'];
$this->surname = $data['surname'];
$this->sex = $data['sex'];
$this->groupnum = $data['groupnum'];
$this->email = $data['email'];
$this->points = $data['points'];
$this->year = $data['year'];
$this->place = $data['place'];
}
public function showName()
{
return $this->name;
}
public function showSurname()
{
return $this->surname;
}
public function showSex()
{
return $this->sex;
}
public function showGroupnum()
{
return $this->groupnum;
}
public function showEmail()
{
return $this->email;
}
public function showPoints()
{
return $this->points;
}
public function showYear()
{
return $this->year;
}
public function showPlace()
{
return $this->place;
}
}
?>