2014-01-27 120 views
-1

我有一個分頁腳本,它工作正常。我想爲我的分頁添加一個範圍。它應該採用以下格式;PHP分頁範圍

< < < 1 | 2 | 3 | 4 | 5> >>

當我點擊「5」時,它應該是;

< < < 4 | 5 | 6 | 7 | 8> >>

同樣當點擊「8」時,它應該是;

< < < 7 | 8 | 9 | 10 | 11> >>

假設「11」是最後一頁,如果我們點擊11,它應該是;

< < < 7 | 8 | 9 | 10 | 11> >>

我的代碼是這樣的;

$recordLimit = 4; 

    $totalNumberofRecords = 100; 

    $currentpage = !empty($startPage) ? (int) $startPage : 1 ; 

    $startFrom = ($currentpage -1) * $recordLimit; 

    $limit = $startFrom.','.$recordLimit; 

    $totalPages = ceil($totalNumberOfRecords/$recordLimit); 

    $j = 0; 

    for ($i = max(1, $currentpage - 1); $i<= min($currentpage + 4, $totalPages); $i++) { 

$fairLists['pages'][$j] = $i; 

if ($startPage == $fairLists['pages'][$j] || ($j == 0 && empty($startPage)) 
    { 
    $fairLists['pages'][$j] = array($i ,"font-weight:bold;"); 
}else{ 
    $fairLists['pages'][$j] = array($i , 'font-weight:normal;'); 
} 
$j++; 

}

我想的範圍限制爲5,而當我們到達的範圍內,它應該顯示1個此前的紀錄和3條一個記錄;

如果有人知道的腳本,然後請大家幫幫我,

感謝,

阿倫

回答

0

您的問題由我解決了..我的代碼,但在使用此代碼,如果它後爲你工作,那麼請打這個意見作爲一個答案..... 代碼是

<php 
$page = isset($_GET['page']) ? $_GET['page'] : 1; 
$recordsPerPage = 10; 
$fromRecordNum = ($recordsPerPage * $page) - $recordsPerPage; 

$query = "SELECT * FROM 
     table_mstr_department 
     ORDER BY DeptId 
     LIMIT 
     {$fromRecordNum}, {$recordsPerPage}"; 


    $stmt = $con->prepare($query); 
    $stmt->execute(); 
    $num = $stmt->rowCount(); 


    echo "<div id='search_area'>"; 
     echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>"; 
       echo "<tr>"; 
        echo "<td colspan='3'></td>"; 
       echo "</tr>"; 

       echo "<tr>"; 
       //********Add New Department Button 
        echo "<td height='auto' width='auto'>"; 
         echo "<input type='submit' name='newemp' value='Add New Department'>"; 
        echo "</td>"; 
        echo "<td width='auto'></td>"; 

        //******Search Button 
        echo "<td width='auto'>"; 
          echo "<div id='search_options'>"; 
            echo "<input type='search' name='search_text' width='auto' style='height:20px;'/>"; 
            echo "<input type='button' name='search_btn' value='Search' />"; 
          echo "</div>"; 
        echo "</td>"; 
       echo "</tr>"; 

       echo "<tr>"; 
        echo "<td height='auto' width='auto'>"; 
        echo "</td>"; 
        echo "<td width='auto'></td>"; 
        echo "<td width='auto'>"; 
         echo "<div id='search_options'>"; 
         echo "</td>"; 
     echo "</tr>"; 

    echo "</table>"; 
    echo "</div>"; 

if($num>0) 
{ 

    echo '<table width="100%" id="dep_table" style="margin-top:10px;" cellspacing="1" cellpadding="2" border="0">'; 
    echo '<tr bgcolor="#4682B4">'; 
    echo '<th>Editor</th>'; 
    echo '<th>Department Id</th>'; 
    echo '<th>Department Name</th>'; 
    echo '</tr>'; 
    $i=0; 


    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
    { 
     $i++; 
     if($i % 2 == 0) 
     { 
      $bgcolor= "#6AA2C3"; 
     } 
     else 
     { 
     $bgcolor= "#A2B5CD"; 
     } 
     //extract row, this will make $row['firstname'] to just $firstname only 
     extract($row); 

     //creating new table row per record 
     echo "<tr bgcolor='$bgcolor' id='$DeptId' name='edit_tr'>"; 
    echo '<td id="edit"><input id="edit" type="radio" name="deptid" value="DeptId" ?></td>'; 

    echo "<td class='format'>{$row['DeptId']}</td>"; 
    echo "<td class='format'>{$row['DeptName']}</td>"; 
    echo "</tr>"; 
    } 


    echo "</table>"; 
} 




echo "</div>"; 

// * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ***

echo "<div id='nav_position'>"; 
    if($page>1) 
    { 
     // ********** show the first page 
     echo "<a href='" . $_SERVER['PHP_SELF'] . "' title='Go to the first page.' class='customBtn'>"; 
      echo "<span style='margin:0 .5em;'> << </span>"; 
     echo "</a>"; 

     // ********** show the previous page 
     $prev_page = $page - 1; 
     echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$prev_page}' title='Previous page is {$prev_page}.' class='customBtn'>"; 
      echo "<span style='margin:0 .5em;'> < </span>"; 
     echo "</a>"; 

    } 


    // ********** show the number paging 

    // find out total pages 
    $query = "SELECT COUNT(*) as total_rows FROM table_mstr_department"; 
    $stmt = $con->prepare($query); 
    $stmt->execute(); 

    $row = $stmt->fetch(PDO::FETCH_ASSOC); 
    $total_rows = $row['total_rows']; 

    $total_pages = ceil($total_rows/$recordsPerPage); 

    // range of num links to show 
    $range = 2; 

    // display links to 'range of pages' around 'current page' 
    $initial_num = $page - $range; 
    $condition_limit_num = ($page + $range) + 1; 

    for ($x=$initial_num; $x<$condition_limit_num; $x++) 
    { 

     // be sure '$x is greater than 0' AND 'less than or equal to the $total_pages' 
     if (($x > 0) && ($x <= $total_pages)) 
     { 

      // current page 
      if ($x == $page) 
      { 
       echo "<span class='customBtn' style='background:#0D6598;'>$x</span>"; 
      } 

      // not current page 
      else 
      { 
       echo " <a href='{$_SERVER['PHP_SELF']}?page=$x' class='customBtn'>$x</a> "; 
      } 
     } 
    } 


    // ***** for 'next' and 'last' pages 
    if($page<$total_pages) 
    { 
     // ********** show the next page 
     $next_page = $page + 1; 
     echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$next_page}' title='Next page is {$next_page}.' class='customBtn'>"; 
      echo "<span style='margin:0 .5em;'> > </span>"; 
     echo "</a>"; 

     // ********** show the last page 
     echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$total_pages}' title='Last page is {$total_pages}.' class='customBtn'>"; 
      echo "<span style='margin:0 .5em;'> >> </span>"; 
     echo "</a>"; 
    } 

    /* 
    echo "<input type='text' name='page' size='1' />"; 
    echo "<input type='submit' name='Go_btn' value='Go' class='customBtn' />";<?php */ 

