【问题标题】:PHP Pagination with rangePHP分页与范围
【发布时间】:2014-01-27 14:58:09
【问题描述】:

我有一个分页脚本,它工作正常。我想在我的分页中添加一个范围。但是它应该采用以下格式;

>>

当我点击“5”时,应该是;

>>

同理,点击“8”,应该是;

>>

假设“11”是最后一页,如果我们点击 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 条下一条记录;

如果有人知道脚本,请帮助我,

谢谢,

阿伦

【问题讨论】:

    标签: php ajax pagination


    【解决方案1】:

    您的问题已由我解决。我的代码如下,但在使用此代码后,如果它适用于您,请点击此评论作为答案..... 代码如下

    <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

    【讨论】:

    • 至少你应该回答是或否
    • 你能用我的例子来说明这一点吗?
    • $recordLimit = 4; $totalNumberofRecords = 100; $currentpage = !empty( $startPage ) ? (int) $startPage : 1 ; $startFrom = ( $currentpage -1 ) * $recordLimit; $limit = $startFrom.','.$recordLimit; $totalPages = ceil($totalNumberOfRecords / $recordLimit); $j = 0;至此,您的代码还可以。但是在此之后,您的代码中没有逻辑
    • 抱歉,按照我的要求,分页工作正常,没有任何问题。我只想要指定方式的限制。如果可能的话,请给我一个提示。否则离开它。
    【解决方案2】:
     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>";
    

    【讨论】:

    • 您可以使用此代码。它会将您的按钮范围作为谷歌网站末尾的谷歌页面搜索按钮
    • 对不起,这不是我所期望的!!..我需要与我在问题中描述的格式相同的功能。
    猜你喜欢
    • 2013-09-06
    • 1970-01-01
    • 2012-03-30
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 2011-10-11
    相关资源
    最近更新 更多