前面已經建立好一個簡單的書局資料表了,接下來要做一個簡易的書局後台網站
首先先建立連線
<?php
//PDO version
$servername = "192.168.64.35";
$username = "XXXXX";
$password = "XXXXX";
$dbName = "testdb";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbName", $username, $password);
// set the PDO error mode to exception //PDO::ATTR_ERRMODE 錯誤報告,PDO::ERRMODE_EXCEPTION 表示 PDO 生成的錯誤
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
echo "<br>";
} catch(PDOException $e) {//表示 PDO 生成的錯誤
echo "Connection failed: " . $e->getMessage();//getMessage —獲取異常信息
}
?>
接下來製作首頁
<?php
require_once 'connecttest2.php';
$sql = "SELECT ID,NAME,BOOKNAME,CASH,DATETIME FROM bookstore ORDER BY ID DESC";
$result = $conn->prepare($sql);
$result->execute();
$numRows = $result->rowCount();
$perpage=5; // 每頁顯示 5 筆
$totalpage=ceil($numRows/$perpage); // 計算總頁數
echo "<html>";
echo "<head>";
echo "<title>書本查詢</title>";
echo "<meta charset='UTF-8'>";
echo "<style>";
echo "table,th,td {border:1px solid black; width:80%;text-align:center}";
echo "#h1,#h2,#h3,#h4,#h5,#h6,#h7 {width:15%}";
echo "</style>";
echo "</head>";
echo "</html>";
echo "<a href='bookstoreadd.php'>新增 </a>";
echo "<br>";
echo "<a href='bookstoresearch.php'>搜尋 </a>";
echo " ,共 $numRows 筆";
echo "<br>";
// 根據 $_GET['page'] 參數值決定從第幾頁開始顯示
// 代表頁次的變數 $page 由 1 起算//empty確認是否為空值//!is_numeric確認是否為整數
if(empty($_GET['page']) || !is_numeric($_GET['page']) || $_GET['page']<1 || $_GET['page']>$totalpage )
{$page=1;}
else{$page=$_GET['page'];}
//
// 根據 $_GET['order'] 參數值決定排序方式
if(empty($_GET['order']) || !is_numeric($_GET['order'])
|| $_GET['order']<1 || $_GET['order']>4 ) {
$field='ID'; // SQL 查詢時的排序參數
$order=0; // 建立頁次連結時使用的參數
}
else if($_GET['order']==1) {
$field='NAME';
$order=1;
}
else if($_GET['order']==2) {
$field='BOOKNAME';
$order=2;
}
else if($_GET['order']==3) {
$field='CASH';
$order=3;
}
else if($_GET['order']==4) {
$field='DATETIME';
$order=4;
}
//
//
echo "<table>";
echo "<tr>";
echo "<td id ='h1'><a href='bookstore.php?order=0'>編號</a></td>";
echo "<td id ='h2'><a href='bookstore.php?order=1'>作者</a></td>";
echo "<td id ='h3'><a href='bookstore.php?order=2'>書名</a></td>";
echo "<td id ='h4'><a href='bookstore.php?order=3'>金額</a></td>";
echo "<td id ='h5'><a href='bookstore.php?order=4'>時間</a></td>";
echo "<td id ='h6'>OPTION</td>";
echo "<td id ='h7'>OPTION</td>";
echo "</tr>";
// 設定查詢 LIMIT 子句的第 1 個參數
$start=($page-1)*$perpage;
$sql = "SELECT ID,NAME,BOOKNAME,CASH,DATETIME FROM bookstore ORDER BY $field DESC LIMIT $start,$perpage";
$result2 = $conn->prepare($sql);
$result2 ->execute();
while( $row = $result2->fetch(PDO::FETCH_NUM))
{
echo "<tr>";
echo "<td id ='h1'>$row[0]</td>";
echo "<td id ='h2'>$row[1]</td>";
echo "<td id ='h3'>$row[2]</td>";
echo "<td id ='h4'>$row[3]</td>";
echo "<td id ='h5'>$row[4]</td>";
echo "<td id ='h6'><a href='bookstoredelete.php?del={$row[0]}'>刪除</a></td>";
echo "<td id ='h7'><a href='bookstoreedit.php?edit={$row[0]}'>編輯</a></td>";
echo "</tr>";
}
echo "</table>";
//
// 輸出直接跳頁的連線
//
for($i=1;$i<=$totalpage;$i++){
if($i!=1)
{ echo " ";}//不換行空格
if($i==$page)
{echo $i;}
else
//$_SERVER['PHP_SELF']:當前正在執行的網頁檔案名稱
{echo sprintf('<a href="%s?page=%d&order=%d"> %d </a>',$_SERVER['PHP_SELF'], $i ,$order, $i);}
}
//
?>
成功執行後如下圖
留言列表