开发者

paging query problem

开发者 https://www.devze.com 2023-01-19 02:16 出处:网络
here is my paging code: function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET[\'page\']) && (int)$_GET[\'page\'] > 0) {

here is my paging code:

function getPagingQuery($sql, $itemPerPage = 10)
{
    if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
        $page = (int)$_GET['page'];
    } else {
        $page = 1;
    }

    // start fetching from this row number
    $offset = ($page - 1) * $itemPerPage;

    return $sql . " LIMIT $offset, $itemPerPage";
}

function getPagingLink($sql, $itemPerPage = 10, $strGet ="")

{
    $result        = dbQuery($sql);
    $pagingLink    = '';
    $totalResults  = dbNumRows($result);
    $totalPages    = ceil($totalResults / $itemPerPage);

    // how many link pages to show
    $numLinks      = 10;


    // create the paging links only if we have more than one page of results
    if ($totalPages > 1) {

        $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;


        if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
            $pageNumber = (int)$_GET['page'];
        } else {
            $pageNumber = 1;
        }

        // print 'previous' link only if we're not
        // on开发者_JAVA百科 page one
        if ($pageNumber > 1) {
            $page = $pageNumber - 1;
            if ($page > 1) {
                $prev = " <a href=\"$self?page=$page&$strGet/\">[Prev]</a> ";
            } else {
                $prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
            }    

            $first = " <a href=\"$self?$strGet\">[First]</a> ";
        } else {
            $prev  = ''; // we're on page one, don't show 'previous' link
            $first = ''; // nor 'first page' link
        }

        // print 'next' link only if we're not
        // on the last page
        if ($pageNumber < $totalPages) {
            $page = $pageNumber + 1;
            $next = " <a href=\"$self?page=$page&$strGet\">[Next]</a> ";
            $last = " <a href=\"$self?page=$totalPages&$strGet\">[Last]</a> ";
        } else {
            $next = ''; // we're on the last page, don't show 'next' link
            $last = ''; // nor 'last page' link
        }

        $start = $pageNumber - ($pageNumber % $numLinks) + 1;
        $end   = $start + $numLinks - 1;        

        $end   = min($totalPages, $end);

        $pagingLink = array();
        for($page = $start; $page <= $end; $page++)    {
            if ($page == $pageNumber) {
                $pagingLink[] = " $page ";   // no need to create a link to current page
            } else {
                if ($page == 1) {
                    $pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
                } else {    
                    $pagingLink[] = " <a href=\"$self?page=$page&$strGet\">$page</a> ";
                }    
            }

        }

        $pagingLink = implode(' | ', $pagingLink);

        // return the page navigation link
        $pagingLink = $first . $prev . $pagingLink . $next . $last;
    }

    return $pagingLink;
}

im calling it like:

$sql = "something";
$result     = mysql_query(getPagingQuery($sql, $rowsPerPage));  
$pagingLink = getPagingLink($sql, $rowsPerPage, $url);

now if my url is like

http://xyz/abc/list.php its working fine.

but when my url is like

http://xyz/abc/list.php?action=def

after i click on page 2 the url changes like http://xyz/abc/list.php?page2&

'action=def' part is gone so the result changes.

if i use to pass the value in $strGet as $_SERVER['QUERY_STRING']

the 1st time it is ok like http://xyz/abc/list.php?page2&action=def

but from the 2nd time onwards it gives like http://xyz/abc/list.php?page3&page2&action=def

so not getting the desired result.

whereas i want it to be like http://xyz/abc/list.php?page3&action=def

plz help.. thanxx in advance


So I consider you use $_GET['page'] ^^

$_GET['page']=$page;


$url = 'http://xyz/abc/list.php?';

foreach($_GET as $key=>$param) {

$url.=$key.'='.$param.'&';

}


use http_build_query() instead of $_SERVER['QUERY_STRING']


What i would do is before setting $page = url code. i would first echo $page before and after setting it. so that i can see exactly what the values are. And i would print echo statements before and after everytime i set $page with the url to make sure it is correct. and if in any place you can see that its not the desired output because you can see that from the echo statements then you can make the right changes to make sure the $page variable is set properly. what i would do then to set it properly is clear the $page variable and make sure that the $page variable is then set freshly with the url.

also when setting the url make sure that the $strGet variable is also echoed out first to make sure that it is the right value that you want to set. and then set the url to the $page variable.

By doing this simple debugging you are making sure all the values are correct first and you know it is before setting them.

give it a go let me know what happens PK

0

精彩评论

暂无评论...
验证码 换一张
取 消