Php 会话数组的分页赢得';数组完成后,不能停止
我有一个PHP页面,它使用分页显示会话的所有数组。分页每页显示十个数组。目前,我的会话包含11个数组。我的问题是,当我转到包含第十一个数组的第二个分页页面时,它会不断显示更多的空数组。分页不停地进行,可能一直到一百万页。我希望它最好在第十一排结束。例如,如果有12个数组,我希望它在第12个数组结束并停止分页。以下是我的问题的图像: 以下是我的完整PHP页面代码:Php 会话数组的分页赢得';数组完成后,不能停止,php,arrays,session,pagination,Php,Arrays,Session,Pagination,我有一个PHP页面,它使用分页显示会话的所有数组。分页每页显示十个数组。目前,我的会话包含11个数组。我的问题是,当我转到包含第十一个数组的第二个分页页面时,它会不断显示更多的空数组。分页不停地进行,可能一直到一百万页。我希望它最好在第十一排结束。例如,如果有12个数组,我希望它在第12个数组结束并停止分页。以下是我的问题的图像: 以下是我的完整PHP页面代码: <? session_start();//start session for this page include_once
<?
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.'">×</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
?>
如果有人能告诉我如何解决这个问题,我将不胜感激。感谢您的帮助。mssql\u fetch\u对象在没有更多结果时返回FALSE,因此您可以使用if语句停止执行:
$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
}
$obj=mssql\u fetch\u对象($results);
如果($obj){
echo';
//等等…像以前一样一直到
$cart_items++;
}否则{
break;//如果没有更多结果,请中断for循环
}
它可以工作,不再显示那些额外的数组,但由于某些原因,它不会在第二页显示第十一个数组。我已将问题中的代码更新为当前的代码。在该特定页面上,$currentpage==1,是吗?是,链接是page.php?pagenum=1
。
$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
}