Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Advanced PHP
  • Discussion

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Web Application Development

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:17
    • Lesson Overview
  • instanceof Operator 1:34
    • instanceof Operator
  • Objects with Object Properties 2:53
    • Objects with Object Properties
  • Version 11.0 Overview 6:41
    • Version 11.0 Changes: Cart and CartItem Classes
    • Version 11.0 Coding Example
  • Version 11.1 Overview 31:03
    • Version 11.1 Changes & Examples

Transcription: Web Application Development

Hello again, and welcome back to's Advanced PHP with MySQL course.0002

In today's lesson, we are going to be continuing development of our web application.0007

We are going to be incorporating what we learned in our last lesson about cookies to improve the functionality of our site's shopping cart.0011

We are going to introduce a couple of small topics before we get into the actual changes we made to the web application.0019

We are going to be talking about a new operator called the instanceOf operator.0026

And then, we are also going to be talking about how to access properties of objects that are properties of other objects.0031

And we will see a little more about what that looks like on the slide coming up.0038

Then, we are going to go through two different versions of the web application.0042

In version 11.0, what we are going to be doing is creating a shopping cart object.0045

It is going to be called a Cart--that is the name of the class.0051

And we are going to use that to represent our shopping cart.0056

Right now, we have been using an associative array that we have been passing around,0059

called items, that has keys that are the item ID of the item in the shopping cart, and then its value is the quantity.0064

Well, we are going to change that and put it into an object.0074

In 11.1, what we are going to do is implement a shopping cart that uses cookies.0078

So, in 11.0, we are going to convert things over to an object model for our shopping cart; in 11.1, we are going to 0084

use that object model with cookies to get a more functional shopping cart.0090

PHP provides what is called the instanceOf operator, and what it allows you to do is determine whether a variable is an instance of a specific class type.0096

For example, if we have a variable called object, or obj, and we want to test 0107

to see if it is, for example, an instance of the Item class that we use in our web application;0113

The way you would do that is: instanceOf is a binary operator; you put the variable that you are testing the type of on the left-hand side.0119

And then, you put the exact name of the class on the right-hand side of the operator, and this becomes a boolean expression.0127

So, what it does is says, "If object is an instance of the Item class, return true; if it's not, return false."0134

isItem would be true if object is an Item, and it would be false if it isn't.0142

This is useful for when you are accepting parameters from functions and so forth,0146

to test and make sure that you are getting appropriate objects that you might be working on.0151

For example, if you need an Item object to work on, you need to be sure that you are passed an Item object, as opposed to an Integer, for example.0156

And again, you need to make sure that you use the exact name of the class to the right of the operator.0164

And it is just the name of the class; there is no dollar sign or anything like that.0169

Also, we know that an object has properties.0175

And the data types of an object's properties can be any data type, which includes other objects.0178

So, for example, we could have a Cart class, and we could define a variable item that is intended to be a variable that represents an Item object.0185

What we have is an object within an object; so how do we access the properties of the Item object that is a part of the Cart object?0206

Well, we use the arrow operator and a cascaded syntax.0214

Let's say, for example, we have a Cart class, like the one above, that has a property named Item, as it does up here,0219

which is an Item object; that is the data type of that property.0226

And let's assume that the Item object is the Item that we use in our web application.0231

So, the Item object, we know, has a price property. How do we access the price property?0235

Let's say we have an instance of the Cart class; we have created an instance of Cart called cart.0242

Let's not worry about the details behind it.0248

But it has been set up; an item has been added to it, so the item has been set to a new Item object, for example--maybe in the constructor of the cart.0249

And we want to access the price of this particular Item object.0260

Well, the way you do that is using multiple arrow operators.0264

For example, we get the instance of our Cart object, and then we use the arrow operator to say, "Give me the item property."0268

And what that is going to do is return the instance of the Item object that this current instance of the Cart item property is set to.0277

And then, now that this is an object, we can again call the arrow operator on it to say, "Now I want the price property of this object."0288

In this case, this is an Item object that was taken from the Cart object; and so, this is saying how to get the price from it.0297

