2014-01-08 111 views
0

我有動態下拉列表和php分頁。我想爲頁碼創建一個下拉菜單。而不是有正常的分頁(上一頁1-2-3下一頁),我希望那些在下拉菜單中。 就像在管理頁面中,下拉菜單可幫助您快速移動到所需的頁面。下拉列表中的Php分頁

這樣在phpMyAdmin的

enter image description here

這是我的代碼右排

第一頁

<script> 
function showUser(str,ids) { 
    var $txtHint = $('#txtHint'); 
    if (str=="" || ids=="") { 
     $txtHint.html(''); 
     return; 
    } 
    $txtHint.load('app_list.php?q='+str+'&id='+ids) 
} 

</script> 

Secondpage

$id = $mysqli->real_escape_string($_GET["q"]); 
$id2 = $mysqli->real_escape_string($_GET["id"]); 
if (isset($_GET['id'])) { 
    $id2 = preg_replace('#[^0-9]#i', '', $_GET['id']); 
} else { 
    $id2 = 1; 
} 
$itemsPerPage = 100; 
$lastPage = ceil($nr/$itemsPerPage); 
if ($id2 < 1) { 
    $id2 = 1; 
} else if ($id2 > $lastPage) { 
    $id2 = $lastPage; 
} 

$centerPages = ""; 
$sub1 = $id2 - 1; 
$sub2 = $id2 - 2; 
$add1 = $id2 + 1; 
$add2 = $id2 + 2; 
if ($id2 == 1) { 

    $centerPages .= '&nbsp; <span class="pagNumActive">' . $id2 . '</span> &nbsp;'; 
    $centerPages .= '&nbsp; <a href="#" onclick=showUser(str="'. $id .'",ids="'. $add1 .'")>' . $add1 . '</a> &nbsp;'; 
} else if ($id2 == $lastPage) { 
    $centerPages .= '&nbsp; <a href="#" onclick=showUser(str="'. $id .'",ids="'. $sub1 .'")>' . $sub1 . '</a> &nbsp;'; 
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $id2 . '</span> &nbsp;'; 
} else if ($id2 > 2 && $id2 < ($lastPage - 1)) { 
    $centerPages .= '&nbsp; <a href="#" onclick=showUser(str="'. $id .'",ids="'. $sub2 .'")>' . $sub2 . '</a> &nbsp;'; 
    $centerPages .= '&nbsp; <a href="#" onclick=showUser(str="'. $id .'",ids="'. $sub1 .'")>' . $sub1 . '</a> &nbsp;'; 
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $id2 . '</span> &nbsp;'; 
    $centerPages .= '&nbsp; <a href="#" onclick=showUser(str="'. $id .'",ids="'. $add1 .'")>' . $add1 . '</a> &nbsp;'; 
    $centerPages .= '&nbsp; <a href="#" onclick=showUser(str="'. $id .'",ids="'. $add2 .'")>' . $add2 . '</a> &nbsp;'; 
} else if ($id2 > 1 && $id2 < $lastPage) { 
    $centerPages .= '&nbsp; <a href="#" onclick=showUser(str="'. $id .'",ids="'. $sub1 .'")>' . $sub1 . '</a> &nbsp;'; 
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $id2 . '</span> &nbsp;'; 
    $centerPages .= '&nbsp; <a href="#" onclick=showUser(str="'. $id .'",ids="'. $add1 .'")>' . $add1 . '</a> &nbsp;'; 
} 

$limit = 'LIMIT ' .($id2 - 1) * $itemsPerPage .',' .$itemsPerPage; 

//query 

$paginationDisplay = ""; 
if ($lastPage != "1"){ 
    $paginationDisplay .= 'Page <strong>' . $id2 . '</strong> of ' . $lastPage. '&nbsp; &nbsp; &nbsp; '; 
    if ($id2 != 1) { 
     $previous = $id2 - 1; 
     //$paginationDisplay .= '&nbsp; <a href="next?id=' . $previous . '"> Back</a> '; 
     $paginationDisplay .= '&nbsp; <a href="#" onclick=showUser(str="'. $id .'",ids="'. $previous .'")> Back</a> '; 
    }            
    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>'; 
    if ($id2 != $lastPage) { 
     $nextPage = $id2 + 1; 
     $paginationDisplay .= '&nbsp; <a href="#" onclick=showUser(str="'. $id .'",ids="'. $nextPage .'")> Next</a> '; 
     //$paginationDisplay .= '&nbsp; <a href="next.php?id=' . $nextPage . '"> Next</a> '; 
    } 
} 

我該怎麼做?一些幫助...感謝提前

回答

0

要顯示分頁號碼,您必須首先根據結果和頁面限制計算頁數。

在你的sql查詢中使用'SQL_CALC_FOUND_ROWS作爲COUNT'列來獲得結果數。並將其存儲在一個變量中,比如$ total_results。

計算使用公式的頁數,

$page_count = ($total_results/$page_limit) + 1 

其中,

$page_count => The number of pages available \n 
$total_results => The number of result rows (from sql query COUNT) 
$page_limit => Number of results shown in each page 

在你的PHP頁面打印的HTML這樣的下拉列表中,

<select id='pageNo'> 
    <?php 
     for($i=1; $i < $page_count; $i++) { 
      echo "<option value='".$i."'>".$i."</option>"; 
     } 
    ?> 
</section> 

在jQuery寫入更改函數,

$('#pageNo').on('change', function(){ 
    var loadPage = $('#pageNo').val(); 
    // Do your page submit to load another page 
}); 

我希望這會幫助你。請修改此以符合您的需求。

+0

我在jquery中遇到了困難。我可以如何將它混合到我的jQuery中?請原諒我。 – surname

+0

這是一個獨立的腳本。你可以把它放在你的頁面javascript文件的任何地方。 –

+0

下拉是正確的。但是當我嘗試選擇什麼都沒有發生。你會解釋它嗎?我有兩頁。第1頁有一個jQuery修改動態下拉菜單。第2頁有SQL查詢。 – surname