PHP留言并展示_利用PHP实现简单留言板
跟隨bilibili教學(xué)視頻一步一步實(shí)現(xiàn)簡(jiǎn)單的PHP留言板。
第一個(gè)代碼文件item.php,創(chuàng)建了一個(gè)Message,并連接了數(shù)據(jù)庫(kù)。
class Message
{
var $name;
var $time;
var $content;
function __construct($n, $t, $c)
{
$this->name = $n;
$this->time = $t;
$this->content = $c;
}
function show()
{
echo "名字:" . $this->name . "
";
echo "時(shí)間:" . $this->time . "
";
echo "內(nèi)容:" . $this->content . "
";
echo "=================================" . "
";
}
}
class DB
{
var $database = null;
function __construct()
{
$dbhost = "localhost";
$account = "wang";
$password = "123456";
$dataform = "db_messages";
$this->database = mysqli_connect($dbhost, $account, $password, $dataform);
// if($this->database)
// {
// echo "連接數(shù)據(jù)庫(kù)成功";
// }
}
function __destruct()
{
mysqli_close($this->database);
}
}
第二個(gè)代碼文件messageBoard.php,將上面那個(gè)部分引用進(jìn)來(lái),并且做出相應(yīng)的工作。
include_once('item.php'); //引入一次item.php
class MessageBoard extends DB //讓DB類(lèi)可以執(zhí)行下去
{
var $messages = array();
function __construct()
{
parent::__construct(); //DB的構(gòu)造子
$this->receiveMessage(); //接收并保存數(shù)據(jù)
$this->loadData(); //在加載數(shù)據(jù)庫(kù)里已經(jīng)有的數(shù)據(jù)到一個(gè)數(shù)組里面
$this->showAllMessages(); //展示數(shù)組里所有東西到頁(yè)面中
$this->showForm(); //展示表單
}
function receiveMessage()
{
if (count($_POST) != 0) {
$this->saveData($_POST['userName'], date("Y-m-d h:i:s", time()), $_POST['content']);
}
//總秒數(shù)time()
}
function loadData()
{
$query = "SELECT * FROM `all_messages`";
$result = mysqli_query($this->database, $query);
while ($row = mysqli_fetch_array($result)) {
$temp = new Message($row['name'], $row['time'], $row['content']);
array_push($this->messages, $temp);
}
}
function saveData($u, $t, $c)
{
$query = "INSERT INTO `all_messages`( `name`, `time`, `content`) VALUES ('$u','$t','$c')";
mysqli_query($this->database, $query);
}
function showAllMessages()
{
foreach ($this->messages as $m) {
$m->show();
}
}
function showForm()
{
echo "
";echo "名字:" . "" . "
";
echo "內(nèi)容:" . "" . "
";
echo "";
echo "
";}
}
$mb = new MessageBoard();
大家可以試一下,就是一個(gè)簡(jiǎn)易的留言板,包括數(shù)據(jù)的輸入,到數(shù)據(jù)庫(kù)的存儲(chǔ),然后數(shù)據(jù)庫(kù)的提取,最后到顯示,一個(gè)比較完整的流程,適合新手參考。有問(wèn)題可以shi'liao
總結(jié)
以上是生活随笔為你收集整理的PHP留言并展示_利用PHP实现简单留言板的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算ln_钢筋工程算量培训-钢筋工程量计
- 下一篇: lisp对excel其他行列写入_【极简