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 电脑键盘实现触摸功能
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据
  • ¥15 multisim中关于74ls192n和DSWPK开关的问题(相关搜索:计数器)