首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在数组完成后,会话数组的分页不会停止

在数组完成后,会话数组的分页不会停止
EN

Stack Overflow用户
提问于 2014-03-17 15:49:37
回答 1查看 78关注 0票数 0

我有一个PHP页面,它使用分页显示会话的所有数组。分页每页显示十个数组。目前,我的会话有11个数组。我的问题是,当我转到包含第11个数组的第二个分页页面时,它会继续显示更多的空数组。分页一直在进行,大概有一百万。我希望它在第十一数组的右边结束,最好是。例如,如果有12个数组,我希望它结束并停止在第12个数组上分页。下面是我的问题的一张图片:

以下是我的完整PHP页面代码:

代码语言:javascript
复制
<? 
session_start();//start session for this page 
include_once("config.php");
    //instantiate variables 
    $currentpage = isset($_GET['pagenum']) ? (integer)$_GET['pagenum'] : 0; 
    $numperpage = 10; //number of records per page 
    $numofpages = count($_SESSION['products'])/$numperpage; //total num of pages 
    $first = 0; //first page 
    $last = $numofpages; 

    if($currentpage==0){ //which page is previous 
        $previous = 0; //if very first page then previous is same page 
    }else{ 
        $previous = $currentpage-1; 
    } 

    if($currentpage==$last-1){//which page is last 
        $next = $currentpage; 
    }else{ 
        $next = $currentpage+1; 
    } 
    echo '<form method="post" action="PAYMENT-GATEWAY">';
    echo '<ul>';
    $cart_items = 0;

    $cart_itm = $_SESSION['products'];
    for($x=($currentpage*10);$x<(($currentpage*10)+10);$x++){ //output data 
        $product_code = $cart_itm[$x]["code"];
           $queryy = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code'";
           $results = mssql_query($queryy, $mysqli);
           $obj = mssql_fetch_object($results);
           if ($obj) {
            echo ($x+1)." ".$cart_itm[$x]["code"]."<br>"; 
            echo '<li class="cart-itm">';
            echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm[$x]["code"].'&return_url='.$current_url.'">&times;</a></span>';
            echo '<div class="p-price">'.$currency.$obj->price.'</div>';
            echo '<div class="product-info">';
            echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
            echo '<div class="p-qty">Qty : '.$cart_itm[$x]["qty"].'</div>';
            echo '<div>'.$obj->product_desc.'</div>';
            echo '</div>';
            echo '</li>';
            $subtotal = ($cart_itm[$x]["price"]*$cart_itm[$x]["qty"]);
            $total = ($total + $subtotal);

            echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
            echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
            echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
            echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm[$x]["qty"].'" />';
            $cart_items ++;
            } else {   
            break;  //if no more results, break the while loop
            }           

        }

        echo '</ul>';
        echo '<span class="check-out-txt">';
        echo '<strong>Total : '.$currency.$total.'</strong>  ';
        echo '</span>';
        echo '</form>';
        echo '<a href="checkout.php">Checkout</a>';
    echo "<a href='page.php?pagenum=".$previous."'>Previous</a>                        <a href='page.php?pagenum=".$next."'>Next</a>"; //hyperlinks controls 
?>

下面是我的config.php页面的完整代码:

代码语言:javascript
复制
<?php
$mysqli = mssql_connect('gdm','Ga','Rda!');  
$objConnectee = mssql_select_db('Gdg',$mysqli ); 
?>

如果有人能告诉我如何解决这个问题,我们将不胜感激。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-17 16:08:53

如果没有更多的结果,mssql_fetch_object将返回FALSE,因此可以使用if语句停止执行:

代码语言:javascript
复制
$obj = mssql_fetch_object($results);
if ($obj) {
    echo '<li class="cart-itm">';

    //etc... as before all the way down to
    $cart_items ++;
} else {   
    break;  //if no more results, break the for loop
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22458939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档