我的代碼顯示了一頁上的所有頁數。我想限制頁面數量?如何限制頁碼?
我的代碼是
$start_date = $_REQUEST['date1'];
$end_date = $_REQUEST['date2'];
$condition="1=1";
if ($start_date!="")
$condition.=" AND event_date>='".date("Y-m-d", strtotime($start_date))."'";
if ($end_date!="")
$condition.=" AND event_date<='".date("Y-m-d", strtotime($end_date))."'";
$start_date = date("Y-m-d", strtotime($start_date));
$end_date = date("Y-m-d", strtotime($end_date));
$link = mysql_connect('localhost', 'username', 'password');
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
if ($_REQUEST["orderby"]!="") $ord = " ORDER BY ".$_REQUEST["orderby"];
if ($_REQUEST["dir"]!="") $ord .= " ".$_REQUEST["dir"];
mysql_select_db("intern_db", $link);
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$recordPerPage= '30';
$startPoint = ($page - 1)*$recordPerPage;
$result = mysql_query("SELECT count(*) cnt FROM ` admin_crmf_poc_event_history` where $condition");
$row = mysql_fetch_array($result);
$num_rows = $row["cnt"];
$result = mysql_query("SELECT * FROM ` admin_crmf_poc_event_history` where $condition $ord LIMIT $startPoint,$recordPerPage");
$totalPages=ceil($num_rows/$recordPerPage);
if ($page > 1) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($page- 1).'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">Previous</a>';
}
for ($i = 1; $i < $totalPages; $i++) {
echo ' <a href="'.$_SERVER['PHP_SELF'].'?page= '. $i .'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">' . $i . '</a> ';
}
if ($page < $totalPages) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='. ($page+1).'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">Next</a>';
}
echo "<br>";
echo "you are in $page page";
mysql_close($link);
我想告訴喜歡previous 1 2 3 4 5 6 7 8 9 10 next
。 當我點擊下一頁時,它會顯示從11到20的其他10頁,依此類推
對你來說一個好主意是使用一些準備好的分頁功能或類..沒有必要「發現美國」.. 在你的代碼中,你不會逃避你的數據和任何人可以做不注入的sql注入od .. :) – Svetoslav