Domanda

I have a shopping cart and I am trying to display the products in my user's shopping cart with SESSION arrays. Currently, all products added to the shopping cart get arrayed into the $_SESSION["products"] SESSION. Now, I am trying to query the $_SESSION["products"] session to display the products in a list with pagination. I am using pagination so only 10 products get displayed per page. Basically the $_SESSION["products"] is a cached array of products that the user added to their shopping cart via their browser's cache. 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". In my pagination code below, the pagination clearly works properly and retrieves the product_code and displays 10 per page. I am looking for something exactly like that except by retrieving from the SESSION.

In simplest forms, how do I retrieve the data from my 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.

P.S. 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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top