Created
July 29, 2018 21:47
-
-
Save daldantas/f9bf216a9afec92d5a2490f413f9029f to your computer and use it in GitHub Desktop.
To get a table on a sheet (PHPOO/MySQL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// DATABASE GENERATOR | |
// CREATE DATABASE db; | |
// USE db; | |
// CREATE TABLE contacts ( | |
// id int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, | |
// name varchar(255) NOT NULL, | |
// email varchar(255) NOT NULL, | |
// phone varchar(15) NOT NULL); | |
// INSERT INTO contacts (name, email, phone) VALUES | |
// ('Contact 1', 'contact1@email.com', '(01) 99999-9999'), | |
// ('Contact 2', 'contact2@email.com', '(02) 88888-8888'); | |
// MODELS | |
class Model | |
{ | |
private $host = "localhost"; | |
private $user = "root"; | |
private $pass = ""; | |
private $db = "db"; | |
private $mysqli; | |
protected function execute($q) | |
{ | |
$this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->db); | |
return $this->mysqli->query($q); | |
$this->mysqli->close(); | |
} | |
} | |
class Contact extends Model | |
{ | |
private $q; | |
private $name; | |
private $email; | |
private $phone; | |
protected function selectAll() | |
{ | |
$this->q = "SELECT * FROM contacts"; | |
return $this->execute($this->q); | |
} | |
} | |
// CONTROLLER | |
class Contacts extends Contact | |
{ | |
private $data; | |
public function getAll() | |
{ | |
$this->data = $this->selectAll(); | |
return $this->data; | |
} | |
} | |
// TO VIEW | |
$contacts = new Contacts; | |
$data['contacts'] = $contacts->getAll(); | |
foreach ( $data as $key => $value ){ $$key = $value; } | |
?> | |
<!-- VIEW --> | |
<!DOCTYPE html> | |
<html lang="pt-br"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Contacts</title> | |
</head> | |
<body> | |
<h1>Contact List</h1> | |
<table cellpadding="10" style="text-align: left"> | |
<tr> | |
<th>Id</th> | |
<th>Name</th> | |
<th>Email</th> | |
<th>Phone</th> | |
</tr> | |
<?php foreach ($contacts as $cont => $c): ?> | |
<tr> | |
<td><?=$c['id']?></td> | |
<td><?=$c['name']?></td> | |
<td><?=$c['email']?></td> | |
<td><?=$c['phone']?></td> | |
</tr> | |
<?php endforeach ?> | |
</table> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment