index.php文件
<style>
.pages {
clear:left;
font-family:Arial,Helvetica,sans-serif;
font-size:14px;
font-size-adjust:none;
font-stretch:normal;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:normal;
margin:0px;
padding:0.5em 0pt 2.3em 1em;
}
.pages .page_total, .pages .page_list, .pages .page_select {
float:left;
}
.pages .page_total {
padding-right:10px;
padding-top:3px;
text-align:center;
}
.pages .page_select {
padding-left:10px;
text-align:center;
}
.pages a, .pages span {
background:#FFFFFF none repeat scroll 0%;
border:1px solid #FFFFFF;
display:block;
float:left;
margin:1px;
padding:2px;
}
.pages span.current {
background:#2E6AB1 none repeat scroll 0%;
border:1px solid #2E6AB1;
color:#FFFFFF;
font-weight:bold;
}
.pages a:link, .pages a:visited {
border:1px solid #9AAFE5;
text-decoration:none;
}
.pages a:hover {
border-color:#2E6AB1;
}
.pages a.nextprev {
font-weight:bold;
}
.pages span.nextprev, .pages span.stat {
border:1px solid #DDDDDD;
color:#999999;
}
</style>
<?php
include 'conn.php';
include 'page.class.php';
$obj = new MyConnect();
$obj->connect();
$pg = new page();
$sql = "select count(0) as count from article";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$num_rows = $row['count'];
$param = array();
$param['begintime'] = time();
$param['p'] = $p = isset($_GET['p'])?$_GET['p']:1;
$pg->setPageVar('p');
$pg->setNumPerPage(10);
unset($param['p']);
$pg->setVar($param);
$pg->set($num_rows);
$num = $pg->getNumPerPage();
$begin = $pg->getOffset();
$condition = ' where 1=1 ';
$p && $condition = ' limit '.$begin.','.$num;
$sql .= $condition;
$sql = str_replace('count(0) as count',"article_id,title", $sql);
$result = mysql_query($sql);
echo $sql;
$list = array();
while($row = mysql_fetch_assoc($result)){
$list[] = $row;
}
$page = $pg->output(2);
?>
<table border=1>
<tr>
<td>文章ID</td>
<td>文章标题</td>
</tr>
<?php
if(count($list) > 0){
foreach($list as $k=>$v){
?>
<tr>
<td><?php echo $v['article_id'];?></td>
<td><?php echo $v['title'];?></td>
</tr>
<?php
}
}
?>
<tr>
<td colspan="2"><?php echo $page;?></td>
</tr>
</table>
<style>
.pages {
clear:left;
font-family:Arial,Helvetica,sans-serif;
font-size:14px;
font-size-adjust:none;
font-stretch:normal;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:normal;
margin:0px;
padding:0.5em 0pt 2.3em 1em;
}
.pages .page_total, .pages .page_list, .pages .page_select {
float:left;
}
.pages .page_total {
padding-right:10px;
padding-top:3px;
text-align:center;
}
.pages .page_select {
padding-left:10px;
text-align:center;
}
.pages a, .pages span {
background:#FFFFFF none repeat scroll 0%;
border:1px solid #FFFFFF;
display:block;
float:left;
margin:1px;
padding:2px;
}
.pages span.current {
background:#2E6AB1 none repeat scroll 0%;
border:1px solid #2E6AB1;
color:#FFFFFF;
font-weight:bold;
}
.pages a:link, .pages a:visited {
border:1px solid #9AAFE5;
text-decoration:none;
}
.pages a:hover {
border-color:#2E6AB1;
}
.pages a.nextprev {
font-weight:bold;
}
.pages span.nextprev, .pages span.stat {
border:1px solid #DDDDDD;
color:#999999;
}
</style>
<?php
include 'conn.php';
include 'page.tool.php';
$obj = new MyConnect();
$obj->connect();
$pg = new page();
$sql = "select count(0) as count from article";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$num_rows = $row['count'];
$param = array();
$param['begintime'] = time();
$param['p'] = $p = isset($_GET['p'])?$_GET['p']:1;
$pg->setPageVar('p');
$pg->setNumPerPage(10);
unset($param['p']);
$pg->setVar($param);
$pg->set($num_rows);
$num = $pg->getNumPerPage();
$begin = $pg->getOffset();
$condition = ' where 1=1 ';
$p && $condition = ' limit '.$begin.','.$num;
$sql .= $condition;
$sql = str_replace('count(0) as count',"article_id,title", $sql);
$result = mysql_query($sql);
echo $sql;
$list = array();
while($row = mysql_fetch_assoc($result)){
$list[] = $row;
}
$page = $pg->output(2);
?>
<table border=1>
<tr>
<td>文章ID</td>
<td>文章标题</td>
</tr>
<?php
if(count($list) > 0){
foreach($list as $k=>$v){
?>
<tr>
<td><?php echo $v['article_id'];?></td>
<td><?php echo $v['title'];?></td>
</tr>
<?php
}
}
?>
<tr>
<td colspan="2"><?php echo $page;?></td>
</tr>
</table>
page.class.php
<?php
/*
+--------------------------------------------------------------------------
| mysql纪录分页类
| ============================
| by youreyes 2012
+--------------------------------------------------------------------------
*/
class page {
/**
* 页面输出结果
*
* @var string
*/
var $output = '';
/**
* 使用该类的文件,默认为 PHP_SELF
*
* @var string
*/
var $file;
/**
* 页数传递变量,默认为 'p'
*
* @var string
*/
var $page_var = "p";
/**
* 页面大小,每页显示多少个记录
*
* @var integer
*/
var $num_per_page = 10;
/**
* 当前页面
*
* @var ingeger
*/
var $current_page;
/**
* 要传递的变量数组
*
* @var array
*/
var $var_str;
/**
* 总页数
*
* @var integer
*/
var $total_pages;
/**
* LIMIT 语句中的offset
*
* @var integer
*/
var $offset = 0;
/**
* 分页显示几个链接 如:第 1 2 3 4 5 6 7 8 页
*
* @var integer
*/
var $link_num = 8;
var $prev_page = 0;
var $prev_page_url = '';
var $next_page = 0;
var $next_page_url = '';
/**
* 分页设置
*
* @access public
* @param int $num_per_page 页面大小
* @param int $record_nums 总记录数
* @param int $current 当前页数,默认会自动读取
* @return void
*/
function set($record_nums, $current_page=false, $unit='条记录') {
if (!$current_page) {
$current_page = (isset($_GET[$this->page_var]) && is_numeric($_GET[$this->page_var]) && $_GET[$this->page_var] > 0) ? intval($_GET[$this->page_var]) : ((isset($_POST[$this->page_var]) && is_numeric($_POST[$this->page_var]) && $_POST[$this->page_var] > 0) ? intval($_POST[$this->page_var]) : 1);
}
if ($current_page == 0) {
$current_page = 1;
} else {
$current_page = floor($current_page); // 防止有人故意捣乱
}
if ($current_page > 0) {
$total_pages = ($record_nums > 0) ? ceil($record_nums/$this->num_per_page) : 1;//总页数
if ((int)$current_page > $total_pages) { // 防止有人故意将页数输入太大
$current_page = $total_pages;
}
$current_page = floor($current_page); // 防止有人故意捣乱
$offset = ($current_page-1) * $this->num_per_page;
} else { // 无翻页参数则默认为第1页
$offset = 0;
$current_page = 1;
$total_pages = 1;
}
$this->current_page = $current_page;
$this->offset = $offset;
$this->total_pages = $total_pages;
if ($record_nums == 0 || $total_pages == 1) {
$this->output = '';
return;
}
if (!$this->file) {$this->file = $_SERVER['PHP_SELF'];}
$start = ($current_page-round($this->link_num/2)) > 0 ? ($current_page-round($this->link_num/2)) : 1;
$end = ($current_page+round($this->link_num/2)) < $total_pages ? ($current_page+round($this->link_num/2)) : $total_pages;
if ($current_page < round($this->link_num/2)) {
$end = ( $this->link_num > $total_pages ) ? $total_pages : $this->link_num;
}
if ($current_page != 1) {
$this->prev_page = $current_page - 1;
$this->prev_page_url = $this->file . '?' . $this->page_var . '=' . $this->prev_page . ($this->var_str);
} elseif ($current_page = 1) {
$this->prev_page = 0;
$this->prev_page_url = '';
}
if ($this->prev_page){
$this->output .= '<a href="' . $this->file . '?' . $this->page_var . '=' . ($current_page-1) . ($this->var_str) . '" target="_self" class=nextprev>上一页</a>';
}else{
$this->output .= '<span class=nextprev>上一页</span>';
}
$page_list_ary = array();
for ($t = $start; $t <= $end; $t++) {
$page_list_ary[] = ($current_page==$t) ? '<a class=current><font color="red"><b>' . $t . '</b></font></a>' : '<a class=p_num href="' . $this->file . '?' . $this->page_var . '=' . $t . ($this->var_str) . '" target="_self">' . $t . '</a>';
}
$this->output .= implode('', $page_list_ary);
if ($current_page != $total_pages) {
$this->next_page = $current_page + 1;
$this->next_page_url = $this->file . '?' . $this->page_var . '=' . $this->next_page . ($this->var_str);
} elseif ($current_page = $total_pages) {
$this->next_page = 0;
$this->next_page_url = '';
}
if ($this->next_page){
$this->output .= '<a href="' . $this->file . '?' . $this->page_var . '=' . ($current_page+1) . ($this->var_str) . '" target="_self" class=nextprev>下一页</a>';
}else{
$this->output .= '<span class=nextprev>下一页</span>';
}
//select下拉框直接跳转
$this->output .= " 转到第<select onchange=\"location='" . $this->file . '?' . $this->page_var . "='+this.options[this.selectedIndex].value+'" . ($this->var_str) . "'\" align=absmiddle style=\"font-size:8pt;border: 1px solid #999999;\">\r";
$show_all = 200;
$slice_start = 5;
$slice_end = 5;
$percent = 20;
$range = 10;
if ($total_pages < $show_all) {
$pages = range(1, $total_pages);
} else {
$pages = array();
// Always show first X pages
for ($i = 1; $i <= $slice_start; $i++) {
$pages[] = $i;
}
// Always show last X pages
for ($i = $total_pages - $slice_end; $i <= $total_pages; $i++) {
$pages[] = $i;
}
// garvin: Based on the number of results we add the specified $percent percentate to each page number,
// so that we have a representing page number every now and then to immideately jump to specific pages.
// As soon as we get near our currently chosen page ($pageNow - $range), every page number will be
// shown.
$i = $slice_start;
$x = $total_pages - $slice_end;
$met_boundary = false;
while($i <= $x) {
if ($i >= ($current_page - $range) && $i <= ($current_page + $range)) {
// If our pageselector comes near the current page, we use 1 counter increments
$i++;
$met_boundary = true;
} else {
// We add the percentate increment to our current page to hop to the next one in range
$i = $i + floor($total_pages / $percent);
// Make sure that we do not cross our boundaries.
if ($i > ($current_page - $range) && !$met_boundary) {
$i = $current_page - $range;
}
}
if ($i > 0 && $i <= $x) {
$pages[] = $i;
}
}
// Since because of ellipsing of the current page some numbers may be double,
// we unify our array:
sort($pages);
$pages = array_unique($pages);
}
$this->output .= '<option value="' . $current_page . '">' . $current_page . '</option>';
foreach ($pages AS $i) {
if ($i == $current_page) {
$selected = 'selected="selected" style="font-weight: bold"';
} else {
$selected = '';
}
$this->output .= '<option ' . $selected . ' value="' . $i . '">' . $i . "</option>\r";
}
$this->output .= ' </select>页';
//显示每页记录数及记录总数
$this->output = '<div id="page_string" class="pages"><a class=p_total>共' . $record_nums . $unit . '</a>' . $this->output . '</div>';
}
function setNumPerPage($num_per_page)
{
$this->num_per_page = intval($num_per_page);
}
/**
* 要传递的变量设置
*
* @access public
* @param array $data 要传递的变量,用数组来表示,参见上面的例子
* @return void
*/
function setVar($data) {
foreach ($data as $k=>$v) {
$this->var_str.='&'.$k.'='.urlencode($v);
}
}
function setPageVar($page_var) {
$this->page_var = $page_var;
}
function setLinkNum($number)
{
$this->link_num = intval($number);
}
/**
* 分页结果输出
*
* @access public
* @param bool $return 为真时返回一个字符串,否则直接输出,默认直接输出
* @return string
*/
function output($return = false) {
if ($return) {
return $this->output;
} else {
echo $this->output;
}
}
function getOffset()
{
//echo $this->offset;
return $this->offset;
}
function getTotalPages()
{
return $this->total_pages;
}
function getCurrentPage()
{
return $this->current_page;
}
function getNumPerPage()
{
return $this->num_per_page;
}
} //End Class
?>