This commit is contained in:
Yuan Chiu 2020-04-04 18:17:47 +08:00
commit b76905eb28
11 changed files with 495 additions and 0 deletions

8
.editorconfig Normal file
View File

@ -0,0 +1,8 @@
root = true
[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
htdocs/config.php

22
Readme.md Normal file
View File

@ -0,0 +1,22 @@
JGB Yuan 練習作業
===
# 安裝方法
待補充
# 檔案結構
* index.php 列表(包含分頁)
* edit.php 編輯
* view.php 瀏覽
# 題目要求
下週五code review的小作業請使用原生寫法(不得使用任何框架),用 html + css + php + mysql 寫出簡易的留言版系統
要求:
1. 有列表,帶分頁(分頁形式不拘)
2. 新增/編輯/刪除留言功能
3. 資料存在MySQL
4. css盡量使用畫面別太醜
本機沒環境的話請裝xampp for mac

BIN
htdocs/.DS_Store vendored Normal file

Binary file not shown.

57
htdocs/config.sample.php Normal file
View File

@ -0,0 +1,57 @@
<?php
// 資料庫 =====================================================================
/**
* 資料庫位址
*/
define('DB_HOST', 'localhost');
/**
* 資料庫連結埠
*
* usually 5432 for PostgreSQL, 3306 for MySQL
*/
define('DB_PORT', '');
/**
* 資料庫連結帳號
*/
define('DB_USER', 'user');
/**
* 資料庫連結密碼
*/
define('DB_PASS', 'passwd123');
/**
* 資料庫名稱
*/
define('DB_NAME', 'yuanjgb-mb');
/**
* 資料庫內資料表前綴字串
*
* 每一張表格名稱的起始字串,為了避開一些網頁空間只允許建立一個資料庫的限制。
*/
define('DB_PREFIX', 'mg__');
// 網站設定 ===================================================================
/**
* 網站標題
*/
define('SITE_NAME', 'JGB留言板');
/**
* 網站首頁網址
*
* Warning: 網址後面務必加上"/"
*/
define('SITE_URL', 'http://localhost/');
// 路徑設定 ===================================================================
/**
* 網站根目錄
*/
define('FOLDER_ROOT', __DIR__);

0
htdocs/css/here Normal file
View File

83
htdocs/index.php Normal file
View File

@ -0,0 +1,83 @@
<?php
require_once 'config.php';
require_once FOLDER_ROOT.'/lib/Database/DbMessage.php';
$list = [
[
'id' => 1,
'title' => 'text',
'updated_at' => '2020-04-01 12:13',
],
[
'id' => 2,
'title' => 'text',
'updated_at' => '2020-04-01 12:13',
],
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<header>
<h1>留言板</h1>
</header>
<div id="main">
<div class="func-btn">
<ul>
<li><a href="edit.php" class="btn">New</a></li>
</ul>
</div>
<section>
<!-- 主表格 -->
<?php
if (count($list) > 0) {
echo '
<table>
<thead>
<tr>
<th>id</th>
<th>title</th>
<th>updated_at</th>
<th>action</th>
</tr>
</thead>
<tbody>
';
foreach ($list as $key => $item) {
echo '
<tr>
<th>id</th>
<td>title</td>
<td>updated_at</td>
<td>
<ul>
<li><a href="" class="btn">Edit</a></li>
<li><a href="" class="btn">Delete</a></li>
</ul>
</td>
</tr>
';
}
echo '
</tbody>
</table>
';
}
else {
echo '無資料';
}
?>
</section>
</div>
</body>
</html>

BIN
htdocs/lib/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,151 @@
<?php
/**
* Database.php
*
* 此檔案針對整體資料庫的功能,像是建立此資料庫、建立表格、清空...等等
*
*/
namespace MessageBoard\Database;
require_once FOLDER_ROOT.'/lib/Database/MySQLDB.php';
/**
* 資料庫操作抽象類別
*
* 請根據一個資料表創建一個類別,並繼承此類別。
* 所有對於資料表的操作(包含查詢、新增、修改、刪除),一律使用新創已繼承的類別物件。
*
* 基本的操作方式例如:
*
* use MessageBoard\Database;
* $db = new Database\[資料表類別](array(
* 'type' => 'mysql',
* 'host' => 'localhost',
* 'port' => '3306',
* 'user' => 'user',
* 'password' => '123456',
* 'dbname' => 'chu-elearning',
* 'prefix' => 'chu_'
* ));
*
* 實際範例可參考 `DBAdmin` 類別的說明文件
*
* @author Yuan Chiu <chyuaner@gmail.com>
* @version 2.0.0
* @subpackage Database
*/
abstract class Database {
/**
* 資料庫伺服器位址
* @type string
*/
protected $db_host;
/**
* 資料庫伺服器連結埠
* @type string
*/
protected $db_port;
/**
* 資料庫帳號
* @type string
*/
protected $db_user;
/**
* 資料庫密碼
* @type string
*/
protected $db_passwd;
/**
* 資料庫名稱
* @type string
*/
protected $db_name;
/**
* 資料表前綴字元
* @type string
*/
protected $db_prefix;
// ------------------------------------------------------------------------
/**
* 資料庫連結物件
*/
protected $connDB;
// ========================================================================
/**
* 連接資料庫
*
* @param array $conf (optional) 資料庫相關參數,格式為:
* array( 'type' => 'mysql',
* 'host' => 'localhost',
* 'port' => '3306',
* 'user' => 'user',
* 'password' => '123456',
* 'dbname' => 'chu-elearning',
* 'prefix' => 'chu_' )
* 若不填寫將會直接使用設定在`config.php`的常數
*
* @throws MessageBoard\Exception\DatabaseNoSupportException
* @author Yuan Chiu <chyuaner@gmail.com>
* @since 2.0.0
*/
public function __construct($conf = null) {
// 將資料庫設定資訊帶入
if(isset($conf)) {
$this->db_host = $conf['host'];
$this->db_port = $conf['port'];
$this->db_user = $conf['user'];
$this->db_passwd = $conf['password'];
$this->db_name = $conf['dbname'];
$this->db_prefix = $conf['prefix'];
}
else {
$this->db_host = DB_HOST;
$this->db_port = DB_PORT;
$this->db_user = DB_USER;
$this->db_passwd = DB_PASS;
$this->db_name = DB_NAME;
$this->db_prefix = DB_PREFIX;
}
$this->connDB = new MySQLDB($this->db_name
, $this->db_host
, $this->db_port
, $this->db_user
, $this->db_passwd);
}
/**
* 轉為完整的資料表名稱(包含前綴字元)
*
* @param string $tableName 資料表名稱
* @return string 完整的資料表名稱
*
* @author Yuan Chiu <chyuaner@gmail.com>
* @since 2.0.0
*/
public function table($tableName) {
return $this->db_prefix.$tableName;
}
/**
* 測試資料庫有無連接成功
*
* @since 2.0.0
*/
public function connectTest() {
// TODO: Fill code in
}
}

View File

@ -0,0 +1,104 @@
<?php
namespace MessageBoard\Database;
require_once FOLDER_ROOT.'/lib/Database/Database.php';
class DbMessage extends Database
{
/**
* 取得列表
*
* @param integer $limit 第幾筆開始
* @param integer $count 此查詢要列出幾筆
* @return void
*/
public function getList(int $limit, int $count)
{
return [];
}
/**
* 取得單筆資料
*
* @param [type] $id
* @return void
*/
public function getDataByid($id)
{
$sqlString = "SELECT `UID`, `UPassword`, ".
"`group`.`GID`, `group`.`GName`, `class`.`CID`, `class`.`CName`, ".
"`UEnabled`, `UBuildTime`, `UModifyTime`, ".
"`LMode`, `MMode`, `UEnable_NoAppoint`, ".
"`UNickname`, `URealName`, `UEmail`, `UMemo` ".
"FROM `".$this->table('user')."` AS `user` ".
"LEFT JOIN `".$this->table('user_auth_group')."` as `group` ".
"ON `group`.`GID` = `user`.`GID`".
"LEFT JOIN `".$this->table('user_class')."` as `class` ".
"ON `class`.`CID` = `user`.`CID`".
"WHERE ".$where;
$query = $this->connDB->prepare($sqlString);
$query->execute();
$queryResultAll = $query->fetchAll();
return $queryResultAll;
// // 如果有查到一筆以上
// if( count($queryResultAll) >= 1 ) {
// // 製作回傳結果陣列
// $result = array();
// foreach($queryResultAll as $key => $thisResult) {
// if($thisResult['UEnabled'] != '0') {
// $output_enable = true;
// }
// else { $output_enable = false; }
// if($thisResult['UEnable_NoAppoint'] != '0') {
// $output_enable_noAppoint = true;
// }
// else { $output_enable_noAppoint = false; }
// array_push($result,
// array( 'user_id' => $thisResult['UID'],
// 'password' => $thisResult['UPassword'],
// 'group_id' => $thisResult['GID'],
// 'group_name' => $thisResult['GName'],
// 'class_id' => $thisResult['CID'],
// 'class_name' => $thisResult['CName'],
// 'enable' => $output_enable,
// 'build_time' => $thisResult['UBuildTime'],
// 'modify_time' => $thisResult['UModifyTime'],
// 'learnStyle_mode' => $thisResult['LMode'],
// 'material_mode' => $thisResult['MMode'],
// 'enable_noAppoint' => $output_enable_noAppoint,
// 'nickname' => $thisResult['UNickname'],
// 'realname' => $thisResult['URealName'],
// 'email' => $thisResult['UEmail'],
// 'memo' => $thisResult['UMemo'])
// );
// }
// return $result;
// }
// else {
// return null;
// }
}
public function insert(Type $var = null)
{
# code...
}
public function edit()
{
# code...
}
public function delete($id)
{
# code...
}
}

View File

@ -0,0 +1,69 @@
<?php
/**
* MySQLDB.php
*
* 有用到的Define:
* DB_NAME, DB_HOST, DB_USER, DB_PASS
*/
namespace MessageBoard\Database;
use \PDO;
/**
* 資料庫連接專用類別採用自PDO
*
* @extends PDO
* @author Yuan Chiu <chyuaner@gmail.com>
* @version 2.0.0
* @see https://github.com/shuliu/myPDO
* @subpackage Database
*/
class MySQLDB extends PDO {
/**
* 連結資料庫
*
* @param string $dbname 資料庫名稱
* @param string $host 資料庫伺服器位址
* @param int $port 資料庫伺服器連接埠
* @param string $user 資料庫伺服器帳號
* @param string $passwd 資料庫伺服器密碼
* @author Yuan Chiu <me@yuaner.tw>
* @since 2.0.0
*/
public function __construct($dbname, $host, $port, $user, $passwd){
parent::__construct('mysql:dbname='.$dbname
.';host='.$host.';port='.$port
.';charset=utf8', DB_USER, DB_PASS);
//配合PHP< 5.3.6 PDO沒有charset用的
//參考: http://gdfan1114.wordpress.com/2013/06/24/php-5-3-6-%E7%89%88-pdo-%E9%85%8D%E5%90%88%E5%AD%98%E5%8F%96%E8%B3%87%E6%96%99%E5%BA%AB%E6%99%82%E7%9A%84%E4%B8%AD%E6%96%87%E5%95%8F%E9%A1%8C/
$this->exec('set names utf8');
}
// ========================================================================
/**
* 錯誤訊息的陣列
*
* 改寫Adodb -> ErrorMsg
*
* @access public
* @return array 錯誤訊息
*
* @since 2.0.0
* @author shuliu <https://github.com/shuliu>
* @see https://github.com/shuliu/myPDO/blob/master/PDO.class.php
*/
public function errorMsg(){
$err = parent ::errorinfo();
if( $err[0]!='00000' ){
return array('errorCode'=>$err[0]
,'number'=>$err[1]
,'message'=>$err[2]);
}else{
return null;
}
}
}