I will do my best; in order to operate a shopping cart system you need a way to tie the user to their cart items (as opposed to someone else's). Usually this is done directly or indirectly with cookies (I will assume you know what a cookie is, though you said you are a newbie I am guessing PHP newbie not toal web newbie?).
So, the general idea is that when a user of your site first tries to access the cart they are assigned a cookie with a unique id and from then on (at least until the cookie/cart is cleared) this id will stay with them and identify their cart items.
In your code any access to the cart presumable fuirst calls the GetCartId function. This function checks to see if they already have the cookie (a cookie called 'cartId') and either returns it if they have or assigns them one if they haven't. In order to ensure that the cartid they are given is unique, the function uses PHPs built in sessions (again I will assume you understand the concept of sessions). By starting a session for this user, PHP will automatically assign thenm a unique session id, which is then also used as the cookie id:-
This line sets a cookie called cartID, with the value of the PHP session id - session_id() is a built-in function -and such that it will last for 30 days (the last parameter for the setcookie call is a time expressed in seconds since 1970and this line takes the current time - time() - and adds 3600 (=1 hr) * 24 (=1 day) * 30.
While it would have been possible for the function to use the session id directly as the key into the table, the PHP session only lasts for one visit, so by setting a cookie with the value, the cart is made semi-permanent, provided the user comes back on the same machine within 30 days.
So then in the cart table we have
cartId int auto_increment not null,
- primary key allows you to access each line individually
- this is waht identifies which user it belongs to
- this is an identifier for the type of item, which allows you to have different items in the cart at the same time, e.g. 3 fish, 5 loaves and 1000 people.
- how many of this item the user has in the basket
so one record in the table represents the number of items of a particular type that a particular user currently has in the cart. There can be 0,1 or several lines for a given user, if they have added different types of item to the basket, equally there can be multiple lines for each item type but for different users. If a user has not selected any of a particular item, then there will be no line in the table at all for that user and item (NOT a line with 0 as the qty).
- User 1 has 2 items of type 1 (line 1) and 5 items of type 2 (line2) currently in the basket.
- User 2 has 4 items of type 3 (line 3) in the basket
- User 4 has 1 item of type 5 (line 4) in the basket
- User 3 has nothing in the basket (no lines with cookieid = 3)
Lastly in the add call
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
this finds out whether this user already has any of the item in question in the basket. If the query returns 1 (or more but should probably only be one) then I imagine the code performs an update query to add the new items to the ones already in the basket (i.e. it changes the qty in the existing row rather than creating another row for the same item). If the query returns 0 it means this user has not currently got any of these items in the basket so I imagine there will be an insert query to create a new line of that item type for that user.