0

can anyone explain me add to cart script.i have read it in some sites but dont know what it means. can anyone explain a simple one?

2
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by Kieran Y5
0

You need to know how to use sessions first, then work from there. http://www.w3schools.com/PHP/php_sessions.asp

If you already know that then you will need to:

  • Put the item into an array that *should* be used for the chart

Session Class (top of page, before ANY content);

class Session
{
  var chart = new Array(); // The chart
  var numInChart; // Number of items in the chart

  /* Class Constructor */
  function Session()
  {
    @session_start(); // Starts the session
    getChart();       // Gets the chart from the session
  }

  function getChart()
  {
    $x = 0;

    while(isset($_SESSION['chart_'.$x]))
    {
      $this->chart[$x] = $_SESSION['chart_'.$x];
      $x++;
    }
    $this->numInChart = $x;
  }

  function addToChart($itemId)
  {
    $t = $this->numInChart + 1;
    $_SESSION['chart_'.$t] = $itemId;

    getChart(); // Re-check the chart
  }
};
$session = new Session;

Explanation:
To make it easier, I have used a class for the chart.

To add an item to the chart, call $session->addToChart($itemID); this adds another variable into the session. You should have an ID for all items so that you use as little data as possible and stop many possible errors.

When the class starts, its starts with $sesson->Session(). This then calls the getChart(); function which gets all of the chart and how many items are in it.

You can get an item from the chart by using $session->chart[ $num ]. You should use a loop to get all of the items.

You can get the total number of items in the chart by using $session->numInChart.

If you don't understand what I am saying then please tell me, I will the re-write this post.

Kieran :)

Edited by Kieran Y5: n/a

0

i meant cart . the shopping cart code. also do u know how2 make chat room in php

0

i meant cart . the shopping cart code. also do u know how2 make chat room in php

kk, its the same thing, just change all of the chart things to cart - I was writing it out at 12am (ish) so I was tried as hell :(

A chat room requires JavaScript and AJAX OR you could do it in flash but i am not sure.

Edited by Kieran Y5: n/a

0

can u please tell where 2 write code. i pasted the code it gave me errors. what are the pre requisites for such a project? thanks a lot....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.