douweicheng5532 2014-03-17 15:49
浏览 31
已采纳

在阵列完成后,会话阵列的分页不会停止

I have a PHP Page that displays all of my SESSION's arrays using pagination. The pagination displays ten arrays a page. Currently, my session is holding eleven arrays. My problem is that when I go to the second pagination page which contains my eleventh array, it keeps displaying more arrays that are empty. The pagination keeps going on and on, until a million probably. I would like it to end right at the eleventh array preferably. If for example there was twelve arrays I would like it to end and stop paginating at the twelfth array. Here is an image of my problem:

enter image description here

Here is my full PHP page code:

<? 
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 
?>

And here is my config.php page's full code:

<?php
$mysqli = mssql_connect('gdm','Ga','Rda!');  
$objConnectee = mssql_select_db('Gdg',$mysqli ); 
?>

If anyone can tell me how I can fix this problem it would be greatly appreciated. Thank you for any help.

  • 写回答

1条回答 默认 最新

  • dota220141003 2014-03-17 16:08
    关注

    mssql_fetch_object returns FALSE if there are no more results, so you could stop execution with an if statement:

    $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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来