And you can do this indefinitely; you can have objects within objects within objects.0304

And the reason I am introducing in now, as well as the instanceOf operator, is because these are some things0309

that are getting introduced into the versions of the web application we are going over in today's lesson.0314

So, let's say Price was even an object, and maybe it had a property that was called currency, that lets you know the currency it was in.0320

And we could access it like so, and that goes on down the line.0330

This also works with class properties that are arrays of objects.0336

For example, let's say, in our cart, instead of just having an Item object, we had an associative array of Item objects,0341

where the key to that array is an item ID, and then the values are Item objects.0348

What you can do is say, "For this Cart instance, give me the Item object that is in the items associative array, associated with the key item-1001."0357

This is going to return an Item object, and then because it is an object, we can use the arrow operator on it to get the price of it.0369

And so, this is going to say, "Give me the price of this Item object."0378

This is saying, "Get me the price of item-1001 that is in this particular Cart instance."0382

This would be assuming that it has an item 1001, and so forth.0390

But that just demonstrates how you can also access object properties that are arrays of objects.0393

Now, we are going to get into the nuts and bolts of the changes we are making in this web application.0403

We are introducing two new objects into our web application: Cart, and one called CartItem.0408

And what they are is classes that are created to model a shopping cart.0415

That is what we do with object-oriented programming; we model real-world objects.0419

We have a Cart class, and as in the last slide's example, it is going to contain a property called cartItems.0425

And this cartItems is going to be an associative array of CartItem objects.0445

So, we have another object that we are creating called CartItem.0452

And what this does is represents an item that is in a cart.0457

We set it up so that it has two properties: one is called item, and one is called quantity.0464

For example, in our shopping cart, when we add an item to a shopping cart, we add the item, and then we also have a quantity associated with it.0471

We are going to encapsulate that into one object.0478

It is a CartItem object; it is going to contain an Item object, which is the store item that this CartItem represents.0481

And then, it is also going to have an Integer property, quantity, that is going to represent what quantity of that item is currently in the shopping cart.0488

Now, in our shopping cart, this is an associative array; it is going to have keys that are item ID's for items in the shopping cart.0496

And what they are going to point to is CartItem objects, which, as mentioned over here, contain an Item and a quantity.0504

We have this Cart object that contains an associative array of a number of CartItems.0519

And a CartItem is an object that encapsulates a store item, along with its associated quantity.0525

What we can do, then, is: we are also going to be creating a method for our CartItem class called calcItemTotal.0532

What that is going to allow us to do is: because this CartItem class knows what Item it represents, and it knows the quantity,0544

it has access to the price of the Item; it also has access to the quantity of the Item.0553

So, we can calculate the cost due to this particular Item in the Cart.0557

And that would be the price of the Item, times whatever quantity it is in.0562

So, what we can do in our Cart class is: we are going to create a method called calcTotal.0566

And what that is going to do is loop over, when we need to calculate the total of a shopping cart, all of the CartItem objects within its cartItems array.0573

And it is going to call the calcItemTotal on each of those items.0584

Instead of having to manually go and look up the item and get its price, and then multiply it times the quantity,0588

it is going to have the CartItem object do it for us by this calcItemTotal method.0595

This is going to loop over all of the different CartItem objects within the cart.0600

It is going to add up their totals, multiply it times the sales tax rate, and then generate the total for the entire cart.0604

So, each CartItem generates the total due to that item, and then the Cart takes all of those CartItem objects together,0614

adds them up, and then calculates sales tax.0622

So, we are going to be replacing our former method (that was just a user-defined function called calcCartTotal).0624

And that functionality is going to be moved into this Cart class, as well as this CartItem class.0633

Let's go and take a look at what these classes look like.0640

Look at version 11.0...well, first let's look at the CartItem.0645

Our CartItem class, if we look at the source code, has two properties: item and quantity.0655

quantity is an integer representing the amount of this particular item in the cart.0663

item is an Item object that represents the actual store item that this CartItem represents.0667

This object is an Item and its associated quantity.0674

