Pagination in PDO PHP

前端 未结 2 783
攒了一身酷
攒了一身酷 2020-12-12 04:00

im trying to learn pagination with PHP/PDO.

            $limit = 20;
            $sth = $conn->prepare(\"SELECT * FROM directory WHERE user_active != \'\'         


        
2条回答
  •  情书的邮戳
    2020-12-12 04:30

    Please use Below code for Pagination using PDO:

    
    
    
    
    Simple Pagination Demo using PDO Query
        
    
    
    
    getMessage();    }
    // No. of adjacent pages shown on each side
    $adjacents = 2;
    // We will assign variable here for entry By. you can use your variables here.
    $EntryBy = "furqan.aziz";
    // We Will prepare SQL Query
    $STM = $dbh->prepare("SELECT ServerName FROM statstracker WHERE EntryBy = :EntryBy");
    // bind paramenters, Named paramenters alaways start with colon(:)
    $STM->bindParam(':EntryBy', $EntryBy);
    // For Executing prepared statement we will use below function
    $STM->execute();
    // Count no. of records 
    $Records = $STM->rowCount();
    // Your File Name will be the same like your php page name which is index.php
    $targetpage = "index.php";
    // Below is setting for no. of records per page.
    $limit = 10; 
    $page = $_GET['page'];
    if($page) 
    //First Item to dipaly on this page
        $start = ($page - 1) * $limit;          
    else
    //if no page variable is given, set start to 0
        $start = 0;                             
    // Get data using PDO prepare Query.
    $STM2 = $dbh->prepare("SELECT `SrNo`, `ServerName`, `HiMemUti`, `AvgMemUti`, `HiCpuUti`, `AvgCpuUti`, `HiIOPerSec`, `AvgIOPerSec`, `HiDiskUsage`, `AvgDsikUsage`, `EntryBy` FROM statstracker WHERE EntryBy = :EntryBy ORDER BY SrNo LIMIT $start, $limit");
    // bind paramenters, Named paramenters alaways start with colon(:)
    $STM2->bindParam(':EntryBy', $EntryBy);
    // For Executing prepared statement we will use below function
    $STM2->execute();
    // We will fetch records like this and use foreach loop to show multiple Results later in bottom of the page.
     $STMrecords = $STM2->fetchAll();
    // Setup page variables for display. If no page variable is given, default to 1.
    if ($page == 0) $page = 1;
    //previous page is page - 1                 
    $prev = $page - 1;
    //next page is page + 1                     
    $next = $page + 1;
    //lastpage is = total Records / items per page, rounded up.                         
    $lastpage = ceil($Records/$limit);
    //last page minus 1 
    $lpm1 = $lastpage - 1;                      
    //Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once.
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "\n";       
    }
    //Below is a start of table in which we will show records using foreach loop.
    echo "";
    // For Exporting Records to Excel we will send $EntryBy in link and will gate it on ExportToExcel page for stats for this user. 
    echo"";
    echo"Export To Excel";
    echo"";
    // We use foreach loop here to echo records.
    foreach($STMrecords as $r)
        {
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";  
        }
    echo "
    Simple Pagination Demo using PDO Queries
    Sr#ServerHi Memory UtilizationAvg Memory UtilizationHi CPU UtilizationAvg CPU UtilizationHi I/O UtilizationAvg I/O UtilizationHi Disk UsageAvg Disk UsageBy
    " .$r[0] ."" .$r[1] ."" .$r[2] ."" .$r[3] ."" .$r[4] ."" .$r[5] ."" .$r[6] ."" .$r[7] ."" .$r[8] ."" .$r[9] ."" .$r[10] ."
    "; // For showing pagination below the table we will echo $pagination here after . For showing above the table we will echo $pagination before echo $pagination; // Closing MySQL database connection $dbh = null; ?>

    For more details please visit below sites for source code and CSS styles

    1. Click here!

    2. Click here!

    提交回复
    热议问题