mysqli = new mysqli("localhost", "increment", apache_getenv("INCREMENT_PASS"), "www_increment"); $this->mysqli->set_charset("utf8mb4"); $this->page = $page->url; } function get_count() : int { $query = $this->mysqli->prepare("SELECT count FROM increments WHERE page=?"); $query->bind_param('s', $this->page); $query->execute(); $result = $query->get_result(); if ($result->num_rows >= 1) { $row = $result->fetch_assoc(); return intval($row['count']); } else { $query = $this->mysqli->prepare("INSERT INTO increments (page,count) VALUES (?,0)"); $query->bind_param('s', $this->page); $query->execute(); return 0; } } function increment() { $query = $this->mysqli->prepare("UPDATE increments SET count=count+1 WHERE page=?"); $query->bind_param('s', $this->page); $query->execute(); } function handle_increment() { if ($_SERVER['REQUEST_METHOD'] == "POST") { if (isset($_POST['increment'])) { $this->get_count(); $this->increment(); } } } } class IncrementButton implements Widget { private int $_count; function __construct() { $this->_count = 0; } static function new() { return new IncrementButton(); } function count($count) { $this->_count = $count; return $this; } public function html() : string { $increments = strval($this->_count) . " increment"; if ($this->_count != 1) $increments .= "s"; return << EOF; } } ?>