125 lines
3.3 KiB
PHP

<?php
namespace MessageBoard\Database;
use PDO;
require_once FOLDER_ROOT.'/lib/Database/Database.php';
class DbMessage extends Database
{
/**
* 取得列表
*
* @param integer $page 第幾筆開始
* @param integer $count 此查詢要列出幾筆
* @return array
*/
public function getList(int $page=null, int $count=null)
{
$sqlString = "SELECT id,title,content,updated_at,created_at FROM `".$this->table('message')."`";
// 設定分頁功能
if(!empty($page) && !empty($count)) {
$sqlString .= ' LIMIT :offset ,:length';
$query = $this->connDB->prepare($sqlString);
$offset = ($page-1) * $count;
$query->bindParam(":offset", $offset, PDO::PARAM_INT);
$query->bindParam(":length", $count, PDO::PARAM_INT);
$query->execute();
}
else {
$query = $this->connDB->prepare($sqlString);
$query->execute();
}
$queryResultAll = $query->fetchAll();
return $queryResultAll;
}
/**
* 取得所有資料的筆數
*
* @return int
*/
public function getCount()
{
$sqlString = "SELECT count(*) FROM `".$this->table('message')."`";
return (int)($this->connDB->query($sqlString))->fetch()[0];
}
/**
* 取得單筆資料
*
* @param int $id
* @return array
*/
public function getDataByid($id)
{
$sqlString = "SELECT id,title,content,updated_at,created_at FROM `".$this->table('message')."` WHERE id = :id";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $id);
$query->execute();
$queryResultAll = $query->fetch();
return $queryResultAll;
}
/**
* 建立資料
*
* @param string $title 標題
* @param string $content 內容
* @return int 建立後的ID
*/
public function insert($title, $content)
{
$sqlString = "INSERT INTO `".$this->table('message')."` (title,content) VALUES ( :title, :content )";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":title", $title);
$query->bindParam(":content", $content);
$query->execute();
return $this->connDB->lastInsertId();
}
/**
* 編輯
*
* @param int $id 編號
* @param string $title 標題
* @param string $content 內容
* @return int 是否有成功
*/
public function edit($id, $title, $content)
{
$sqlString = "UPDATE `".$this->table('message')."` SET content= :content ,title= :title WHERE id= :id";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $id);
$query->bindParam(":title", $title);
$query->bindParam(":content", $content);
$query->execute();
return $query->rowCount();
}
/**
* 移除
*
* @param int $id 編號
* @return int 是否有成功
*/
public function delete($id)
{
$sqlString = "DELETE FROM `".$this->table('message')."` WHERE id= :id";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $id);
$query->execute();
return $query->rowCount();
}
}