2011-12-02 97 views
14
$objConnect = mysql_connect("localhost","root","") or die(mysql_error()); 
$objDB = mysql_select_db("Test"); 

$strSQL = "SELECT * FROM UserAddedRecord WHERE (Name LIKE '%".$getname."%' and State LIKE '%".$getstate."%' and Cell LIKE '%".$getcell."%' and Custgroup LIKE '%".$getgroup."%') AND user_id=$id"; 

$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); 
$Num_Rows = mysql_num_rows($objQuery); 

$Per_Page = 5; 

if (!isset($_GET['Page'])) { 
    $Page = 1; 
} else { 
    $Page = $_GET['Page']; 
} 

$Prev_Page = $Page - 1; 
$Next_Page = $Page + 1; 

$Page_Start = (($Per_Page * $Page) - $Per_Page); 
if ($Num_Rows <= $Per_Page) { 
    $Num_Pages = 1; 
} elseif (($Num_Rows % $Per_Page) == 0) { 
    $Num_Pages = ($Num_Rows/$Per_Page) ; 
} else { 
    $Num_Pages = ($Num_Rows/$Per_Page) + 1; 
    $Num_Pages = (int) $Num_Pages; 
} 


$strSQL .=" order by addedrec_ID DESC LIMIT $Page_Start , $Per_Page"; 
$objQuery = mysql_query($strSQL) or trigger_error(mysql_error());; 

if ($Prev_Page) { 
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'><< Back</a> "; 
} 

for ($i=1; $i <= $Num_Pages; $i++) { 
    if ($i != $Page) { 
     echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$i&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>$i</a> "; 
    } else { 
     echo "<b> $i </b>"; 
    } 
} 

if ($Page!=$Num_Pages) { 
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>Next>></a> ";   
} 

mysql_close($objConnect); 

這是我用來爲數據搜索創建分頁頁面的代碼。但我只注意到我有太多的記錄,那麼它會有太多的頁碼。我試圖限制頁碼顯示,並尋找很多例子..它有很多方法來做到這一點,但我仍然不知道如何限制它爲我的方法...極限分頁頁碼

回答

28

好吧,如果你的意思是表明像

Prev 1 2 3 4 5 6 .. 40 41 Next 
Prev 1 2 .. 6 7 8 9 10 .. 40 41 Next 

第一件事情,我們需要的就是創建一個可以處理功能我們需要輸出分頁。這裏有一個我使用的功能,它運行良好。

function get_paging_info($tot_rows,$pp,$curr_page) 
{ 
    $pages = ceil($tot_rows/$pp); // calc pages 

    $data = array(); // start out array 
    $data['si']  = ($curr_page * $pp) - $pp; // what row to start at 
    $data['pages']  = $pages;     // add the pages 
    $data['curr_page'] = $curr_page;    // Whats the current page 

    return $data; //return the paging data 

} 

現在這個功能非常穩定,對我來說效果很好。

所以,你通過這個功能

  • $ tot_rows =計行查詢
  • $頁=每頁項目
  • $ curr_page =當前頁碼

好了,現在你有你需要的數據,你需要顯示它。

下面是我用的,請在你想'啊,太長了'之前閱讀它。這其實很簡單。

繼承人的快照,它會返回

enter image description here

<!-- Create the query --> 
    <?php $count = mysql_fetch_assoc(mysql_query ("SELECT COUNT(rows) as count FROM table")) ; 

    <?php $count = $count[0]['count']; ?> 

<!-- Call our function from above --> 
<?php $paging_info = get_paging_info($count,5,34); ?> 


