doufei3561 2014-03-14 20:26
浏览 78
已采纳

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

I have a PHP page that retrieves the arrays in my $_SESSION['products'] session. Each array in that session is a product added by the user to their "shopping cart". Currently my session has eleven arrays meaning I have added eleven products to the cart. I am now trying to display the arrays on my view_cart.php page, and paginate them by ten. Basically I would like the page to show the first ten arrays then create a new page to display the last one. Right now, I believe the code is set up well and has potential to paginate the arrays, but only one array is displayed on the page.

For example, when I run the page on my live website this is what gets displayed:

×
(Code :1)

Qty : 1
Total : 0
Checkout

Here is my full PHP code for the view_cart.php page:

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

Here is my full config.php page's code:

<?php
$mysqli = mssql_connect('gdf','Gdfac','Rdcfga');  
$objConnectee = mssql_select_db('Gdab',$mysqli ); 
?>

Thank you for any help. All help is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dp926460 2014-03-14 20:33
    关注

    The reason is because

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

    this will run the break loop 10 times then run a code block once

    should be

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

    this is will run the code block 10 times

    it is the "run one line when there is no curly brace" feature of php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图