简体   繁体   English

如何使用PHP的MSSQL查询SESSION数组

[英]How to MSSQL Query a SESSION Array Using PHP

I have a shopping cart and I am trying to display the products in my user's shopping cart with SESSION arrays. 我有一个购物车,并且尝试使用SESSION阵列在用户的购物车中显示产品。 Currently, all products added to the shopping cart get arrayed into the $_SESSION["products"] SESSION. 当前,添加到购物车中的所有产品都排列在$_SESSION["products"] SESSION中。 Now, I am trying to query the $_SESSION["products"] session to display the products in a list with pagination. 现在,我试图查询$_SESSION["products"]会话以在带有分页的列表中显示产品。 I am using pagination so only 10 products get displayed per page. 我正在使用分页,因此每页仅显示10个产品。 Basically the $_SESSION["products"] is a cached array of products that the user added to their shopping cart via their browser's cache. 基本上, $_SESSION["products"]是用户通过浏览器的缓存添加到购物车中的一系列缓存的产品。 I realize that you cannot literally SQL query the $_SESSION["products"] but I am looking for something similar to retrieve the results as a "query". 我意识到您不能从字面上SQL查询$_SESSION["products"]但是我正在寻找类似的东西来以“查询”的形式检索结果。 In my pagination code below, the pagination clearly works properly and retrieves the product_code and displays 10 per page. 在下面的分页代码中,分页显然可以正常工作,并检索product_code ,每页显示10个。 I am looking for something exactly like that except by retrieving from the SESSION. 我正在寻找与SESSION完全一样的东西,除了从SESSION中检索。

In simplest forms, how do I retrieve the data from my SESSION? 以最简单的形式,如何从SESSION中检索数据?

This is a visual interpretation of the sort of code I am looking for: 这是我正在寻找的那种代码的直观解释:

$strSQL = $_SESSION["products"]; 

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

This is my Pagination code: 这是我的分页代码:

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

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

$objConnect = mssql_connect('gdm','Gdr','Rod1!');  
$objDB = mssql_select_db('GBadfr',$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 = 2;   // 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>';
        echo "dsa ".$cart_items."dsa";
    }

?>
</body>
</html>

Thank you for any help. 感谢您的任何帮助。 All help is appreciated. 感谢所有帮助。

PS I have a similar question at Shopping Cart's `View Cart Items` Page is not Paginating Properly that you may take a look at if you are willing. 附注:我在购物车的“查看购物车项目”页面上没有类似的问题,即“页面未正确分页” ,如果愿意,可以看看。 Thank you again for any help. 再次感谢您的帮助。

Should work 应该管用

<?php
$i = 0;
  foreach ($_SESSION['products'] as $result)
     if(++$i > 10) break;
   {

     // Echo what you want from your array.

   }

?>

Just seen you updated your code (The above won't work). 刚刚看到您更新了代码(以上操作无效)。 If you want to iterate through the array. 如果要遍历数组。 Use the foreach loop to display the shopping basket and each of it's items. 使用foreach循环显示购物篮及其每个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM