PHP中的分页仅每页更新1行

时间:2016-01-26 02:34:12

标签: php mysql pagination

需要帮助修复我的分页。我限制每页显示5行,每次进入下一页时,它只更新底部的1行而不是每页显示5个新项目。

这是我现在拥有的。对不起,我发布了很长的代码。目前,我所做的解决方法是将$ listcount设置为1000,以显示同时禁用分页的所有项目。

    <?php
    $global_connection = db_connect();
    $tabledata = "";

    $totalpages = 0;
    $curpageoffset = 0;
    $listcount = 5;
    $pageparam = "";

    $colspan = 7;
    $isownpost = 0;


    if ($global_connection) {
        $ctr = 0;
        $orderbyclause = "order by buyinfo_created";
        $limitstr = "";
        $addtable = "";
        $clause = "and buyinfo.buyinfo_deadline>='".date('Y-m-d H:i:s')."' and buyinfo.buyinfo_status<3";
        if (isset($_REQUEST['recommend'])) {
            $pageparam .= "&recommend=".$_REQUEST['recommend'];
            $sectiontitle = "Recommended posts";
            $addtable = ",recommend";
            $clause .= " and recommend.buyinfo_id=buyinfo.buyinfo_id and recommend.user_id=".util_getuserid();
        } else if (isset($_REQUEST['member'])) {
            $pageparam .= "&member=".$_REQUEST['member'];
            $orderbyclause .= " desc";
            if ($_REQUEST['member'] == util_getuserid() || $_REQUEST['member'] == 0) {
                $isownpost = 1;
                $colspan--;
                $sectiontitle = "My Posts";
                // Do not hide completed/expired items
                $clause = " and user.user_id='".util_getuserid()."'";
            } else {
                $sectiontitle = "Posts by ".db_getvalue("select user_login from user where user_id='".$_REQUEST['member']."'", $global_connection);
                $clause .= " and user.user_id='".$_REQUEST['member']."'";
            }
        } else {
            $sectiontitle = "Ongoing Posts";
        }
        $totalrows = db_getvalue("select count(buyinfo_id) from buyinfo,user $addtable where buyinfo.user_id=user.user_id $clause $orderbyclause", $global_connection);
        if ($totalrows == "")
            $totalrows = 0;
        $totalpages = ceil($totalrows/$listcount);
        if ($totalpages > '1') {
            $curpageoffset = 0;
            if (isset($_REQUEST['offset'])) {
                if ($_REQUEST['offset'] >= $totalpages)
                    $curpageoffset = $totalpages - 1;
                else if ($_REQUEST['offset']>0)
                    $curpageoffset = $_REQUEST['offset'];
            }
            $limitstr = " limit $curpageoffset,$listcount";
        }

        $classname = "postlist-oddrow";
        $buyresults = db_query("select * from buyinfo,user $addtable where buyinfo.user_id=user.user_id $clause $orderbyclause $limitstr", $global_connection);
        while ($buyrow = db_getnextdata($buyresults)) {
            $ctr++;
            $totalbids = db_getvalue("select count(buyinfoquote_id) from buyinfoquote where buyinfo_id='".$buyrow['buyinfo_id']."'", $global_connection);
            $tabledata.= "<tr class='$classname'>";
//          $tabledata.= "<td align=right>".util_numfmt($ctr)."</td>";
            $tabledata.= "<td><a href='/posts/?item=".$buyrow['buyinfo_id']."' target='_blank'><h4>".util_displaystatictext($buyrow['buyinfo_title'])."</h4></a></td>";
            $tabledata.= "<td>";
            $exampledata = "<table  class='posts-imagerholder'><tr>";
            $imgctr = 0;
            $imgresults = db_query("select buyinfoexample_imgurl from buyinfoexample where buyinfo_id=".$buyrow['buyinfo_id'], $global_connection);
            while ($imgrows = db_getnextdata($imgresults)) {
                $imgctr++;
                $exampledata.= "<td align=center valign=top><img  alt='loading...' style='border: none;' class='posts-image' src='" . $imgrows['buyinfoexample_imgurl'] . "' /></td>";
            }
            if ($imgctr == 0)
                $exampledata = "No examples";
            else
                $exampledata.= "</tr></table>";

            $tabledata.= "$exampledata</td>";
            if ($buyrow['user_id'] === '2') {
                    $tabledata.= "<td><a href='/posts/?item=".$buyrow['buyinfo_id']."' target='_blank'>".util_displaystatictext($buyrow['buyinfo_name'])."</a></td>";
            } else {
                    $tabledata.= "<td><a href='' OnClick='util_reload(\"profile/?member=".$buyrow['user_id']."\"); return false;'>".util_displaystatictext($buyrow['buyinfo_name'])."</a></td>";
            }
            $tabledata.= "<td>".$buyrow['buyinfo_budget']."</td>";
            $tabledata.= "<td>".$buyrow['buyinfo_notes']."</td>";
            $tabledata.= "<td>".util_datetimefmt($buyrow['buyinfo_created'])."</td>";
            $tabledata.= "</tr>";

            if ($classname == "postlist-oddrow")
                $classname = "postlist-evenrow";
            else
                $classname = "postlist-oddrow";
        }
        if ($ctr == 0) {
            if (isset($_REQUEST['recommend']))
                $tabledata = "<tr><td colspan='$colspan' align='center'>We have no recommendations. Please try updating your keywords in your profile.</td></tr>";
            else
                $tabledata = "<tr><td colspan='$colspan' align='center'>No posts</td></tr>";
        }
        db_disconnect($global_connection);
    } else {
        $tabledata = "<tr><td colspan='$colspan' align='center'>Server Error</td></tr>";
    }
?>
<div style='background-color: #f4f4f4; padding: 60px 0px;'>

<table width=90%>
<tr><td>
<h4><?php 

$src=$_SESSION['PROFILE_PICTURE'];
echo "<img src='".$src."' width='50px'>  ".$_SESSION['FULLNAME']."<br/><br/>" ;
echo  "<img src='$global_baseurl"."img/postsicon.png'> $sectiontitle"; 
?></h4>
<table border=0 cellspacing=0 cellpadding=5 class='postlist'>
<?php
    $postcolnames = "<tr class='postlist-colname'>";

    $postcolnames .= "<td>Title</td>";
    $postcolnames .= "<td align=center >Examples</td>";
    if ($isownpost == 0) {
        $postcolnames .= "<td>Buyer</td>";

    }
    $postcolnames .= "<td>Budget</td>";
    $postcolnames .= "<td>Notes</td>";
    $postcolnames .= "<td>Date Posted</td>";
    $postcolnames .= "</tr>";

    echo $postcolnames;
    echo $tabledata;
    echo str_replace("postlist-colname", "postlist-colname postlist-evenrow", $postcolnames);
?>
</table>
</td></tr>
<?php
    if ($totalpages>1) {
            echo "<tr><td><ul class='pagination'>";
            echo "<li><a href='' aria-label='Previous' ";
            if ($curpageoffset == 0) {
                echo "onclick='return false;'";
            } else {
                echo "onclick='util_reload(\"posts/?offset=".($curpageoffset-1)."$pageparam\"); return false;'";
            }
            echo " ><span aria-hidden='true'>&laquo;</span></a></li>";

            $tmpctr = 0;
            while ($tmpctr < $totalpages) {
                $curstyle = "";
                if ($tmpctr == $curpageoffset)
                    $curstyle = "class='active'";

                echo "<li $curstyle><a href='' onclick='util_reload(\"posts/?offset=$tmpctr$pageparam\"); return false;'>";
                $tmpctr++;
                echo "$tmpctr</a></li>";
            }

            echo "<li><a href='' aria-label='Next' ";
            if ($curpageoffset >= $totalpages-1) {
                echo "onclick='return false;'";
            } else {
                echo "onclick='util_reload(\"posts/?offset=".($curpageoffset+1)."$pageparam\"); return false;'";
            }
            echo " ><span aria-hidden='true'>&raquo;</span></a></li>";

            echo "</ul></td></tr>";
    }
?>
</table>
</div>

2 个答案:

答案 0 :(得分:2)

$curpageoffset = $_REQUEST['offset'];

应该是

$curpageoffset = $_REQUEST['offset']*$listcount-1;

目前您将开始位置增加,您应该按页数乘以限制(减去1)

答案 1 :(得分:0)

$totalpages = ceil($totalrows/$listcount);
    if ($totalpages > '1') {
        $curpageoffset = 0;
        if (isset($_REQUEST['offset'])) {
            if ($_REQUEST['offset'] >= $totalpages)
                $curpageoffset = $totalpages - 1;
            else if ($_REQUEST['offset']>0)
                $curpageoffset = $_REQUEST['offset'];
        }
        $limitstr = " limit $curpageoffset,$listcount";
    }

currentoffset,listcount,currentpage,totalpages,requestoffset

$currentPage = ceil($requestOffset/$listcount);
if ($currentPage > $totalpages) {$currentpage = 0;}
$currentoffset = $currentPage * $listcount;

然后

$limitstr = " limit $curpageoffset,$listcount";