2014-04-15 31 views
0

我的代碼顯示了一頁上的所有頁數。我想限制頁面數量?如何限制頁碼?

我的代碼是

$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 '&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page= '. $i .'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">' . $i . '</a>&nbsp;&nbsp;'; 
} 
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頁,依此類推

+0

對你來說一個好主意是使用一些準備好的分頁功能或類..沒有必要「發現美國」.. 在你的代碼中,你不會逃避你的數據和任何人可以做不注入的sql注入od .. :) – Svetoslav

回答

1

正如我評論你的問題,你的代碼裏面有A LOT的問題,所以更好的是使用一些準備好的功能/類爲它:)只是搜索它..

我怎麼能給你簡單的方法顯示你想要的..

與代碼替換您的循環,並會被罰款:)

$start = (floor($page/10) * 10) + 1; 
for($i = $start; $i < $totalPages; $i++){ 
    if($i >= ($start + 10)){ 
     break; 
    } 
    echo '&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page= '. $i .'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">' . $i . '</a>&nbsp;&nbsp;'; 
} 

但實際上SECURE YOUR CODE

月1日 - MYSQL_功能已棄用使用的mysqli或PDO來代替..

2日 - 逃避所有的變量在查詢..

3日 - 想想使用工作的更靈活的方法(函數/對象..)

+0

謝謝你的幫助。其實我是PHP和MySQL的新手。我將在未來遵循你的指示。再次感謝你,你的代碼工作':)' – Rizvi

+0

我應該怎麼做,如果我想它倒序也? – Rizvi