2011-12-02 11 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); 

Đây là mã tôi sử dụng để tạo ra trang pagination cho dữ liệu search.But Tôi chỉ nhận thấy rằng tôi có quá nhiều kỷ lục sau đó nó sẽ có quá nhiều numbers.I'm trang cố gắng để hạn chế số trang được hiển thị và tìm nhiều ví dụ .. có nhiều cách để thực hiện nhưng tôi vẫn không biết cách giới hạn nó cho phương pháp của mình ...Giới hạn số trang pagination

Trả lời

28

Ok nếu bạn có ý nghĩa thể hiện một cái gì đó giống như

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

Điều đầu tiên chúng ta cần phải là tạo ra một chức năng mà có thể xử lý những gì chúng ta cần để tạo ra phân trang. Heres một chức năng tôi sử dụng và nó hoạt động tốt.

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 

} 

Bây giờ chức năng này là khá vững chắc và hoạt động rất tốt đối với tôi.

Vì vậy, bạn vượt qua chức năng này

  • $ tot_rows = đếm hàng cho truy vấn số
  • $ pp = mỗi trang
  • $ curr_page = trang hiện

Ok, bây giờ bạn có dữ liệu bạn cần, bạn sẽ cần hiển thị dữ liệu đó.

Heres những gì tôi sử dụng và vui lòng đọc nó trước khi bạn nghĩ, 'ah, quá dài'. Nó thực sự rất đơn giản.

Heres một bản chụp của những gì nó sẽ trở lại

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

Cảm ơn bạn rất nhiều vì ví dụ của bạn và tất cả các hướng dẫn, tôi hiện đang cố gắng để hiểu và tìm hiểu nó. –

+0

Rất vui được trợ giúp. Rõ ràng, tôi đã nắm lấy điều này từ mã của tôi và thay đổi nó một chút nhưng cấu trúc là có, cho bạn để chơi với. –

+1

Sự cố với loại điều hướng trang này là khó có thể truy cập các trang ở giữa phạm vi '...' (ít nhất bằng cách nhấp vào). Bạn có thể xem xét sử dụng cách tiếp cận tôi mô tả trong [câu hỏi này] (http://stackoverflow.com/q/7835752/999120), cho phép truy cập dễ dàng vào bất kỳ trang nào trong một vài mouseclicks, ngay cả khi có 1000 trang. – Doin

2

Bạn sẽ cần sử dụng mệnh đề LIMIT trong Câu lệnh sql. Ví dụ:

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

tham khảo: SELECT Syntax

+0

Cảm ơn bạn đã trả lời. Có phải hàng này không? '$ StrSQL. =" Order by addedrec_ID DESC LIMIT $ Page_Start, $ Per_Page "; ' –

+0

Thanh toán http://php.about.com/od/mysqlcommands/g/Limit_sql.htm – Cyclonecode

2

tôi giải quyết một vấn đề rất tương tự cho người khác với một kịch bản rất giống nhau ở đây: How to limit pages shown in pagination script

Ngoài ra, tôi nhận thấy một vài bất thường khác với kịch bản của bạn (bên cạnh định dạng không đọc được mà tôi đã sửa). Bạn nên thay đổi tất cả các lần xuất hiện của $_SERVER[SCRIPT_NAME] thành $_SERVER['SCRIPT_NAME']. Trong kịch bản của bạn, ví dụ:

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

Sẽ trở thành:

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

Cảm ơn bạn đã liên kết và chỉnh sửa, tôi sẽ dùng thử ngay bây giờ. –

2

mã của tôi luôn hiển thị ít nhất 9 trang trong trường hợp tổng số trang> 9 bất cứ điều gì bạn đang trên trang 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

Mục đích của mã này chỉ là để hiển thị rõ ràng (chắc chắn) số trang trong pagination

Nhận số trang hiện tại từ url và xác định $curr_pg_numb

số Nhận tổng trang từ mysql và xác định $total_pages

Và đây là mã cho pagination

$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 = ceil ($ tổng số/$ items_per_page);

2

làm cho nó đơn giản, đây là mã:

<?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; 

>

tôi đã không kiểm tra nó, nhưng tôi nghĩ rằng nó phải làm việc :-)

Cind regards, Ivan

?
+0

Mã hoạt động nhưng điều này chỉ đơn giản là in tất cả các số trang. Câu hỏi đặt ra là giới hạn số trang chỉ cho ví dụ 10 số trang. nếu người dùng ở trang 12 thì nó sẽ hiển thị ví dụ từ 10 đến 20, v.v. –