PHP ?? ?? ?? ??? ?? ?? ??? ??
?? ???? ?? ?? ?? ??? ?? ???????, ?? ????? ?? ?? ??? ??? ??? ??? ?????! ??? ??? ??? ??? ????? ?????????!
?? PHP ?? page.php? ????. ? ??? ??? ?? ??? ?? ??? ????!
? ?? ??: ???? ??????? ?????!
// 顯示所有的錯(cuò)誤 error_reporting(E_ALL & ~E_NOTICE ); // 連接mysql數(shù)據(jù)庫(kù) $link = mysqli_connect('localhost','root', 'root'); if (!$link) { echo "connect mysql error!"; exit(); } // 選中數(shù)據(jù)庫(kù) news為數(shù)據(jù)庫(kù)的名字 $db_selected = mysqli_select_db($link, 'news'); if (!$db_selected) { echo "<br>selected db error!"; exit(); } // 設(shè)置mysql字符集 為 utf8 $link->query("set names utf8");
? ?? ??: ??? ??? ????? ?? ??? ?? ??? ???? ??? ???? ???. ??? ??? SQL ?? ?? ???? ???? ???.
// 查詢新聞表中的數(shù)據(jù) $sql = "select * from new where 1 "; // 查詢語(yǔ)句 $sql_count = "select count(*) as amount from new where 1 "; // 統(tǒng)計(jì)總記錄數(shù) $sql .= "order by id asc";
?? ?? ? ??? ?? ???? ???. :
// 獲取總記錄條數(shù) $result_amount = mysqli_query($link, $sql_count); $arr_amount = mysqli_fetch_array(mysqli_query($link, $sql_count), MYSQL_ASSOC); // 總記錄條數(shù) $amount = $arr_amount['amount'];
?? ?? ? ??? ?? ? ??? ?? ?????
// 每頁(yè)的記錄條數(shù) $page_size = 4; // 總頁(yè)碼 $max_page = ceil( $amount / $page_size );
???? ?? ?? ????? ??? ?? ??? ?? ????. ? ??? ???? ?? ???, ?? ???, ??? ???? ?????!
// 獲取當(dāng)前頁(yè)碼 $page = intval($_GET['page']); // 獲取page值,并轉(zhuǎn)成int if( $page <= 0 || $page > $max_page){ // 如果page值小于0,或是大于最大頁(yè)碼 $page = 1; } // 上一頁(yè) $pre_page = $page -1; if( $pre_page < 1 ){ // 如果上一頁(yè)小于1 $pre_page = 1; } // 下一頁(yè) $next_page = $page + 1; if( $next_page > $max_page ){ // 如果下一頁(yè)大于最大頁(yè)碼 $next_page = $max_page; } // 分頁(yè)計(jì)算, 計(jì)算分頁(yè)的offset $offset = ($page - 1 ) * $page_size; $sql .= " limit $offset, $page_size ";
??? ??? ??? ????, ?? ?? ???? ? ??? ??? ?????
<?php include_once "../common/page.php"; ?>
????? ?? ?? ??? ???? ??? ??? ?? ???? ??
<div class="pagelist"> <a href="new_list.php">首頁(yè)</a> <?php if( $page > 1 ){ ?> <a href="new_list.php?page=<?php echo $pre_page;?>">上一頁(yè)</a> <? } if( $page < $max_page ){ ?> <a href="new_list.php?page=<?php echo $next_page;?>">下一頁(yè)</a> <? } ?> <a href="new_list.php?page=<?php echo $max_page;?>">末頁(yè)</a> / 總頁(yè)碼 <font color="red"><?php echo $max_page;?></font>頁(yè) 當(dāng)前頁(yè)碼 <font color="red"><?php echo $page;?></font>頁(yè) </div>
OK! ??? ??? ???????!
??: ????? ??? ???? ?????? ?? ??? ?? ????. ??? ??? ??? ??? ??? ???~