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?

Recommended Answers

All 5 Replies

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 :)

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

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.

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

What are the errors?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.