如何检查分页中哪个页面处于活动状态

时间:2016-07-07 09:03:09

标签: php

我正在尝试创建一个博客,我使用的分页脚本类似于下面的那个,它就像一个魅力,但我想检查用户正在浏览的页面,并将该页面的按钮设为class = btn btn-success btn-md active。 Atm所有页面的按钮颜色都是“活动”颜色,如何在最好的mannor中进行排序?

<?php
$rowsPerPage = 10;

try
{
$conn = new PDO( "sqlsrv:server=.\sqlexpress ; Database=", "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{ 
die( print_r( $e->getMessage() ) ); 
}

try
{
    $tsql = "SELECT COUNT(ProductNumber) FROM Production.Product";
    $stmt = $conn->query($tsql);
    $rowsReturned = $stmt->fetch(PDO::FETCH_NUM);
    if($rowsReturned[0] == 0)
    {
        echo "No rows returned.";
    }
    else
    {     
        $numOfPages = ceil($rowsReturned[0]/$rowsPerPage);
        for($i = 1; $i<=$numOfPages; $i++)
        {
            $pageNum = "index.php?page=$i";

       <------HERE IS THE PART I NEED HELP WITH------------>

            print("<a href='$page' class='btn btn-success btn-md active'>$i</a>&nbsp;");
        }
        echo "<br/><br/>";
    }
    $tsql = "SELECT * FROM 
                (SELECT ROW_NUMBER() OVER(ORDER BY ProductID) 
                             AS RowNumber, 
                                Name,    
                                ProductNumber
             FROM Production.Product)
             AS Temp
             WHERE RowNumber BETWEEN ? AND ?";

    $stmt2 = $conn->prepare($tsql);

    if(isset($_GET['pageNum']))
    {
        $highRowNum = $_GET['pageNum'] * $rowsPerPage;
        $lowRowNum = $highRowNum - $rowsPerPage + 1;
    }
    else
    {
        $lowRowNum = 1;
        $highRowNum = $rowsPerPage;
    }
    $params = array(&$lowRowNum, &$highRowNum);
    $stmt2->execute(array($lowRowNum, $highRowNum));
    print("<table border='1px'>
            <tr>
                <td>Row Number</td>
                <td>Product Name</td>
                <td>Product ID</td>
            </tr>");
    while($row = $stmt2->fetch(PDO::FETCH_NUM) )
    {
        print("<tr>
                <td>$row[0]</td>
                <td>$row[1]</td>
                <td>$row[2]</td>
              </tr>");
    }
    print("</table>");
}
catch(Exception $e)
{ 
    die( print_r( $e->getMessage() ) ); 
}

?>

1 个答案:

答案 0 :(得分:1)

嗨当您使用此$pageNum = "index.php?page=$i";时,您可以通过以下代码检查哪个页面处于活动状态

 $activepage="";
         if(isset($_GET["page"])){
            $activepage=$_GET["page"];
            }

更改打印功能

if ($activepage == $i){
    print("<a href='$page' class='btn btn-success btn-md active'>$i</a>&nbsp;");
                }
                else{   
        print("<a href='$page'>$i</a>&nbsp;");
                }