แสดงที่ละ 10 บรรทัด/หน้า


<?php

//include("db.php");

$conn = new PDO("mysql:host=localhost; dbname=oscommerce","root","");

$conn->query("set names utf8");

?>

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>Products</title>

</head>

<body>

<h1>Products<br>

</h1>

<table width="600" border="0" cellspacing="0" cellpadding="5">

<tr bgcolor="#33FF66">

<th scope="col">ID</th>

<th scope="col"><a href="index1.php?sort=name">Model</a></th>

<th scope="col"><a href="index1.php?sort=name">Name</a></th>

<th scope="col"><a href="index1.php?sort=price">Price</a></th>

</tr>

<?php

$sql="select count(*) as total

fromproducts ";

$stmt=$conn->query($sql);

$row=$stmt->fetch();

$totalrecord=$row['total']; // หาจำนวนหน้าทั้งหมด

if($_GET['sort']=="model")

$sortField="pd.products_model";

elseif($_GET['sort']=="name")

$sortField="pd.products_name";

elseif($_GET['sort']=="price")

$sortField="p.products_price";

else

$sortField="p.products_id";

$sql="select p.products_id,

p.products_model,

pd.products_name,

p.products_price

fromproducts as p

joinproducts_description as pd

onp.products_id=pd.products_id

where pd.language_id=1

order by $sortField";

$pagesize=8; //กำหนดแสดง record/page

$totalpage=ceil($totalrecord/$pagesize); //คำนวนหน้าทั้งหมด

$page=intval($_GET['page']);

if(empty($page)) // ตรวจหน้า page ถ้าเป็นค่าว่าง

{

$page=1;

}

$start=($page-1)*$pagesize;

$sql.=" limit $start, $pagesize"; //แสดง record/หน้า

$stmt=$conn->query($sql);

while($row=$stmt->fetch())

{

?>

<tr bgcolor="#D5FFEA">

<td align="center"><?=$row[0]?></td>

<td><?=$row[1]?></td>

<td><?=$row[2]?></td>

<td align="right"><?=number_format($row[3],2)?></td>

</tr>

<?

}

?>

</table>

<?php //แสดงหน้า Page กด Link

for ($i=1;$i<=$totalpage;$i++)

{

if($i==$page)

print("<b>$i</b> ");

else

print("<a href='{$_SERVER['PHP_SELF']}?page=$i&sort={$_GET['sort']}'>$i</a> ");

}

?>

</body>

</html>

หมายเลขบันทึก: 600089เขียนเมื่อ 28 มกราคม 2016 12:05 น. ()แก้ไขเมื่อ 28 มกราคม 2016 13:09 น. ()สัญญาอนุญาต: ครีเอทีฟคอมมอนส์แบบ แสดงที่มา-ไม่ใช้เพื่อการค้า-ไม่ดัดแปลงจำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (0)

ไม่มีความเห็น

พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท