SESSION阵列的分页不是正确的Paginating Arrays

时间:2014-03-14 20:47:07

标签: php arrays session pagination shopping-cart

我有一个PHP页面,用于检索$ _SESSION ['products']会话中的数组。该会话中的每个阵列都是用户添加到其“购物车”的产品。目前我的会话有11个阵列,这意味着我已经将11个产品添加到购物车中。我现在正在尝试在view_cart.php页面上显示数组,并将它们分成10个。基本上我希望页面显示前十个数组,然后在view_cart.php?Page=2上显示第十一个数组。现在,代码在第一页上显示前十个结果,但在view_cart.php?Page=2上,它会再次显示相同的十个结果。

以下是view_cart.php页面的完整代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
session_start();
include_once("config.php");

$objConnect = mssql_connect('gdafgm','gdacf','Rdas!');  
$objDB = mssql_select_db('Gdar',$objConnect ); 

$strSQL = "SELECT * FROM products WHERE 1=1 ".$cheack." ORDER BY id ASC"; 

$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");  
$Num_Rows = mssql_num_rows($objQuery);  

$Per_Page = 10;   // Per Page  
$Page = $_GET["Page"];  
if(!$_GET["Page"])  
{  
$Page=1;  
}  

$Prev_Page = $Page-1;  
$Next_Page = $Page+1;  

$Page_Start = (($Per_Page*$Page)-$Per_Page);  
if($Num_Rows<=$Per_Page)  
{  
$Num_Pages =1;  
}  
else if(($Num_Rows % $Per_Page)==0)  
{  
$Num_Pages =($Num_Rows/$Per_Page) ;  
}  
else  
{  
$Num_Pages =($Num_Rows/$Per_Page)+1;  
$Num_Pages = (int)$Num_Pages;  
}  
$Page_End = $Per_Page * $Page;  
IF ($Page_End > $Num_Rows)  
{  
$Page_End = $Num_Rows;  
}  
?>

<?php
    if(isset($_SESSION["products"]))
    {
        $total = 0;
        echo '<form method="post" action="PAYMENT-GATEWAY">';
        echo '<ul>';
        $cart_items = 0;
$i = 0;
foreach ($_SESSION['products'] as $cart_itm)
   {
     if(++$i > 10) break;
            $product_code = $cart_itm["code"];
           $queryy = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code' LIMIT " . $Page_Start . "," . $Per_Page;
           $results = mssql_query($queryy, $mysqli);
           $obj = mssql_fetch_object($results);

            echo '<li class="cart-itm">';
            echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["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["qty"].'</div>';
            echo '<div>'.$obj->product_desc.'</div>';
            echo '</div>';
            echo '</li>';
            $subtotal = ($cart_itm["price"]*$cart_itm["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["qty"].'" />';
            $cart_items ++;

        }

        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>';
    }

?>
</body>
</html>

以下是我的config.php页面的所有代码:

<?php
$mysqli = mssql_connect('gda','Gbc','Rgda');  
$objConnectee = mssql_select_db('Gbdac',$mysqli ); 
?>

以下是我在运行view_cart.php页面时遇到的错误:

Warning: mssql_query() [function.mssql-query]: message: Must declare the scalar variable "@start". (severity 15) in D:\Hosting\4502990\html\partscatalogue\view_cart4.php on line 65

Warning: mssql_query() [function.mssql-query]: Query failed in D:\Hosting\4502990\html\partscatalogue\view_cart4.php on line 65

Warning: mssql_fetch_object(): supplied argument is not a valid MS SQL-result resource in D:\Hosting\4502990\html\partscatalogue\view_cart4.php on line 66

感谢您的帮助。非常感谢所有帮助。

2 个答案:

答案 0 :(得分:1)

不解决它:

  

您必须使用以下行替换$ queryy作业:

$query = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code' LIMIT " . $Page_Start .
     

&#34;&#34; 。 $ Per_Page;

     

Haven未在您的代码上对此进行测试,如果不是,您可以发布结果吗?   解决你的问题?还要想想MySQL的LIMIT _start_,_length_来   用于建议的分页目的。

修改

抱歉重读SQL Server

修复SQL Server:

$query作业替换为:

$query = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code'
     AND product_id BETWEEN ($Page_Start) AND ($Page_Start + $Per_Page)";

答案 1 :(得分:1)

你应该能够通过说出像

这样的内容来获得一个页面的结果
$strSQL = "
    SELECT * FROM products
    WHERE 1=1 {$cheack}
    ORDER BY id
    OFFSET (($Page - 1) * $Per_Page)
    FETCH NEXT $Per_Page ONLY
";

(确保设置了适当的PHP变量之后)。