weixin_33686714 2014-01-27 05:09 采纳率: 0%
浏览 2

PHP分页与范围

I've a pagination script and it is working fine. I want to add a range to my pagination. BUt it should be in the following format;

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

When I click on "5", then it should be;

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

Similarly when click on "8", then it should be;

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

Suppose "11" is the last page and if we click on 11, it should be;

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

My code is look like this;

    $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++;

}

I want to limit the range to 5, and when we reach the range, it should display 1 previous record and 3 next records;

If somebody knows the script, then please help me,

Thanks,

Arun

  • 写回答

2条回答 默认 最新

  • 胖鸭 2014-01-27 05:29
    关注

    your problem is solved by me.. My code is as under but after using this code if it works for you then please hit this comment as an answer..... Code is as

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

    You can modify this code accordingly. This code displays three columns... Best of luck..... and do not forgot to like this as an asnwer

    评论

报告相同问题?

悬赏问题

  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?