SESSION数组Foreach循环不适用于PHP页面

时间:2014-03-14 20:26:29

标签: php arrays session caching shopping-cart

我有一个PHP页面,用于检索$_SESSION['products']会话中的数组。该会话中的每个阵列都是用户添加到其购物车中的产品"。目前我的会话有11个阵列,这意味着我已经将11个产品添加到购物车中。我现在正尝试在view_cart.php页面上显示数组,并将它们分成10个。基本上我希望页面显示前十个数组然后创建一个新页面来显示最后一个数组。现在,我相信代码设置良好并且有可能对数组进行分页,但页面上只显示一个数组。

例如,当我在我的实时网站上运行该页面时,会显示以下内容:

×
(Code :1)

Qty : 1
Total : 0
Checkout

以下是view_cart.php页面的完整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('gbdca','Gdca','Rdca');  
$objDB = mssql_select_db('Gdca',$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'";
       $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('gdf','Gdfac','Rdcfga');  
$objConnectee = mssql_select_db('Gdab',$mysqli ); 
?>

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

1 个答案:

答案 0 :(得分:2)

原因是因为

foreach ($_SESSION['products'] as $cart_itm)
     if(++$i > 10) break;
   {

这将运行break循环10次然后运行一次代码块

应该是

foreach ($_SESSION['products'] as $cart_itm)
   {
     if(++$i > 10) break;

这将运行代码块10次

当没有大括号时,#34;运行一行&#34; php的功能。