在PHP PDO中使用分页从数据库加载数据

时间:2018-05-08 06:40:40

标签: php pdo

我有一个使用PHP PDO编程开发的网站。在这个网站上有一个页面,其中列出了假日包的详细信息以及分页。我的问题是只有6个数据库记录在页面中加载,但分页显示多个页面。实际上我是第一次使用PDO而且我无法解决这个问题。

这是我的代码

index.class(所有数据库函数都写在这里)

<?php
include_once "class/common.php";
class INDEX {
var $doAction;  
var $msg;
var $objScr;
var $objCommonFunc;     // This is common function object.
var $rsltInfo;          // A common result set used by this class in various queries.
var $rsltOp;            // A common result set used by this class in various queries.
var $arrSysVar;         // Array that stores the System Variable those are used in screen.

// Other variables
var $adminEmail;
var $arrMetaContent;    // For storing content and meta information.
var $leftMenuId;        // Recid of 'menugroup' table for managing leftbar.
var $rightMenuId;       // Recid of 'menugroup' table for managing rightbar.
var $menuPos;   

function getHolydays($rgnId,$start_from, $record_per_page){

    $qrySel = "SELECT h.recid recid, h.regionid regionid, h.name name, h.subtitle subtitle, h.txtDays txtDays, h.txtNights txtNights, h.pricingfrom pricingfrom, h.thumb_img thumb_img 
    FROM holydays h";
    if($rgnId!=0){ $qrySel.= " WHERE h.regionid=".$rgnId; }
    $qrySel .=" LIMIT $start_from,$record_per_page";
    $objDbResult = $this->rsltInfo->getResult($qrySel);
    return $objDbResult;
}

function getHolydaysPagntnCount($rgnId){

    $qrySel = "SELECT h.recid recid, h.regionid regionid, h.name name, h.subtitle subtitle, h.txtDays txtDays, h.txtNights txtNights, h.pricingfrom pricingfrom, h.thumb_img thumb_img 
    FROM holydays h";
    if($rgnId!=0){ $qrySel.= " WHERE h.regionid=".$rgnId; }
    //echo $qrySel ;
    $objDbResult = mysql_query($qrySel);
    return $objDbResult;
}
}
$objScr = new INDEX();
?>

国际-holidays.php

            <?php $active_menu = 'Holidays'; 
        include_once("index.class.php");
        include_once 'header.php';
        $objScr = new INDEX();

        //$region_name="";
        //if($_REQUEST['mcId'])$mcId=$_REQUEST['mcId'];
        $rgnId=0;
        if($_REQUEST['rgnId'])$rgnId=$_REQUEST['rgnId'];

        if($_REQUEST['pgId'])
        $pgId=$_REQUEST['pgId'];
        $start_from=!empty($_REQUEST['page'])?$_REQUEST['page']:0;
        //echo $start_from;
        $record_per_page=6;



        $rsltrgn = $objScr->getRegionname();
        $rsltsdrgn = $objScr->getRegionname();
        $rsltholyDetails = $objScr->getHolydays($rgnId,$start_from, $record_per_page);
        $holydetailscount = mysql_numrows($objScr->getHolydayscount());

        $holydaycount = mysql_numrows($objScr->getHolydaysPagntnCount($rgnId));
        $total_pages = ceil($holydaycount/$record_per_page);

        $rsltbannerDets= $objScr->getbannerImg($rgnId);
        $rowbannerdts = $rsltbannerDets->fetchAssoc();
        ?>              
        <div class="col-md-8 col-sm-12 col-8">
        <div class="right">
        <div class="pagination-wrp clearfix">
        <nav class="nav-pagination">
        <?php                                //Here is the code for pagination
        if($total_pages>0){?>

        <ul class="pagination">
        <?php if($pgId>1){?>
        <li class="active"> <?php $prvpages=$start_from-6; $prev = ($pgId - 1); ?> 
        <a href="international-holidays-details.php?<?php echo "page=".$prvpages."&rgnId=".$rgnId."&pgId=".$prev; ?>" aria-label="Previous">
        <img src="images/left-arrow.svg" alt=""></a></li>
        <?php }?>
        <?php
        if($pgId!="" && $pgId!=1){ $spage=$pgId-1; }else{ $spage=1; $pgId=1; }
        $maxPages=5;
        $maxDisplayPage = $pgId + $maxPages - 1;

        if($total_pages-$spage<5){ $spage=$total_pages-5; }
        if($spage<=0){ $spage=1; }

        for($i=$spage;$i<=min($maxDisplayPage, $total_pages);$i++){

        if($i==1){ $st=0; }
        else{
        $st = ($i - 1) * $record_per_page;
        }
        ?>
        <li <?php if($i==$pgId){?> class="active" <?php }?>>
        <a href="international-holidays-details.php?<?php echo "page=".$st."&rgnId=".$rgnId."&pgId=".$i; ?>" >
        <?php echo $i; ?></a>
        </li>

        <?php 
        }

        ?>  
        <li class="active"><?php $nextpages=$start_from+6; if($pgId<$total_pages){ $next = ($pgId + 1); ?> 
        <a href="international-holidays-details.php?<?php echo "page=".$nextpages."&rgnId=".$rgnId."&pgId=".$next; ?>" aria-label="Next">
        <img src="images/right-arrow.svg" alt=""> </a></li> 
        <?php }?></ul><?php }?>

        </nav>
        </div>

        <?php
        if($holydetailscount > 0) {
        while($rowhly = $rsltholyDetails->fetchAssoc())
        {
        //$catgryname=str_split("Hello",3);

        ?>
        <div class="col-sm-6 wow fadeInUp">              
                                         //Here the holiday packages as shown in boxes.
        <a href="international-holidays-details.php?holyId=<?php echo $rowhly['recid']; ?>" class="offer-thumb">
        <div class="offer-img"><img src="../sysimages/origimages/<?php echo $rowhly['thumb_img']; ?>" class="w-100" alt=""></div>
        <div class="details clearfix">
        <div class="title"><?php echo $rowhly['name']; ?><span><?php echo $rowhly['subtitle']; ?> </span></div>
        <div class="price"><span>aed <?php echo $rowhly['pricingfrom']; ?></span>
        <?php echo $rowhly['txtDays']; ?> days &nbsp;<?php echo $rowhly['txtNights']; ?> night</div>
        </div>
        </a>
        </div>
        <?php } }?>


        </div>
        </div>

相同的代码在其他页面中完美运行。事情是分页显示5页,但是当我点击任何页面例如2时,它不会加载第二页而是显示第一页本身。 我不明白发生了什么。此代码由其他人开发,我的任务是对此进行一些更新。 任何人都可以帮助我..我真的需要完成这件事。

1 个答案:

答案 0 :(得分:-1)

如果你使用这个插件怎么样?就我而言,它非常有用。

https://mottie.github.io/tablesorter/beta-testing/example-pager-custom-controls.html

或者您可以使用mysql手动完成。

Pagination using MySQL LIMIT, OFFSET