|
|
|
รบกวนช่วยแก้ไขโค๊ดแบ่งหน้าตัวนี้หน่อยค่ะ ใส่โค๊ดแล้วแต่ไม่ทำงาน |
|
|
|
|
|
|
|
พอดีได้โค๊ดจากเว็บนี้ไปทำโค๊ดแบ่งหน้าค่ะ แ่ต่พอเอาใส่ไปแล้ว ข้ัอมูลก็ยังขึ้นมาหมดอยู่ดี
ในตัวอย่างอยากให้โชว์ข้อมูลที่ละ 2 แล้วแบ่งหน้าต่อไปน่ะค่ะ แก้ไขยังไงดีคะ รบกวนดูโค๊ดให้ทีค่ะ
Code (PHP)
<?php
$id_type=$_GET['id_type'];
include ("news/function.php");
include("news/config.inc.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 > 2)
{
$this->return = ($this->current_page != 1 And $this->items_total >= 2) ? "<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 >= 2) 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><style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
body,td,th {
font-family: sans-serif;
font-size: 14px;
}
-->
<!--
.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>
<body>
<?
//$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("db_news");
$strSQL = "select * from news where type = '$id_type' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 2; // 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 type ASC LIMIT $Page_Start , $Per_Page";
$objQuery = mysql_query($strSQL);
?>
<table width="973" height="100%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="1014" height="350" valign="top"><table width="85" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="255" valign="top" bgcolor="#FFFFFF"><div align="left"><img src="ForPicture/Head/Header.jpg" width="972" height="250"></div></td>
</tr>
<tr>
<td height="31" valign="top" bgcolor="#FFFFFF">
<?php
echo "$newtype[$id_type] <br>";
$sql = "select * from news where type = '$id_type' order by id desc";
$result = mysql_db_query($db,$sql);
while ($dbarr= mysql_fetch_array($result) ){
$id=$dbarr[id];
$type=$dbarr[type];
$title=$dbarr[title];
$detail=$dbarr[detail];
$name=$dbarr[name];
$user_name=$dbarr[user];
$date_new=$dbarr[date_new];
//ปัญหาคือตรงนี้ค่ะ โชว์ข้อมูลตลอด ทุกอัน ไม่รู้จะเพิ่มโค๊ดตรงไหนดี
echo " <li>[$date_new] <A href = 'view.php?id_view=$id' Target='_blank'> $title</a></li>";
}
?></td>
</tr>
<td height="2"></form>
<div align="center"></div>
</table></td>
</tr>
</table>
<p> </p>
<br>
<? echo "<center>";?>
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 "<center>";
echo $pages->display_pages()
?>
<?
//mysql_close($objConnect);
?>
</body>
</html>
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2011-07-20 14:16:02 |
By :
vuccine |
View :
790 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมว่าโค้ดแบ่งหน้าจากบรรทัด 171-247 มันน่าจะทำงานได้แล้วน่ะครับ
ไม่แน่ใจเป็นเพราะ function ด้านบนรึป่าวน่ะครับ
ลองเอาออก แล้วใช้โค้ด บรรทัด 171-247 ดูน่ะครับ
|
|
|
|
|
Date :
2011-07-20 16:09:25 |
By :
mangkunzo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เอาออกแล้ว Error ค่ะ
|
|
|
|
|
Date :
2011-07-22 13:37:33 |
By :
vuccine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่าง Code ผมเอง (PHP)
<?
include("connect.php");
?>
<link href="Scripts/CSS_Text.css" rel="stylesheet" type="text/css">
<table width="185" height="21" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="200" height="32" valign="top" bgcolor="#0A73BA"><table width="185" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="29" valign="bottom" bgcolor="#FFFFFF"><span class="h1_text">กิจกรรม<img src="File_images/Icon01.gif" width="20" height="20" /></span></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="185" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="9" height="10" align="center"> </td>
<td width="176" height="10" align="left" class="h6_text"> </td>
</tr>
<?
$strSQL ="select * from tb_event";
$objQuery = mysql_query($strSQL);
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 8; // Per Page
$Page = $_GET["PageEvent"];
if(!$_GET["PageEvent"])
{
$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 Date DESC LIMIT $Page_Start , $Per_Page";
$objQuery = mysql_query($strSQL);
$No=1;
if($Num_Rows <1)
{
?>
<tr>
<td height="10" align="center"><img src="File_images/ICON_D.gif" width="7" height="6" /></td>
<td height="10" align="left" class="h6_text">ยังไม่มีข้อมูล</td>
</tr>
<?
}
else
{
while($Event=mysql_fetch_array($objQuery))
{
?>
<tr>
<td height="10" align="center"><img src="File_images/ICON_D.gif" width="7" height="6" /></td>
<td height="10" align="left" class="h6_text"><a href="ShowPR.php?event=<?=$Event['ID']?>" class="h4_text" rel="lightbox_Event[]" rev="width=800, height=500" >
<?
echo $Event['Head'];
?></a></td>
</tr>
<?
}
}
?>
<tr>
<td height="25" colspan="2" align="right" valign="bottom" background="img/line_B.gif" class="h6_text"> ทั้งหมด
<?=" ".$Num_Rows." ";?>
รายการ :
<?=$Num_Pages;?>
หน้า :
<?
if($Prev_Page)
{
echo " <a href='$_SERVER[SCRIPT_NAME]?PageEvent=$Prev_Page'><<ย้อนกลับ</a> ";
}
for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?PageEvent=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a href ='$_SERVER[SCRIPT_NAME]?PageEvent=$Next_Page'>ถัดไป>></a> ";
}
?>
</td>
</tr>
</table></td>
</tr>
</table>
|
|
|
|
|
Date :
2011-07-22 15:20:07 |
By :
mangkunzo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|