|
|
|
จากโค๊ดแสดงข้อมูลตาม ABC ของพี่ PlaKrim ถ้าจะใช้โค๊ดแบ่งหน้าด้วย ต้องเพิ่มยังไงครับ |
|
|
|
|
|
|
|
เอาโค๊ดนี้ไปนะครับใส่หลัง $sql ="select
$sqlquery = mysql_query($sql) or die ("error [".$sql."]");///เริ่มแบ่งหน้า
$Num_Rows = mysql_num_rows($sqlquery);
$Per_Page = 20; // จำนวนข้อมูล
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$sql .=" order by id LIMIT $Page_Start , $Per_Page";///จบส่วนการแบ่งหน้า
ส่วนหลังนี้จะเป็น output แบ่งหน้าเอาไปประยุกต์เอาเองนะครับ ใส่หลัง while ไปแล้วก็ได้ครับ หลัง }
echo "<table width=600 heigth=25 border=1 cellpadding=0 cellspacing=0 bordercolor=#FFFFFF bgcolor=#E983DE>
<tr>
<td heigth=25 scope=col align=right class=style5 valign=middle>$Num_Rows ข้อมูล $Num_Pages หน้า ";
if($Prev_Page)
{
echo "<a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'></a>";
}
for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "<a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo "<a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'></a></td>
</tr>
</table>";
}
echo "<hr color=\"#F7F7F7\">";
mysql_close($dbcon);
?>
|
|
|
|
|
Date :
2010-06-08 22:34:36 |
By :
Dragons_first |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คือผมอยากได้ตัวแบ่งหน้าเหมือนของพี่ PlakriM หน่ะครับ ตอนนี้ตัวแบ่งหน้ามันเยอะ 700 กว่า record มันแบ่งหน้าให้เยอะเลยครับ 20 กว่าหน้า จะเขียนโค้ดแบ่งหน้า กับ โค้ดของผมได้ยังไงบ้างอะคับ ขอบคุณล่วงหน้าเน้อ
|
|
|
|
|
Date :
2011-07-05 16:25:10 |
By :
maruk |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
กระทู้ข้ามปี...
|
|
|
|
|
Date :
2011-07-05 16:31:58 |
By :
ไวยวิทย์ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไก่หรอ แบ่ง cat แล้วจะแบ่งหน้าอีกใช่ปะ
|
|
|
|
|
Date :
2011-07-05 16:47:26 |
By :
PlaKriM |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
555 กระทู้ข้ามปีจริงๆ 5555
พี่ PlakriM ถูกต้องแล้วคร๊าบบบพี่
|
ประวัติการแก้ไข 2011-07-05 17:06:55
|
|
|
|
Date :
2011-07-05 16:58:48 |
By :
maruk |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมลองทำตามบทความนี้ครับ เรืองการแบ่งหน้า https://www.thaicreate.com/community/php-mysql-pagination.html
อันนี้คือแยกหน้าให้ แต่พอคลิกให้แสดงตามตัวอักษร มัน error ตามรูปแบบนี้ เวลา search ก็เป็นเหมือนกันครับ
อันนี้โค๊ดครับ
Code (PHP)
<?PHP
class Paginator{
var $items_per_page;
var $items_total;
var $current_page;
var $num_pages;
var $mid_range;
var $low;
var $high;
var $limit;
var $return;
var $default_ipp;
var $querystring;
var $url_next;
function Paginator()
{
$this->current_page = 1;
$this->mid_range = 7;
$this->items_per_page = $this->default_ipp;
$this->url_next = $this->url_next;
}
function paginate()
{
if(!is_numeric($this->items_per_page) OR $this->items_per_page <= 0) $this->items_per_page = $this->default_ipp;
$this->num_pages = ceil($this->items_total/$this->items_per_page);
if($this->current_page < 1 Or !is_numeric($this->current_page)) $this->current_page = 1;
if($this->current_page > $this->num_pages) $this->current_page = $this->num_pages;
$prev_page = $this->current_page-1;
$next_page = $this->current_page+1;
if($this->num_pages > 10)
{
$this->return = ($this->current_page != 1 And $this->items_total >= 10) ? "<a class=\"paginate\" href=\"".$this->url_next.$this->$prev_page."\">« Previous</a> ":"<span class=\"inactive\" href=\"#\">« Previous</span> ";
$this->start_range = $this->current_page - floor($this->mid_range/2);
$this->end_range = $this->current_page + floor($this->mid_range/2);
if($this->start_range <= 0)
{
$this->end_range += abs($this->start_range)+1;
$this->start_range = 1;
}
if($this->end_range > $this->num_pages)
{
$this->start_range -= $this->end_range-$this->num_pages;
$this->end_range = $this->num_pages;
}
$this->range = range($this->start_range,$this->end_range);
for($i=1;$i<=$this->num_pages;$i++)
{
if($this->range[0] > 2 And $i == $this->range[0]) $this->return .= " ... ";
if($i==1 Or $i==$this->num_pages Or in_array($i,$this->range))
{
$this->return .= ($i == $this->current_page And $_GET['Page'] != 'All') ? "<a title=\"Go to page $i of $this->num_pages\" class=\"current\" href=\"#\">$i</a> ":"<a class=\"paginate\" title=\"Go to page $i of $this->num_pages\" href=\"".$this->url_next.$i."\">$i</a> ";
}
if($this->range[$this->mid_range-1] < $this->num_pages-1 And $i == $this->range[$this->mid_range-1]) $this->return .= " ... ";
}
$this->return .= (($this->current_page != $this->num_pages And $this->items_total >= 10) And ($_GET['Page'] != 'All')) ? "<a class=\"paginate\" href=\"".$this->url_next.$next_page."\">Next »</a>\n":"<span class=\"inactive\" href=\"#\">» Next</span>\n";
}
else
{
for($i=1;$i<=$this->num_pages;$i++)
{
$this->return .= ($i == $this->current_page) ? "<a class=\"current\" href=\"#\">$i</a> ":"<a class=\"paginate\" href=\"".$this->url_next.$i."\">$i</a> ";
}
}
$this->low = ($this->current_page-1) * $this->items_per_page;
$this->high = ($_GET['ipp'] == 'All') ? $this->items_total:($this->current_page * $this->items_per_page)-1;
$this->limit = ($_GET['ipp'] == 'All') ? "":" LIMIT $this->low,$this->items_per_page";
}
function display_pages()
{
return $this->return;
}
}
?>
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<style type="text/css">
<!--
.paginate {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
}
a.paginate {
border: 1px solid #000080;
padding: 2px 6px 2px 6px;
text-decoration: none;
color: #000080;
}
h2 {
font-size: 12pt;
color: #003366;
}
h2 {
line-height: 1.2em;
letter-spacing:-1px;
margin: 0;
padding: 0;
text-align: left;
}
a.paginate:hover {
background-color: #000080;
color: #FFF;
text-decoration: underline;
}
a.current {
border: 1px solid #000080;
font: bold .7em Arial,Helvetica,sans-serif;
padding: 2px 6px 2px 6px;
cursor: default;
background:#000080;
color: #FFF;
text-decoration: none;
}
span.inactive {
border: 1px solid #999;
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
padding: 2px 6px 2px 6px;
color: #999;
cursor: default;
}
-->
</style>
<style type="text/css">
ol {
padding:0px;
margin:0px;
}
li {
list-style:none;
float:left;
background:#eee;
border:solid 1px #ccc;
font:13px tahoma;
text-align:center;
vertical-align:middle;
margin-right:2px;
padding:2px 5px;
cursor:pointer;
}
</style>
<body>
<?php
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("db_satisfy");
$strSQL = "SELECT * FROM customer where 1=1 ";
if(isset($_GET["text"])){
$strSQL .= " and CompanyName_EN like '" . $_GET["text"] . "%';";
}
if(isset($_GET["find"]) and !empty($_GET["find"])){
$strSQL .= " and CompanyName_TH like '%" . $_GET["find"] . "%';";
}
//echo $strSQL;
$char = "abcdefghijklmnopqrstuvwxyz";
$navi = str_split($char);
$find = (isset($_GET["find"]) and !empty($_GET["find"]))? "&find=" . $_GET["find"] : "";
echo "<ol>";
foreach($navi as $text){
echo "<li onclick=\"location.href='?text=" . strtoupper($text) . $find . "'\">" . strtoupper($text) . "</li>";
}
echo "</ol>";
echo "<br clear=\"all\" />";
echo "<form id=\"form1\" name=\"form1\" method=\"get\" action=\"\">";
echo "<input type=\"text\" name=\"find\" id=\"find\" value=\"" . $_GET["find"] . "\" />";
echo "<input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" />";
echo "</form>";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
//---------------------------
$Per_Page = 10; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$strSQL .=" order by CustomerID ASC LIMIT $Page_Start , $Per_Page";
$objQuery = mysql_query($strSQL);
//---------------------------
?>
<table width="80%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="5%">no</td>
<td width="49%">company name EN</td>
<td width="49%">company name TH</td>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["CompanyName_EN"];?></td>
<td><?=$objResult["CompanyName_TH"];?></td>
</tr>
<?
}
?>
</table>
<br>
Total <?= $Num_Rows;?> Record
<?
$pages = new Paginator;
$pages->items_total = $Num_Rows;
$pages->mid_range = 10;
$pages->current_page = $Page;
$pages->default_ipp = $Per_Page;
$pages->url_next = $_SERVER["PHP_SELF"]."?QueryString=value&Page=";
$pages->paginate();
echo $pages->display_pages();
mysql_close($objConnect);
?>
ช่วยแนะนำหน่อยครับพี่ๆ ขอบคุณล่วงหน้าเน้อ(ก็หวังว่าจะไม่ข้ามปีอีก อิอิ)
|
ประวัติการแก้ไข 2011-07-06 16:44:12 2011-07-06 16:48:32
|
|
|
|
Date :
2011-07-06 16:38:54 |
By :
maruk |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จัดให้แล้วนะไก่เอ้ย
Test Db :
Quote:CREATE TABLE IF NOT EXISTS `table_name` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`field1` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `table_name` (`id`, `field1`) VALUES
(1, 'aabbccddee'),
(2, 'aaffeecciibb'),
(3, 'bbiioowwss'),
(4, 'dduuyyttrrii'),
(5, 'uuyyeeww'),
(6, 'ppooxxzzqq'),
(7, 'ddooiiuuyytt'),
(8, 'uuppllkkmm'),
(9, 'ppeewwaa'),
(10, 'qqeerrttmm'),
(11, 'aassffgghh'),
(12, 'aallkkhhgg'),
(13, 'bbppoottee'),
(14, 'ddmmvvxx'),
(15, 'xxkkiiuuee'),
(16, 'zzhhggfftt'),
(17, 'xxkkiiuuee'),
(18, 'ssuukkeehh'),
(19, 'ddyywwmm');
Demo Code :
Code (PHP)
<style type="text/css">
ol{
padding:0px;
margin:0px;
}
li{
list-style:none;
float:left;
background:#eee;
border:solid 1px #ccc;
font:13px tahoma;
text-align:center;
vertical-align:middle;
margin-right:2px;
padding:2px 5px;
cursor:pointer;
}
</style>
<?php
// แบ่งหน้าง่ายๆ สไตปลากิรม จริงๆ มี class เดี๋ยวปรับให้ดีกว่านี้ก่อนค่อยปล่อย
function page_navi($page, $limit, $range, $count, $send=NULL, $current_style="navi_on", $other_style="navi_out", $target="_self"){
$output = "";
$total = ceil($count/$limit);
$navi_start = $page-$range;
$navi_end = $page+$range;
$send .= (!empty($send))? "&" : NULL;
if($navi_start <= 0) $navi_start = 1;
if($navi_end >= $total) $navi_end = $total;
if($page>1){
$navi_back = $page-1;
if($page > 2)
$output .= "<a href=\"?" . $send . "page=1\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>«</strong></a> ";
$output .= "<a href=\"?" . $send . "page=" . $navi_back . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>‹</strong></a> ";
}
for($i = $navi_start; $i <= $navi_end; $i++){
if($i == $page)
$output .= "<a href=\"?" . $send . "page=" . $i . "\" target=\"" . $target . "\" class=\"" . $current_style . "\"><strong><font color=\"" . $other_style . "\">$i</font></strong></a> ";
else
$output .= "<a href=\"?" . $send . "page=" . $i . "\" target=\"" . $target . "\" class=\"" . $other_style . "\">$i</a> ";
}
if($page < $total){
$navi_next = $page+1;
$output .= "<a href=\"?" . $send . "page=" . $navi_next . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>›</strong></a> ";
if(($page+1) < $total)
$output .= "<a href=\"?" . $send . "page=" . $total . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>»</strong></a>";
}
if($navi_start>$navi_end)
$output .= "<a href=\"?" . $send . "page=" . $page . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>$page</strong></a> ";
return $output;
}
// db connection
$dbconn = mysql_connect("localhost:3308","root","root") or die(mysql_error());
$dbname = "iamsoftmaker";
mysql_db_query($dbname, "set character_set_results = utf8" , $dbconn) or die(mysql_error());
mysql_db_query($dbname, "set names utf8" , $dbconn) or die(mysql_error());
// category by char
$char = "abcdefghijklmnopqrstuvwxyz";
$navi = str_split($char);
$find = (isset($_GET["find"]) and !empty($_GET["find"]))? "&find=" . $_GET["find"] : "";
echo "<ol>";
foreach($navi as $text){
echo "<li onclick=\"location.href='?text=" . strtoupper($text) . $find . "'\">" . strtoupper($text) . "</li>";
}
echo "</ol>";
// form search
echo "<br clear=\"all\" />";
echo "<form id=\"form1\" name=\"form1\" method=\"get\" action=\"\">";
echo "<input type=\"text\" name=\"find\" id=\"find\" value=\"" . $_GET["find"] . "\" />";
echo "<input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" />";
echo "</form>";
// คำนวณการแบ่งหน้า
$page = (isset($_GET['page']))? intval($_GET['page']) : 1;
$limit_end = 2; // กี่ record ต่อหน้า
$limit_start = ($page-1)*$limit_end;
$send = "";
$sql = "select * from table_name where 1=1";
if(isset($_GET["text"])){
$send = "text=" . $_GET["text"]; //ส่งไปด้วยถ้าจะแบ่งหน้า
$sql .= " and field1 like '" . $_GET["text"] . "%'";
}
if(isset($_GET["find"]) and !empty($_GET["find"])){
$send .= "find=" . $_GET["find"]; //ส่งไปด้วยถ้าจะแบ่งหน้า
$sql .= " and field1 like '%" . $_GET["find"] . "%'";
}
echo '<br>' . $sql . " limit " . $limit_start. "," . $limit_end . '<br>';
$result = mysql_query($sql . " limit " . $limit_start. "," . $limit_end, $dbconn);
$count = mysql_num_rows($result);
$total = mysql_num_rows(mysql_query($sql, $dbconn));
if($total > 0){
while($rows = mysql_fetch_assoc($result)){
echo '<br>' . $rows['field1'];
}
echo "<br clear=\"all\" />";
echo "\n<div style=\"float:left;\">Page : " . page_navi($page, $limit_end, 4, $total, $send) . " </div><div style=\"float:right;\">[" . ($limit_start+1) . "-" . ($limit_start+$count) . "] of <strong>" . $total . "</strong></div>";
}
?>
|
|
|
|
|
Date :
2011-07-13 01:05:38 |
By :
PlaKriM |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณพี่ PlakriM มากๆเลยคร๊าาาบบ โมดูลนี้ไม่ข้ามปีแล้วววว...เย้
|
|
|
|
|
Date :
2011-07-13 09:51:26 |
By :
maruk |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|