We have a constructor that takes an Item object and an integer for the quantity, and just sets those private properties.0680

And then, here is our calcItemTotal method that we had talked about.0687

What that does is returns the total due to this particular item in a particular shopping cart.0692

For example, it multiplies the item price times its quantity.0698

Well, it gets the quantity from this CartItems variable quantity; so it says $this and then gets the quantity.0701

Now, to get the price, the price of an item is contained within the Item object;0710

so what we need to do is use the $this variable to say, "Give me the Item property of this particular instance of a CartItem."0715

And what that is going to do is give you the Item object of this item property that was set in our constructor.0723

And then, using this cascaded arrow syntax that we talked about before, it is going to say, 0730

"From that Item object that this CartItem represents, give me the price of that."0735

It is going to multiply the two together, and it is going to return that.0740

If we go and look at our Cart class, it has a number of different methods in it.0744

It has a constructor that, as we will see, allows you to pass it a bunch of items--not CartItems, but Item objects.0747

And it will add them to its cartItems property.0756

So, let's look at the source code; we can see that it has one property called cartItems, as we had discussed.0761

And it is an associative array of CartItem objects, and the keys to that array are the item ID's.0768

Now, this Cart class has a number of different methods that have been created.0774

It has an addItem method, which allows you to add an item to a particular cart.0779

It allows you to get access to that cartItems property, which gives you an associative array0785

of all of the CartItem objects contained in this particular shopping cart.0792

It has a method called inCart that allows you to test whether a particular item is currently in the cart, based on item ID.0797

It allows you to test whether the cart is currently empty or not (meaning that there are no items in the cart).0803

And then, it also allows you to remove an item from the cart by specifying the item ID of the item you want to remove from the cart.0809

And then, here, our calcTotal method, if we look at the code for it--we can see that what it does is:0816

it gets the associative array property in this class cartItems, which is an array of all of those CartItem objects.0824

It loops over each of those items, and then it cumulatively adds to the subtotal the total due to each item.0831

So, for each CartItem object it loops over, it calls the calcItemTotal method and adds it to the subtotal.0838

Once it is done looping over all of the items, it adds the sales tax to it, rounds it, and then returns that method.0843

And that is going to be used by, for example, our checkout script, to be able to calculate the total of a cart.0849

One thing to note is that, if we look at the addItem function, what it takes is not a CartItem object, but an Item object.0861

For example, when we go to add an item on our script--let's say we are going to be adding a button to our item.php page0870

that is going to allow you to add an item to the cart--when you click on that item, it is going to go to viewCart.php.0879

And it is going to load an Item object from the database for the item that you selected, based on the item ID.0884

And then, it is going to be calling this addItem method on a shopping Cart object, and it is going to pass it this Item object.0893

Well, what is going to happen is: this method is going to test if this item is in the cart.0901

If it is not in the cart, it is going to create a new CartItem object that wraps that particular Item object.0907

And it is going to set its quantity equal to 1.0912

If it is already in the cart, what it does is goes and gets the current quantity of that item in the cart.0915

And in this case, you can see here: we are using the syntax again--the cascading arrow syntax.0922

And here, what we are doing is saying, "From this particular Cart object, give me the cartItems property (which is an array)."0927

"Give me the CartItem object that is associated with the item ID that is for this Item we are trying to add."0934

"And then, get me the quantity of it."0944

And then, what we are going to do is re-add the item to the cartItems array, but increasing the quantity by 1.0945

And so, that is how we add the item to the cart.0953

