具有分頁功能的getList

This commit is contained in:
Yuan Chiu 2020-04-09 22:02:32 +08:00
parent 46479a9353
commit 55f10909b4

View File

@ -2,6 +2,8 @@
namespace MessageBoard\Database;
use PDO;
require_once FOLDER_ROOT.'/lib/Database/Database.php';
class DbMessage extends Database
@ -10,21 +12,44 @@ class DbMessage extends Database
/**
* 取得列表
*
* @param integer $limit 第幾筆開始
* @param integer $page 第幾筆開始
* @param integer $count 此查詢要列出幾筆
* @return void
* @return array
*/
public function getList(int $limit=null, int $count=null)
public function getList(int $page=null, int $count=null)
{
$sqlString = "SELECT * FROM `".$this->table('message')."`";
$query = $this->connDB->prepare($sqlString);
$query->execute();
$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];
}
/**
* 取得單筆資料
*
@ -33,7 +58,7 @@ class DbMessage extends Database
*/
public function getDataByid($id)
{
$sqlString = "SELECT * FROM `".$this->table('message')."` WHERE id = :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);
@ -83,6 +108,12 @@ class DbMessage extends Database
return $query->rowCount();
}
/**
* 移除
*
* @param int $id 編號
* @return int 是否有成功
*/
public function delete($id)
{
$sqlString = "DELETE FROM `".$this->table('message')."` WHERE id= :id";