<p> 
    <!-- If the current page is more than 1, show the First and Previous links --> 
    <?php if($paging_info['curr_page'] > 1) : ?> 
     <a href='' title='Page 1'>First</a> 
     <a href='' title='Page <?php echo ($paging_info['curr_page'] - 1); ?>'>Prev</a> 
    <?php endif; ?> 



    <?php 
     //setup starting point 

     //$max is equal to number of links shown 
     $max = 7; 
     if($paging_info['curr_page'] < $max) 
      $sp = 1; 
     elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max/2))) 
      $sp = $paging_info['pages'] - $max + 1; 
     elseif($paging_info['curr_page'] >= $max) 
      $sp = $paging_info['curr_page'] - floor($max/2); 
    ?> 

    <!-- If the current page >= $max then show link to 1st page --> 
    <?php if($paging_info['curr_page'] >= $max) : ?> 

     <a href='' title='Page 1'>1</a> 
     .. 

    <?php endif; ?> 

    <!-- Loop though max number of pages shown and show links either side equal to $max/2 --> 
    <?php for($i = $sp; $i <= ($sp + $max -1);$i++) : ?> 

     <?php 
      if($i > $paging_info['pages']) 
       continue; 
     ?> 

     <?php if($paging_info['curr_page'] == $i) : ?> 

      <span class='bold'><?php echo $i; ?></span> 

     <?php else : ?> 

      <a href='' title='Page <?php echo $i; ?>'><?php echo $i; ?></a> 

     <?php endif; ?> 

    <?php endfor; ?> 


    <!-- If the current page is less than say the last page minus $max pages divided by 2--> 
    <?php if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max/2))) : ?> 

     .. 
     <a href='' title='Page <?php echo $paging_info['pages']; ?>'><?php echo $paging_info['pages']; ?></a> 

    <?php endif; ?> 

    <!-- Show last two pages if we're not near them --> 
    <?php if($paging_info['curr_page'] < $paging_info['pages']) : ?> 

     <a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.($paging_info['curr_page'] + 1); ?>' title='Page <?php echo ($paging_info['curr_page'] + 1); ?>'>Next</a> 

     <a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.$paging_info['pages']; ?>' title='Page <?php echo $paging_info['pages']; ?>'>Last</a> 

    <?php endif; ?> 
</p> 
+0

非常感謝您的示例和所有說明,我現在試圖理解並學習它。 –

+0

很高興幫助。顯然,我只是從代碼中抓住了這個,稍微修改了一下,但結構在那裏,供你玩。 –

+1

