4 Replies - 14440 Views - Last Post: 05 November 2013 - 02:03 PM Rate Topic: -----

#1 jesders88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 25-October 12

PHP Shopping Cart w/Sessions(No SQL)

Posted 29 October 2013 - 04:16 PM

I need to create a shopping cart and I cant figure out how to implement it without SQL.

It shouldnt be that difficult but what I have does not work

Can someone help me figure out where I am going wrong?

PS- I only need to display 2 things in the cart the quantity and itemID

<?php?>
<HTML>

<HEAD>
</HEAD>

<BODY>

      <p>A09_DictCart/default.htm</p>

<center>

      <h2>Catalog</h2>

      <TABLE border=1 cellPadding=3 cellSpacing=1>
        <TBODY>
        <TR>
          <TD>Image</TD>
          <TD>ItemID</TD>
          <TD>Description</TD>
          <TD>Price</TD>
          <TD>Add Item To Cart</TD></TR>
        <TR>
          <TD><IMG src="./images/p007228iiob.jpg" width="100" height="100"></TD>
          <TD>007228</TD>
          <TD>Dri-Plus Dri-Fowl Wading Jacket</TD>
          <TD>$99.95</TD>
          <TD><a href="updateCart.php?action=add&amp;itemID=007228&amp;quantity=1">Add 
            this to my cart!</a></TD></TR>
        <TR>
          <TD><IMG src="./images/p007420iiob.jpg" width="100" height="100"></TD>
          <TD>007420</TD>
          <TD>Scent-Lok Blaze Over Jacket</TD>
          <TD>$69.95</TD>
          <TD><a href="updateCart.php?action=add&amp;itemID=007420&amp;quantity=1">Add 
            this to my cart!</a></TD></TR>
        <tr>
          <TD><IMG src="./images/p007741ii01.jpg" width="100" height="100"></TD>
          <TD>07741</TD>
          <TD>Switchback Boot</TD>
          <TD>$119.95</TD>
          <TD><a href="updateCart.php?action=add&amp;itemID=07741&amp;quantity=1">Add 
            this to my cart!</a></TD>
        </tr>
        <TR>
          <TD><IMG src="./images/p010315iiob.jpg" width="100" height="100"></TD>
          <TD>010315</TD>
          <TD>Master Guide Wading Boot</TD>
          <TD>$64.95</TD>
          <TD><a href="updateCart.php?action=add&amp;itemID=010315&amp;quantity=1">Add 
            this to my cart!</a>
          </TD>
        </TR>
      </TBODY>
    </TABLE>
  
      <p><a href="viewCart.php">View Cart</a></p>
      
      <p>&nbsp;</p>
      
  </center>
  </BODY>
  
  </HTML>




<?php
session_start();
if(isset($_GET['itemID']))
{
    $itemID = $_GET['itemID'];
}
else
{
    $itemID = 1;
}

if(isset($_GET['action']))
{
    $action = $_GET['action'];
}
else
{
    $action = "empty";
}

switch ($action)
{
   case "add":
       if(isset($_SESSION['cart'][$itemID])){
           $_SESSION['cart'][$itemID]++;
       }
       else{
           $_SESSION['cart'][$itemID]=1;
       }
       break;
   case "remove":
       if(isset($_SESSION['cart'][$itemID])){
           $_SESSION['cart'][$itemID]--;
           if(isset($_SESSION['cart'][$itemID])==0){
              unset($_SESSION['cart'][$itemID]);
           }
           }
       
       break;
   case "empty":
        unset($_SESSION['cart']);
       break;
       
    
}


?>

<html>
    <head></head>
    <body>
        <a href="viewCart.php">View Shopping Cart</a>
    </body>
</html>



<?php
session_start();
echo "<h2>" .'Shopping Cart Contents'."</h2>";

    if(isset($_SESSION['cart'])){
    
    echo "<table><tr><th>Item ID</th><th>Quantity</th></tr>";
    foreach ($_SESSION['cart'] as $cart){
        echo "<td>". $itemID ."</td><td>". $quantity."</td></tr>";
    
    
    
    }
    echo "</table";
    }
    else{
        echo "<h2>No items in cart.</h2>";
    
    } 
        
        
        



?>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>

<body>



<center>




<p align="center"><a href="index.php">Back to Catalog</a></p>

</center>

</body>





Is This A Good Question/Topic? 0
  • +

Replies To: PHP Shopping Cart w/Sessions(No SQL)

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,404
  • Joined: 08-June 10

Re: PHP Shopping Cart w/Sessions(No SQL)

Posted 29 October 2013 - 11:57 PM

Quote

I need to create a shopping cart and I cant figure out how to implement it without SQL.

donít you want to save those shopping cart actions? I would definitely save it in a DB. ok, if you use a NoSQL DB, you donít have to worry about SQL either ...
Was This Post Helpful? 0
  • +
  • -

#3 jesders88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 25-October 12

Re: PHP Shopping Cart w/Sessions(No SQL)

Posted 30 October 2013 - 01:35 PM

yep no database at least at this point. I am just trying to figure out the concept right now. I want to learn how to without a database first of all
Was This Post Helpful? 0
  • +
  • -

#4 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: PHP Shopping Cart w/Sessions(No SQL)

Posted 30 October 2013 - 02:42 PM

I honestly think you should start with MySQL. Learn how to connect them now rather than later. I don't really understand the point of not having a DB to back it all into and then creating dynamic links as well so that you don't have to create a new page for each item.
Personal Opinion though.
Was This Post Helpful? 0
  • +
  • -

#5 Zimdale  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 30-July 13

Re: PHP Shopping Cart w/Sessions(No SQL)

Posted 05 November 2013 - 02:03 PM

I don't see why everyone is so anti-this. It's a neat concept, not practical but who the hell cares.


The problem I see is your show makes little to no sense..

 foreach ($_SESSION['cart'] as $cart){
        echo "<td>". $itemID ."</td><td>". $quantity."</td></tr>";



Where is $itemID and $quantity coming from? You would probably have to do a foreach with a key to really get anything.

Something like this
foreach($_SESSION['cart'] as $itemID => $quantity)
{
    echo "<td>". $itemID ."</td><td>". $quantity."</td></tr>";
}



Of course that is untested but I think it's closer than what you have.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1