echo "</div>"; 
?> 

您可以相應地修改這些代碼。此代碼顯示三列... 祝您好運.....,不要忘了喜歡這個作爲asnwer

+0

@Arun德蘭,是你的問題得到解決或不.. – Partap

+0

至少你應該回復是或否 – Partap

+0

你能舉例說明一下嗎? –

0
echo "<div id='nav_position'>"; 
if($page>1) 
{ 
    // ********** show the first page 
    echo "<a href='" . $_SERVER['PHP_SELF'] . "' title='Go to the first page.' class='customBtn'>"; 
     echo "<span style='margin:0 .5em;'> << </span>"; 
    echo "</a>"; 

    // ********** show the previous page 
    $prev_page = $page - 1; 
    echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$prev_page}' title='Previous page is {$prev_page}.' class='customBtn'>"; 
     echo "<span style='margin:0 .5em;'> < </span>"; 
    echo "</a>"; 

} 


// ********** show the number paging 

// find out total pages 
$query = "SELECT COUNT(*) as total_rows FROM table_mstr_department"; 
$stmt = $con->prepare($query); 
$stmt->execute(); 

$row = $stmt->fetch(PDO::FETCH_ASSOC); 
$total_rows = $row['total_rows']; 

$total_pages = ceil($total_rows/$recordsPerPage); 

// range of num links to show 
$range = 2; 

// display links to 'range of pages' around 'current page' 
$initial_num = $page - $range; 
$condition_limit_num = ($page + $range) + 1; 

for ($x=$initial_num; $x<$condition_limit_num; $x++) 
{ 

    // be sure '$x is greater than 0' AND 'less than or equal to the $total_pages' 
    if (($x > 0) && ($x <= $total_pages)) 
    { 

     // current page 
     if ($x == $page) 
     { 
      echo "<span class='customBtn' style='background:#0D6598;'>$x</span>"; 
     } 

     // not current page 
     else 
     { 
      echo " <a href='{$_SERVER['PHP_SELF']}?page=$x' class='customBtn'>$x</a> "; 
     } 
    } 
} 


// ***** for 'next' and 'last' pages 
if($page<$total_pages) 
{ 
    // ********** show the next page 
    $next_page = $page + 1; 
    echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$next_page}' title='Next page is {$next_page}.' class='customBtn'>"; 
     echo "<span style='margin:0 .5em;'> > </span>"; 
    echo "</a>"; 

    // ********** show the last page 
    echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$total_pages}' title='Last page is {$total_pages}.' class='customBtn'>"; 
     echo "<span style='margin:0 .5em;'> >> </span>"; 
    echo "</a>"; 
} 

/* 
echo "<input type='text' name='page' size='1' />"; 
echo "<input type='submit' name='Go_btn' value='Go' class='customBtn' />";<?php */ 

echo "</div>"; 
+0

您可以使用此代碼。它會給你的按鈕的範圍aas谷歌網站搜索按鈕結束時 – Partap

+0

謝謝!我會檢查你的代碼並回復你。 –

+0

這是否適合你?.... – Partap