我是 OOP PHP 的新手,我想尽快获得尽可能多的编码约定。我制作了这个小小的留言簿脚本,我想知道是否有任何事情没有完成。
索引.php:
<?php
$username = "root";
$password = "";
$database = "oop";
$host = "localhost";
mysql_connect($host, $username, $password);
mysql_select_db($database);
?>
<html>
<head>
</head>
<body>
<?php
include("views/gaestebog.php");
?>
</body>
</html>
Guestbook.php,类:
<?php
class Gaestebog {
public function __contruct() {
}
public function getPosts() {
$query = mysql_query("SELECT * FROM gaestebog");
while ($row = mysql_fetch_array($query)) {
echo '
<tr>
<td>'.$row['navn'].'</td>
</tr>
<tr>
<td>'.$row['besked'].'</td>
</tr>
';
}
}
public function addPost($navn, $besked) {
mysql_query("INSERT INTO gaestebog VALUES('', '$navn', '$besked')");
}
}
?>
和guestbook.php,视图:
<?php
include("classes/Gaestebog.php");
$gaestebog = new Gaestebog();
if (isset($_POST['opret'])) {
$navn = $_POST['navn'];
$besked = $_POST['besked'];
$gaestebog->addPost($navn, $besked);
}
?>
<table>
<?php
$gaestebog->getPosts();
?>
</table>
<hr />
<form action="" method="post">
<table>
<tr>
<td>Navn:</td>
<td><input type="text" name="navn" value="Patrick" /></td>
</tr>
<tr>
<td>Besked:</td>
<td><input type="text" name="besked" value="Hej med dig !!" /></td>
</tr>
<tr>
<td><input type="submit" name="opret" value="Opret" /></td>
</tr>
</table>
</form>