這種類型的頁面導航存在的問題是,在'...'範圍的中間很難找到頁面(至少通過點擊)。您可以考慮使用我在[這個問題](http://stackoverflow.com/q/7835752/999120)中描述的方法,即使有1000頁的頁面,也可以通過幾次鼠標點擊輕鬆訪問任何頁面。 – Doin

2

您將需要使用您的LIMIT條款在您的sql語句。例如:

SELECT <column> 
    FROM <table> 
    LIMIT 0, 5 

參考:SELECT Syntax

+0

Thanks for your reply.Is this row?'$ strSQL。=「order by addedrec_ID DESC LIMIT $ Page_Start,$ Per_Page」; ' –

+0

結帳這http://php.about.com/od/mysqlcommands/g/Limit_sql.htm – Cyclonecode

2

我解決了一個非常類似的問題,別人用一個非常類似的腳本在這裏:How to limit pages shown in pagination script

而且,我注意到一些其他的異常與腳本(除了我固定的不可讀格式)。您應該將所有出現的$_SERVER[SCRIPT_NAME]更改爲$_SERVER['SCRIPT_NAME']。在腳本中,例如:

echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=..."; 

將成爲:

echo " <a href ='{$_SERVER['SCRIPT_NAME']}?Page=..."; 
+0

感謝您的鏈接和更正,我現在會嘗試。 –

2

我的代碼總是顯示至少9頁的總頁數的情況下> 9不管你是1,2,3

<ul class="pagination pagination-sm" style="align:center"> 
    <li><a href="./?page=1"><</span></a></li> 
    <?php 
    if ($current_page <4){ 
     if ($total_pages<9) 
     { 
      for($i = 1; $i <= $total_pages; $i++) 
      { 

       if ($i == $current_page) 
       { 
        echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n"; 
       } else { 
        echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n"; 
       } 
      } 
     } 
     else 
     { 
      for($i = 1; $i <= 9; $i++) 
      { 

       if ($i == $current_page) 
       { 
        echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n"; 
       } else { 
        echo ' <li><a href="./?trang='.$i.'">'; echo $i.'</a></li>'."\n"; 
       } 
      } 
     } 
    } 
    elseif ($current_page >$total_pages-4) 
    { 
     if ($total_pages<9) 
     { 
      for($i = 1; $i <= $total_pages; $i++){ 

       if ($i == $current_page){ 
        echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n"; 
       } else { 
        echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n"; 
       } 
      } 
     }else{ 
      for($i = $total_pages-8; $i <= $total_pages; $i++){ 

       if ($i == $current_page){ 
        echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n"; 
       } else { 
        echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n"; 
       } 
      } 
     } 

    }else{ 
     for($i = max(1, $current_page - 4); $i <= min($current_page + 4, $total_pages); $i++){ 

      if ($i == $current_page){ 
       echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n"; 
      } else { 
       echo ' <li><a href="./?trang='.$i.'">'; echo $i.'</a></li>'."\n"; 
      } 
     } 
    } 
    ?> 
    <li> 
     <a href="./?trang=<?php echo $total_pages ?>">></a> 
    </li> 
</ul> 
1

目的本代碼是隻顯示在分頁的頁面明確的(某些)號

從以下網址獲取當前頁碼和mysql的定義總頁數的$curr_pg_numb

獲取數量和定義$total_pages

這裏是分頁代碼

$counter=0; 
$init_num_to_left = 3;//initial number of pages to show to left from current page 
$init_num_to_right = 4; 

if($curr_pg_numb - $init_num_to_left < 1){ 
$init_num_to_right = $init_num_to_right + (1 - ($curr_pg_numb - $init_num_to_left)); 
$init_num_to_left = $curr_pg_numb - 1; 
} 
else if($curr_pg_numb + $init_num_to_right > $total_pages){ 
$init_num_to_left = $init_num_to_left + (($curr_pg_numb + $init_num_to_right) - $total_pages); 
$init_num_to_right = ($curr_pg_numb + $init_num_to_right) - $total_pages; 
} 

while($counter <= $total_pages) { 
    if ($counter > 0){ 
    if ($curr_pg_numb==$counter){ 
    echo '<strong> '. (htmlspecialchars($curr_pg_numb , ENT_QUOTES, "UTF-8") + 0). '</strong> '; 
    } 
    else { 
    if ($counter >= ($curr_pg_numb - $init_num_to_left) and $counter <= ($curr_pg_numb + $init_num_to_right)) { 
    echo '<a href="?cpn='. htmlspecialchars(($counter), ENT_QUOTES, "UTF-8"). '" style="margin:5px;">'. htmlspecialchars(($counter), ENT_QUOTES, "UTF-8"). '</a>'; 
    } 
    }//else { 
    }//if ($counter > 0){ 
$counter++; 
}//while($counter <= $total_pages) { 
3

$ count_pages =小區($總/ $ items_per_page的);

2

使其簡單,這裏是代碼:

<?php  
    //put this code on the begining of the page 
    $page=$_GET['page']; //Getting page number from url | example: www.mydomain.com/index.php?page=3 
    if(!is_numeric($page)) $page=1; //If there is no page number specified we set number 1 
$queryCount = "SELECT * FROM table "; //select from db 
$query = $DB->query($queryCount); 
    $num = mysql_num_rows ($query); //count num rows 
    $per_page=5; //default 5 results per page 
$start=$per_page*($page-1); //start for select on next page (page2, 3, 4,5) 
$end=min($num,$page*$per_page); //end 


//here is select for your results. Be careful for LIMIT in the select! 
$query = "SELECT * FROM page LIMIT $start, $per_page "; 


//page bottom, where you want to put your numbers 
$pages=ceil($num/$per_page); 
    for($s=1; $s<=$pages; $s++) 
    { 
    if($s==$page) 
     $numPage .= "[$s] "; 
    else 
     $numPage .= "<a href='index.php?page=$s'>$s</a> "; 
    } 

echo $numPage; 

>

我沒有測試它,但我認爲它必須努力:-)

CIND的問候,伊萬

+0

代碼有效,但這只是簡單地打印所有頁碼。問題是限制頁碼僅爲例如10個頁碼。如果用戶在第12頁上,那麼它應該顯示例如10到20等。 –