The following code will read a db, show the contents to the user and provide instruments to edit them. This has been tested and works well.
model.php
<?php
class Collega {
private $host="localhost";
private $user="root";
private $pwd="";
private $attiva=false;
public function connetti () {
if (!$this->attiva) {
if (!mysql_connect($this->host, $this->user, $this->pwd)) {
die (mysql_error());
exit ();
} else {
$this->attiva=true;
}
}
}
public function selectdatabase ($dbname) {
if (!$this->attiva) {
return false;
exit ();
} else {
if (!mysql_select_db($dbname)) {
echo mysql_error();
return false;
exit ();
} else {
return true;
}
}
}
public function interroga($sql) {
if ($this->attiva) {
$query = mysql_query($sql) or die (mysql_error());
return $query;
}
}
}
$connessione = new Collega ();
$connessione->connetti();
$connessione->selectdatabase("conoscenze");
$result = $connessione->interroga("SELECT * FROM conoscenti");
?>
view.php
<?php
include ("model.php");
include ("controller.php");
class Visualizza {
public function tetris ($rex) {
echo "<table>";
echo "<tr><td>Nome</td><td>Cognome</td><td>Data di nascita</td><td></td><td></td></tr>";
while ($row=mysql_fetch_array($rex)) {
$birth=date_parse($row['Nato']);
echo "<tr><td>".$row['Nome']."</td><td>".$row['Cognome']."</td><td>".$birth['day']."/".$birth['month']."/".$birth['year']."</td><td><form action='".$_SERVER['PHP_SELF']."?action=edit&id=".$row['id']."' method='post'><input type='hidden' name='nome' value='".$row['Nome']."' /><input type='hidden' name='cognome' value='".$row['Cognome']."' /><input type='hidden' name='nato' value='".$row['Nato']."' /><input type='hidden' name='index' value='".$row['id']."' /><button>Modifica</button></form></td><td><form action='".$_SERVER['PHP_SELF']."?action=delete' method='post'><input type='hidden' name='id' value='".$row['id']."' /><button>Elimina</button></form></td></tr>";
}
echo "</table>";
echo "<form action='".$_SERVER['PHP_SELF']."?action=new' method='post'>";
echo "<input type='hidden' name='nome' value='' />";
echo "<input type='hidden' name='cognome' value='' />";
echo "<input type='hidden' name='nato' value='' />";
echo "<button type='submit'>Crea nuova voce</button>";
echo "</form>";
}
public function editordati () {
echo "<form action='".$_SERVER['PHP_SELF']."?action=submit' method='post'>";
echo "<input type='text' value='".$_POST['nome']."' name='nome' />";
echo "<input type='text' value='".$_POST['cognome']."' name='cognome' />";
echo "<input type='date' value='".$_POST['nato']."' name='nato' />";
echo "<input type='hidden' value='".$_GET['id']."' name='id' />";
echo "<input type='submit' />";
echo "</form>";
}
public function nuovo () {
echo "<form action='".$_SERVER['PHP_SELF']."?action=submitnew' method='post'>";
echo "<input type='text' value='' name='nome' />";
echo "<input type='text' value='' name='cognome' />";
echo "<input type='date' value='' name='nato' />";
echo "<input type='submit' />";
echo "</form>";
}
}
$show=new Visualizza ();
if (!isset($_GET['action'])) {
$show->tetris($result);
} elseif ($_GET['action']=="edit") {
if (!$_GET['id']) {
exit ();
} else {
$show->editordati();
}
} elseif ($_GET['action']=="new") {
$show->nuovo();
} elseif ($_GET['action']=="submit") {
$penna->editexisting("conoscenti", $_POST['id']);
$show->tetris($connessione->interroga("SELECT * FROM conoscenti"));
} elseif ($_GET['action']=="submitnew") {
$penna->addnew("conoscenti");
$show->tetris($connessione->interroga("SELECT * FROM conoscenti"));
} elseif ($_GET['action']=="delete") {
$penna->destroyer("conoscenti", $_POST['id']);
$show->tetris($connessione->interroga("SELECT * FROM conoscenti"));
}
?>
controller.php
<?php
class Editor {
public function editexisting ($table, $id) {
if (!isset($_POST['nome']) && !isset($_POST['cognome']) && !isset($_POST['nato'])) {
exit ();
} else {
$editq="UPDATE ".$table." SET Nome='".$_POST['nome']."', Cognome='".$_POST['cognome']."', Nato='".$_POST['nato']."' WHERE id='".$id."'";
$editr=mysql_query($editq) or die (mysql_error());
return $editr;
}
}
public function destroyer ($table, $id) {
$delq="DELETE FROM ".$table." WHERE id='".$id."'";
$delr=mysql_query($delq) or die(mysql_error());
return $delr;
}
public function addnew ($table) {
$addq="INSERT INTO ".$table."(id, Nome, Cognome, Nato) VALUES (null, '".$_POST['nome']."', '".$_POST['cognome']."', '".$_POST['nato']."')";
$addr=mysql_query($addq) or die (mysql_error());
return $addr;
}
}
$penna= new Editor ();
?>
I named the scripts so it's easy to understand who plays what role in the MVC opera. Did I write a good MVC example or should I move some code somewhere else?