What we have done in our web application is (let's reduce the size of this a little bit): in version 10.1,0961

which is our last version of the web application, when we go to viewCart, what it does is simply outputs all of the items in a particular store.0972

And that is not typically how a shopping cart works.0983

Typically, you have an empty shopping cart, and as you add items to it, it builds it up.0985

Because we haven't had the functionality to be able to do that, our last version of it just lists all of the different items.0989

And you enter the quantity you want, and then you go to check out.0995

And there, it is going to say we need to add something.0999

In our new version, what we have done is: on each item page, we have added a button here called Add to Cart.1003

If we look at our old version, there is no button.1011

And what that does is: now, when we click on Add to Cart, it is going to go to the (I'm not sure why the lines aren't showing up; 1014

there they go) viewCart page, and it is only going to add that item to our shopping cart.1025

So, now our viewCart page only has the items that are in the particular shopping cart.1030

What we have done is modified our viewCart script.1037

So, now viewCart, instead of just being, in a way, a static script that just goes through and lists (in our old version)1039

all of the different items in our particular cart, what we are going to do is pass to it an action.1048

And we can set the action to be addItem, and then we specify the item ID of the item we want to add to the cart.1055

And then, we are going to look at viewCart in a second, and we are going to see how we process that into adding an item to a cart.1061

We also can remove an item from the cart, and that is a feature that we have added to the page--we have added this Remove link.1068

And what that does is: now our viewCart page can also process a removeItem action.1075

And you specify the item ID of the item that you want to remove.1083

For example, let's go look at viewCart.php.1089

Previously, in 10.1, all we did was got all of the items in the store, using this getItems user-define function.1094

And then, we just output a row in the table for each item in the store.1104

Now, in our viewCart class, what we have done is: we allow it to take an action now, and an item ID.1109

And the item ID is going to be the item that we want to add or remove from the cart.1119

And then, in our data processing section (and this is just temporary, for this version of the app;1123

and we are going to update it later in this lesson to use cookies), we are going to create a Cart object.1128

And then, what we are going to do is: if the action of the script is to add an item, we are going to load the Item object1136

from the database, based on the item ID, and we are going to add it to the cart.1144

If it is to remove the item, we are going to use the removeItem function that we created in our Cart object to remove the item from the cart.1148

Then, we have changed the output of our Cart page.1159

Before, we would just output rows for each item that is in the store.1165

Now, what we have done is: because we have this isEmpty method, we first of all test if the cart is empty.1171

If it is empty, we just output a message that says, "Your cart is empty," and the script is done.1177

If not, what we do is output that table again.1182

But this time, we loop over all of the items that are currently in that Cart object.1186

So now, instead of just looping over all of the items in the store, we are only looping over all of the items that are in the cart.1191

When we add an item to the cart, the cart is going to be populated with an item.1196

When we call getCartItems on that object, we are going to get the associative array that contains all of those CartItem objects.1200

And then, what we are going to do is extract information from that CartItem object to output each item that we have in our shopping cart.1206

It is going to output the item's name, a link to the item's page, its price, and its quantity.1215

And then, also, we have added this new link down here, which is a link to viewCart.php1221

that has a query string that says Remove Item, and it provides the item ID of the item we want to remove.1227

And so, what that is going to do is do that for each item in our particular cart.1235

The thing that you will notice is that, in our new version, if we added Baseball Bat to the cart;1239

well, if we go and look at our viewCart script at the very beginning, we are going to see that it is going to create a new Cart object.1249

It is going to add the item to the cart, and then, when it goes down to the processing section,1256

it is going to be able to loop over that one CartItem and output a link for it.1262

So, as you can see, it is output here.1267

Now, however, if we go ahead and click viewCart again, and this time just go to viewCart 1270

without any action that says Add Item, it is going to say the cart is empty.1279

And what has happened is that we lost the state of our shopping cart.1282

And that is what we are going to learn that cookies are going to provide for us.1286

They are going to allow us, in between viewing different pages of our website, to maintain the state of that shopping cart--1288

to say, "Hey, this item is in the cart, and it has a quantity of 1."1293

The reason is that, when we go to viewCart without any Add Item action,1296

when we go up here, it is going to create a new Cart object; but it is going to have no item to add to it.1304

So, our cart is going to be empty, so when it goes down here, it is going to say, "Cart is empty," and it is going to output, "Your shopping cart is empty."1307

We have no way, right now, of showing that when we go from viewCart to viewing the cart over and over again--1314

we have no way for those different HTTP requests to maintain the state of the cart.1322

And we are going to do that, using cookies.1328

The other thing that you will notice, as a result of that, is: let's say we add a basketball to our cart, 1330

so now we have a basketball in our shopping cart; now, let's say that we want to go and add a DVD player.1336

Well, when we add a DVD player to our cart, we can see that the basketball is no longer in there.1342

Again, that is because we have no way of maintaining state between the different requests.1348

Each time we go to this viewCart page, it doesn't have any prior knowledge about the basketball that we have added.1353

All it knows about is the item that was currently added to the cart (in this case, it added item 1005).1360

And we are going to see how to fix that in our new application, with the use of cookies.1367

And so, as mentioned, you can see that we have added the Add to Cart buttons to our Item page.1374

And we have added Remove links to remove items from the cart.1380

One other thing is that we have also updated our checkout.php.1385

Previously, in checkout.php, what we would do is pass an items array via POST form.1392

For example, when we would submit this form on our old application, it would post (let's show an example) data to checkout.php.1399

And if we look at the POST data, we can see that it has this items array1416

that has a key which is the item ID, and then the quantity associated with each item.1421

For example, this shopping cart contains items 1001, 1002, and 1003.1427

It contains quantity 1; and so, that is how we are able to calculate our shopping cart total.1431

What we are doing is passing the information along in this array.1437

Well, in our new version, we don't have that form anymore to submit.1444

All we have is this Add to Cart button, and what that does is passes information to the viewCart page, using the action and itemID.1448

And so, what that does is: our viewCart page now only has one item.1461

So, when we go to check out, as you are going to see (and I will explain in a second why that doesn't work),1464

what is going to happen is: we are not submitting all of that information anymore.1471

We are not submitting this items associative array that contains all of the items in the store, along with their quantities.1474

Instead, what we are going to do is change checkout, so that it is going to be able to use a Cart object.1480

What is eventually going to happen is: we are going to have a cookie that is going to contain all of our shopping cart information.1486

So, when we click checkout on our viewCart form, it is going to go to the checkout page.1492

checkout.php is going to be able to read the cookie, build up a Cart object, and then be able to process the order--1498

for example, to send out an email that says, "OK, this is the order that was emailed."1504

We don't have that functionality with the cookie set up right now, so what we are temporarily doing is:1509

we have no way of passing the information from our viewCart form to our checkout.php,1513

because in our new version, viewCart.php is no longer a form.1518

It is just a link to checkout that says "action=checkout."1523

What we do is: just so we have something to show for this checkout form, we create a fake cart.1528

We are just creating a Cart object, and we are adding two items to it.1534

And then, what we are doing is: we just check to make sure that the cart is not empty, which it is not going to be, because we have added stuff to it.1539

We calculate the cart total using the calcTotal method.1544

And then, we process things as before; if the action is to complete the order, we validate the customer data,1550

and then we go ahead and email the order to the site administrator.1558

One thing to note, among many things to note, is that previously we validated the cart by using this isValidCart function that was in our utilLIB.1564

We are not going to use that anymore, because now we have this isEmpty function that is built into our Cart object1575

that will automatically tell us whether the cart is empty or not.1581

We are not going to be using calcCartTotal, because that functionality is now built into our Cart object.1583

And then, in addition, as we saw in our emailOrder function in our last version,1591

we provided it this cartItems associative array, which is that array that has keys of the items' ID's and the values of the quantity of each item.1598

We pass it the order total that was calculated by the calcCartTotal function up here.1607

We pass it the array of customer information.1617

And then, we also have to pass it information about all of the items in the database,1619

because emailOrder needs to extract the price information from the items, for example.1623

It needs the item information, so we have to pass all of that.1628

Well, in our new call to emailOrder, we only pass it two pieces of information.1633

We still pass it the customer data that was submitted on the customer form.1638

But we also now just provide one other variable called Cart, which is this Cart object that we have created up here at the beginning of the form.1642

Now, because the Cart object contains CartItem objects that have their quantity, and then it also has 1651

the Item object embedded within it, that emailOrder function has all the information it needs, within that Cart object,1656

to be able to extract the price of each item, and to generate the order for the particular email.1663

If we look at our emailOrder function, we can see that now it takes a Cart object that contains all of the contents of the shopping cart.1670

And then, it still takes a Customer object that contains all of the customer data.1689

And we can see that what it does here is: now, it loops over each of the items that are contained in the cart.1694

It extracts the price information, and then the quantity information, and then outputs a line in our email--1700

whereas, in our previous version of this, it was a lot more involved in our emailOrder function.1707

What we had to do was loop over each item ID in our associative array and get the quantity for it.1716

Then, what we would have to do, if the quantity was greater than 0, would be to look up the Item 1724

from the parameter allItems that was passed, which is a listing of all the items in the store,1737

extract the price information that way, and then generate this line that shows the item ID and its price.1743

Now, we extract all of that information, rather from this allItems array (that contains information about all the items in the cart),1750

simply by accessing the information that is in the cart method.1757

And so, we have used the emailOrder function.1765

The other thing is that, in our checkout.php script, previously, when we...1767

checkout.php has two sections to it: it previously received information from viewCart.php...1775

And that information would contain all of the item ID's and their quantities for items in the shopping cart.1781

And then, what it would do is allow the user to enter shipping information.1788

And then, it would proceed to a Complete Order section.1793

So, it has two actions that are processed.1797

Well, in order to be able to access all of that cart information that was passed from viewCart.php to checkout.php the first time,1800

what we had to do at the bottom of our form for the customer information was:1809

we had to loop over all of the items in the cart, and create hidden input fields 1816

that would create that items array that specifies the item ID and the item quantity.1821

Well, we don't need to do that anymore, because what we are going to be doing in the next version is using cookies to contain all of that information.1828

So, we have eliminated that from our checkout.php.1834

We have also eliminated passing along the order total between the different actions of our checkout.php form,1839

because now, each time checkout.php is loaded, it has access to the Cart object, which can calculate the total each time.1845

So, we don't need to pass that along.1852

So, when we look at checkout.php in our form, we don't have any of that information here at the bottom.1854

Now, we are going to get to the point where we use cookies.1864

What we are doing is: we are going to use cookies to allow us to maintain state between adding different items to the shopping cart,1867

and also to be able to pass that information from viewCart.php to checkout.1874

We have created a user-defined function in our util library called buildCartFromCookies.1878

And what that does is: it is going to, when you go to the checkout form, for example (checkout.php),1885

look at the cookie that the user sends; it is going to see all of the information in that cookie.1892

And then, it is going to build up a Cart object from that cookie.1897

The cookies that get set when you add an item to the cart are simply going to be name/value pairs1900

where the name is the item ID and the value is the quantity.1911

For example, these would be two cookies: one cookie called 1001 that has a quantity equal to 1; and one cookie called 1002 that has a quantity 2.1916

This buildCartFromCookies method would be able to read both of these cookies, and then build a Cart object up1924

that says, "OK, this cart is going to contain the item 1001 (with the quantity 1) and the item 1002 (with the quantity 2)."1929

So then, when you go to checkout.php, it can pass that information along.1938

And then, when you enter your shipping information on checkout.php, instead of having to forward all of that information1942

hidden at the bottom of the form, that cookie is going to get passed along when you go to the completeOrder action of checkout.php.1948

Because it does that implicitly, you don't have to pass that information along.1957

checkout.php will have access to that cookie, and can then get access to all of the cart information.1962

Let's go take a look at what the last version of the store looks like.1967

So now, let's add an item to a cart; we have added a DVD player to a cart.1972

And let's say we want to add another item to a cart--we want to add a basketball.1979

Now, when we add the item to the cart, it actually maintains state.1983

The reason is that, if we look at our new viewCart.php page, what it does is checks to see if there is any cookie data.1988

If there is, it builds a cart up from that cookie; if there is not, it checks to see if the action of the form was addItem.2002

If the action of the form is addItem, and the cart is empty, then what it does is sets a cookie for that new item.2008

It sets a cookie that says, "OK, item 1001 has a quantity 1."2016

So then, now, the cookie contains information that there is item 1001 of quantity 1 within the shopping cart.2021

When we go to a different page and add another item to the store, this cookie that we have previously set here2028

is going to get sent back to viewCart.php.2034

So, when it builds this cart up from cookies again, it is going to know about that previously added item.2037

And so, what it can do is: then, it will maintain that state.2044

And so, it will contain information about the previously added cart.2049

The other thing it can do is: if the item is already in the cart (for example, Basketball), and we want to add another one to the cart,2051

we click Add to Cart again, and we can see, the quantity is updated to 2.2058

The way that works is that, if the item is already in the cart (and viewCart.php will know that, 2061

because it has built up the Cart object from all the cookies that were passed to it), it can check if the item is in the cart.2070

If it is, it gets its current quantity, and then it resets the cookie for that item ID, incrementing the quantity by 1.2076

The same thing works for removeItem, except that it simply sets the time for a particular ID's cookie to a time in the past.2083

That removes that item cookie, and so, the next time you go to viewCart.php, that cookie is not going to get passed along, because it no longer exists.2094

One thing to note is that, on each of these pages, after we set the cookie,2104

that cookie does not become immediately available to the form,2109

because what we have done is built our item up from the cookie that was passed to the form originally,2113

which--if we had no items in the cart--there is going to be no cookie data.2120

When we go ahead and set the cookie, it doesn't know about that cookie until you refresh the page,2126

so we have a header function that refreshes the viewCart.php, which is going to allow it to show the updated shopping cart.2130

And that is why, when we go here, we can see that we have had an updated quantity.2139

Here, we can click on removeItem; what that is going to do...2144

And if you look down here at our Firebug console, we can see that, when we loaded this form,2147

it set a cookie for our Basketball, item 1006; and you can see that it set its quantity equal to 2.2160

You can also see that previously, when we went to this page, the web browser passed along information 2169

about the current shopping cart in the form of a cookie, where it said, "item-1005 cookie is equal to 1; item-1006 cookie is equal to 1."2177

That is how it knew that there was already one in there.2186

What is going to happen is: when you go to this page, you have added a second basketball, for example.2188

It is going to set the 1006 cookie equal to 2.2193

When we refresh the page, you can see here: Firebug lets you see the refresh happening.2196

We can see that, when the page is refreshed, the new cookie has been sent, so it has that item 1005 equals 1 and item 1006 equals 2.2204

And so, that way, when it refreshes, it is able to show quantity 2 for the basketball and quantity 1 for the DVD player.2214

Now, when we remove this, what we can see in our original request is that we set a cookie, 1005,2222

and we have set it to expire in the past, so the cookie is going to be deleted.2234

So, when the page refreshes, we can see that the only cookie it has is for item 1006, which is our basketball.2238

And that is why it doesn't show up again.2245

Now, what we have also done is: on this page, we have a Checkout button that simply goes to checkout.php with the action checkout.2249

And what that does is: because this is in the same domain, it passes that cookie that contains all the information about the cart along.2258

So, checkout.php now is able to build that cart up from the cookie that it has.2264

It is able to test if the cart is not empty.2272

If it is not empty, it can calculate its total.2275

If it is completing the order, then it goes ahead and processes the customer information, and it goes ahead and emails out the order.2278

And then also, what it does is: after it is done completing the order, it goes ahead 2289

and deletes all the different cookies by setting their values to a time in the past.2293

So, after we check out (I'll just put in some fake data here and go to Complete Order), we are going say that the order was successfully completed.2297

But now, when we go back to view our cart, it is going to say it is empty, because all of the cookies have been deleted that had that shopping cart data.2312

The other thing to note is that, when we have that the action is equal to checkout,2322

we are simply outputting the current total, and then we output that shipping table.2328

And again, you will notice, at the bottom here, that it doesn't contain the information 2332

to forward all of the items along to checkout.php with the completeOrder action.2335

And that is because all of that information is contained in the cookie.2341

That ends today's lesson; thank you for watching look forward to seeing you next time.2347