0

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.

Community
  • 1
  • 1
Kelsey
  • 913
  • 3
  • 19
  • 41
  • 1
    So we should just GUESS if `$strSQL` is a valid SQL query string, or (more likely) an array of results you build from some other query? Exactly what thought process lead you to thinking you could stuff an array into a query and having something actually happen? – Marc B Mar 14 '14 at 19:51

1 Answers1

1

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.

  • I am trying to use the foreach loop to display the shopping basket and each of it's items. I am also trying to paginate the items by ten. For example, if there was 25 items there would be three pages with ten items each on the first two pages and five items on the last page. I did use your code however, and I will update my question to display my code and the result. – Kelsey Mar 14 '14 at 20:09
  • This is what gets displayed on the PHP page when I run it on my website: `× (Code :1) Qty : 1 Total : 0 Checkout`. I have five items currently in the shopping basket. – Kelsey Mar 14 '14 at 20:11
  • I don't have a clue. Are you running a homemade checkout system or an already established solution? It's hard to define your problem. When a user adds an item to the shopping basket what SESSION is it being stored to? –  Mar 14 '14 at 20:14
  • It is not displaying any of the products correctly, and it only displays one array. I prefer to use pagination because my boss has requested it in the blue print. Do you know any way to display ten arrays per page? – Kelsey Mar 14 '14 at 20:18
  • The code above should limit the echoing to 10 but won't be able to paginate as in show 11-20 when clicked. Unfortunately i'm not that skilled in PHP to offer you a solution to that. I could help you echoing the results correctly though. Next to the items, I presume you have add to basket or something along the lines of that when that is pressed what is stored? –  Mar 14 '14 at 20:24
  • Unfortunately I already have a PHP page that correctly displays all of the array items in the session. What I need is for those array items to be paginated by ten items a page. – Kelsey Mar 14 '14 at 20:28