ved at samle dine sql classer i en model component får du et bedre overblik, og kan lettere se hvad du kan genbruge andre steder.
ved at referarer til denne fil og bruge require_once, får du heller ikke fejlen med method er allerede defineret, med mindre du laver sammen methode flere gange i sammen class.
jeg har ikke ændret på arven, men jeg ville nok lave en instance i hver class frem for at arven DB, som vist i myclass
<?php
// db.class.php omdøbt til model.php, da det er en model af databasen
// admin.php er slettet, indhold flyttet over i denne fil
class DB {
// private kan ikke nedarves, kan ikke nåes udefra
// protected kan nedarves 1 led, men ikke nåes udefra
// public kan nedarves mange led, og nåes udefra
protected $dbhost = 'localhost';
protected $dbuser = 'test';
protected $dbpass = 'xxxx';
protected $dbname = 'test';
protected $conn = '';
public function __construct() {
$conn = mysqli_connect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
if (mysqli_connect_errno()) {
die('Connection failed : <i>' . mysqli_connect_error($this->conn) . '</i>');
exit;
}
$this->conn = $conn;
}
public function connection(){
return $this->conn;
}
}
class Admin extends DB {
public function __construct() {
parent::__construct();
}
// flyttet fra DB classen
/*
public function adminHeader($uid) {
$stmt = $this->conn->prepare("select user_id, fullname, access_level from admin where user_id = ?");
if ($stmt) {
mysqli_stmt_bind_param($stmt, "i", $uid);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $user_id, $fullname, $access_level);
while (mysqli_stmt_fetch($stmt)) {
echo '
<label>Navn : </label>
<span>' . $fullname . '</span>
<label>Adgang : </label>
<span>' . $access_level . '</span>
<label>ID Nummer : </label>
<span>' . $user_id . '</span>
';
}
mysqli_stmt_close($stmt);
}
}
*/
public function adminUser($uid) {
echo '<div class="adminheader">';
$stmt = $this->conn->prepare("select user_id, fullname, access_level from admin where user_id = ?");
if ($stmt) {
mysqli_stmt_bind_param($stmt, "i", $uid);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $user_id, $fullname, $access_level);
while (mysqli_stmt_fetch($stmt)) {
echo '
<label>Navn : </label>
<span>' . $fullname . '</span>
<label>Adgang : </label>
<span>' . $access_level . '</span>
<label>ID Nummer : </label>
<span>' . $user_id . '</span>
';
}
mysqli_stmt_close($stmt);
}
// $this->adminHeader($uid);
echo '</div>';
}
// flyttet fra DB classen
/*
public function insertContact($firstname, $middlename, $lastname, $birthday, $phone, $email, $relation) {
$sql = "insert into contacts(firstname, middlename, lastname, birthday, phone, email, relation) values('$firstname', '$middlename', '$lastname', '$birthday', '$phone', '$email', '$relation')";
mysqli_query($this->conn, $sql) or die(mysqli_error($this->conn));
}
*/
// flyttet fra admin.php
// jeg ville flytte denne metode ned i "class Contacts"
public function addContact($firstname, $middlename, $lastname, $birthday, $phone, $email, $relation) {
$sql = "insert into contacts(firstname, middlename, lastname, birthday, phone, email, relation) values('$firstname', '$middlename', '$lastname', '$birthday', '$phone', '$email', '$relation')";
mysqli_query($this->conn, $sql) or die(mysqli_error($this->conn));
// $this->insertContact($firstname, $middlename, $lastname, $birthday, $phone, $email, $relation);
}
}
// fra
http://www.computerworld.dk/eksperten/spm/1011351class Contacts extends DB {
// hvorfor denne ekstra function ??, kald search direkte, eller lig search ind i dene function
public function find_contact($seek) {
$q = $seek;
echo 'Du har søgt på: <i><b>'.$q.'</b></i>';
$this->search($q);
}
// flyttet fra DB classen
public function search($q) {
$search = '%'.$q.'%';
$stmt = $this->conn->prepare("select firstname, middlename, lastname, phone, email from contacts where firstname = ?");
if($stmt) {
mysqli_stmt_bind_param($stmt, 's', $search);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $firstname, $middlename, $lastname, $phone, $email);
while(mysqli_stmt_fetch($stmt)) {
$fullname = $fisrtname.' '.$middlename.' '.$lastname;
echo '
<span class="info">'.$fullname.'</span>
<span class="info">'.$phone.'</span>
<span class="info">'.$email.'</span>
';
}
}
}
// igen hvorfor denne wrapper function ??
public function contactList() {
$this->showAll();
}
// omtalt i
http://www.computerworld.dk/eksperten/spm/1011340 #25
// flyttet fra DB classen
public function showAll() {
$stmt = $this->conn->prepare("select firstname, middlename, lastname, phone, email, relation from contacts");
if ($stmt) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $firstname, $middlename, $lastname, $phone, $email, $relation);
while (mysqli_stmt_fetch($stmt)) {
$fullname = $firstname . ' ' . $middlename . ' ' . $lastname;
echo '
<span>' . $fullname . '</span>
<span>' . $phone . '</span>
<span>' . $email . '</span>
<span>' . $relation . '</span>
';
}
mysqli_stmt_close($stmt);
}
}
}
// her gider jeg ikke arve min connection, jeg kan jo kun arve fra én class, så jeg holder arven i baghånden
// dette gør jeg normalt
class myclass{
private $db; // hvis jeg skal bruge andre motoder fra DB,
private $conn;
public function __construct() {
$this->db=new DB();
$this->conn = $this->db->connection();
}
}
?>