PHP paging function code (simple utility)
- 2020-03-31 21:17:41
- OfStack
Prepare data:
Create a new database, test
Execute the following statement (create a new table, test: id, sex, name)
The CREATE TABLE ` test ` (
'id' INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
'sex' INT(1) NOT NULL,
'name' VARCHAR(20) NOT NULL) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_bin;
Add data to the test table, for example:
1 1 jack Bauer
2 0 little red
3 0 small beautiful
4 1 batman
5 1 zhang SAN
6 0 li si
7 0 Wu Xin
< img border = 0 SRC = "http://files.jb51.net/upload/201012/20101202215515104.png" border = 0 >
PHP statement (index.php) :
Browse the index. PHP page, as shown:
< img border = 0 SRC = "http://files.jb51.net/upload/201012/20101202215558135.png" border = 0 >
It's time to say bye!
PHP simple paging functions
Wrote a simple PHP pagination function, database calls are also written inside, with the people can cut!
Create a new database, test
Execute the following statement (create a new table, test: id, sex, name)
The CREATE TABLE ` test ` (
'id' INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
'sex' INT(1) NOT NULL,
'name' VARCHAR(20) NOT NULL) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_bin;
Add data to the test table, for example:
1 1 jack Bauer
2 0 little red
3 0 small beautiful
4 1 batman
5 1 zhang SAN
6 0 li si
7 0 Wu Xin
< img border = 0 SRC = "http://files.jb51.net/upload/201012/20101202215515104.png" border = 0 >
PHP statement (index.php) :
<?php
//Page Page function
$page = $_GET["page"];
function Page($rows,$page_size){
global $page,$select_from,$select_limit,$pagenav;
$page_count = ceil($rows/$page_size);
if($page <= 1 || $page == '') $page = 1;
if($page >= $page_count) $page = $page_count;
$select_limit = $page_size;
$select_from = ($page - 1) * $page_size.',';
$pre_page = ($page == 1)? 1 : $page - 1;
$next_page= ($page == $page_count)? $page_count : $page + 1 ;
$pagenav .= " The first $page/$page_count page A total of $rows records ";
$pagenav .= "<a href='?page=1'> Home page </a> ";
$pagenav .= "<a href='?page=$pre_page'> The previous page </a> ";
$pagenav .= "<a href='?page=$next_page'> After a page </a> ";
$pagenav .= "<a href='?page=$page_count'> At the end of the page </a>";
$pagenav.=" Jump to the <select name='topage' size='1' onchange='window.location="?page="+this.value'>n";
for($i=1;$i<=$page_count;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option>n";
else $pagenav.="<option value='$i'>$i</option>n";
}
} //Page Page function
//Use the sample
if (!$conn= mysql_connect("localhost", "root" ,"root")) die(' Database selection failed! ');
if (!mysql_select_db("test", $conn)) die(' Database selection failed! ');
mysql_query('set names GBK');
//The Page function is used to calculate from which record $select_from will be retrieved and the $pagenav output paging navigation
$rows = mysql_num_rows(mysql_query("select * from test"));
Page($rows,2);
$sql = "select * from test limit $select_from $select_limit";
$rst = mysql_query($sql);
while ($row = mysql_fetch_array($rst)){
echo "$row[id] - $row[sex] - $row[name] <hr />";
}
echo $pagenav;
?>
Browse the index. PHP page, as shown:
< img border = 0 SRC = "http://files.jb51.net/upload/201012/20101202215558135.png" border = 0 >
It's time to say bye!
PHP simple paging functions
Wrote a simple PHP pagination function, database calls are also written inside, with the people can cut!
function getask(){
$sql = "select * from cms_ask where ansower <> ' ' ";
//I'm going to change the method here
$q_sq = mysql_query($sql);
$count = mysql_num_rows($q_sq);
$page_size = 8;
$page_current = isset($GLOBALS['page']) ? intval($GLOBALS['page']) : 1;
$page_count = ceil($count / $page_size);
$page_start = $page_current - 4;
$page_end = $page_current + 4;
if ($page_current < 5) {
$page_start = 1;
$page_end = 5;
}
if ($page_current > $page_count - 4) {
$page_start = $page_count - 8;
$page_end = $page_count;
}
if ($page_start < 1)
$page_start = 1;
if ($page_end > $page_count)
$page_end = $page_count;
$pagebar = "";
$sql = "select * from cms_ask where ansower <> ' ' order by id desc limit " . (($page_current - 1) * $page_size) . "," . $page_size;
$row=$this -> user -> getall("$sql");
foreach($row as $v){
echo '<dl><dt> Q: '.$v["question"].'</dt><dd> A: '.$v["ansower"].date("Y-m-d H:i:s",$v["postTime"]).'</dd></dl>';
}
$pagebar .= "<div class="page">";
$pagebar .= "<ol>";
if ($page_current != 1) {
$pagebar .= '<li><a href="javascript:get_comment(1)" class="sx">FIRST</a></li>';
}
for ($i = $page_start; $i <= $page_end; $i++) {
if ($i == $page_current) {
$pagebar .= "<li><span class="sort">" . $i . "</span></li>";
} else {
$pagebar .= "<li><a href='javascript:get_comment(" . $i . ")'>" . $i . "</a></li>";
}
}
if ($page_current != $page_count) {
$pagebar .= "<li><a href='javascript:get_comment(" . $page_count . ")' class='sx'>END</a></li>";
}
$pagebar .= "</ol>";
$pagebar .= " </div>";
echo $pagebar;
}