Sign In | Subscribe

Enter your Sign on user name and password.

Forgot password?
  • Follow us on:
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 Introduction to PHP
  • Discussion

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (1)

0 answers

Post by dave smith on August 11, 2015

Professor Matthew Machaj is a genius!

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:10
    • Lesson Overview
  • Version 13.0 Changelog 0:48
    • Updating 'viewCart.php' and calcCartTotal()
    • Creating isValidCart()
    • Adding Three New Item to the Store & Links
  • Version 13.1 Changelog 17:20
    • Updating outputItemLink() to Remove Its Global Dependency on $itemCatalog
  • Homework Challenge 20:34
    • Homework Challenge

Transcription: Web Application Development

Hello again, and welcome back to's Introduction to PHP course.0000

In today's lesson, we are going to be continuing development of our web application, incorporating what we learned about while loops.0004

We are going to go through two different versions of the application today0012

We are going to do version 13.0, in which we are going to make use of while loops to reduce some of the static coding in our file,0014

which means, right now, in some of our scripts and functions, we have values that are hard-coded in there.0023

And what we are going to use is while loops to iterate over arrays, so that we can do things more dynamically.0027

We will see more about that as the lesson goes on.0034

And then, in version 13.1, we are just going to be making a small improvement to our application,0037

where we are going to be removing a global dependency that our outputItemLink function currently has.0042

In version 13.0, the first change that we make is to viewCart.php.0050

And what we are going to do is make use of viewCart.php, which is what displays our shopping cart to all of the users.0057

And right now, we have, inside the HTML file for our viewCart.php, hard-coded the output for each item.0063

So, for every item that we have in the store, we have created an HTML segment that outputs that information.0071

For example, we look at viewCart.php as it stands (and this is the last version, 12.0).0079

You can see that we have a section here of HTML for each item.0085

For item-1001, for example, this is the section we have; and we have the same exact section repeated down here for item-1002,0091

and then 1003, the only difference being that, in each section, we changed the item ID.0098

Any time we see the same code being reused over and over, we want to take advantage of the programming language's functionality0106

to eliminate that redundancy and reduce the number of errors that are possible.0114

What we are going to do is have this page use a while loop.0119

And what it is going to do is iterate over the itemCatalog array, which is in catalog.php, and output a row for each item that is in the catalog.0124

What that means is: any time we update the item catalog (we add a new item, for example),0133

then when we go to viewCart.php, it is automatically going to add a row for that in the shopping cart,0137

as opposed to right now--if we added a fourth item to the cart, for example, we would have to copy this whole section,0142

and then paste it further down the page, and so forth.0148

So, we are going to eliminate that.0153

The other thing that we are going to take advantage of a while loop for is in calcCartTotal,0155

which is our function that calculates the shopping cart total.0160

And currently, right now, if we go and look at it (and it's in utilLIB.php--this is version 12.0), similar to what we had in viewCart.php,0163

where we had an HTML section that is hard-coded for each item in the store, you can see that, for each item, we have a hard-coded calculation.0175

So, for item-1001, we have this calculation here.0185

Now, if we wanted to add a fourth item to the store, for example, we have to go ahead and go to our calcCartTotal function,0189

add another whole line, and put 1004, for example, in there.0197

Obviously, this is not a very scalable solution, because if we have thousands of items in our store, every time we want one,0202

we don't want to have to go to the code and alter our PHP code.0208

So, what we can do is: we are going to incorporate a while loop in this function, and the while loop is going to0212

allow us to dynamically calculate the shopping cart total, based on whatever items are in the item catalog at that time.0217

So, it is going to loop over all of the items that are in the shopping cart and dynamically add them up.0226

So, if we have a store with 5 items that are in the shopping cart, it will go ahead and add them all up0235

without having to actually hard-code all of the item numbers in there.0241

Let's go and take a look back at the viewCart example.0245

As we said, for viewCart, we have a different section for each item.0248

This is version 12.0; well, if we look at version 13.0, scroll to the beginning and scroll back down, this is the same section for outputting the item rows.0253

And as you can see down here, we have the submit button for the form.0266

So, instead of having three different sections of HTML code (one for each item), we have one section of code.0270

It uses a while loop to loop over all of the items in the itemCatalog array.0276

And this common code that we had for each item that we were outputting a row for is incorporated in this while loop.0280

And so, what it is going to do is: as it iterates over itemCatalog, it is going to dynamically generate a row for each item.0288

As you can see up here, just as a review, we are using the list construct and the each function.0296

And again, the each function, when it is supplied an array--what it does is returns an array.0301

And at the 0 index of that array is the key of the current array element in itemCatalog in this example.0310

So, currentItemID is going to get set to the key of itemCatalog.0316

If we look at itemCatalog, the array, the keys to that array are the item ID's.0321

The values of that array are associative arrays that contain the items' information, such as its name, its price, its image file extension, and so forth.0327

Because that is the value of the elements in itemCatalog, the second item that is specified--the list is going to get set to that value.0336

So, in currItem, we are going to be storing the associative array with the current item's information.0344

We can use currItemID and this currItem variable in the code below to do what we had done before in the last version,0349

in that we are going to output the item link; we are going to output the price; and then, we are going to output a box for the quantity.0359

Now, the one change here is: aside from changing the while loop, the code changed a little bit inside here for the price,0365

because in version 12.0, for the price, we manually accessed the global variable itemCatalog,0373

looked up the current item by its item ID, and then accessed its price.0382

However, in our new version of viewCart.php, we have already loaded up the information about the current item0387

when we were iterating over this itemCatalog array.0394

So, rather than go ahead and access that global variable again, we are just going to take advantage of the currItem variable0398

that we have already created in this while statement, and we are going to use that to output the price.0404

So, this is going to do the same thing as before: the Shopping Cart page is going to look0410

the same as it did in the last version, except now it is being dynamically generated.0415

And what we are going to see in a little bit is: I am going to increase the size of the catalog and add a few more items,0419

and we are going to see how this page is going to automatically add rows for those items,0424

without us having to go and hard-code new HTML sections for each item.0427

Similarly (and let me erase what we had added here), in calcCartTotal, our cart total calculation function,0434

this is the version 12.0; in our new version, we are going to make use of the while loop.0444

If I scroll up to where that is...basically, what we have done is replaced each of those hard-coded subtotal addition statements.0449

And what we have done is created a while loop that is going to iterate over the cartItems array, which is passed into the calcCartTotal function.0459

And as we know, in the cartItems array, the keys of that array are the item ID's,0467

and the values associated with those keys are the quantities the user selected in the cart.0472

So, when we loop over that array, we are going to pull the key out, which is going to be the item ID, which we are going to store in the variable itemID.0477

We are going to pull out the quantity, which is the value of each item in this array.0484

And we are going to store that in this variable itemQty.0488

And then, what we are going to do is: we are going to test to see if itemQty is equal to 0.0492

If it is, we are going to make use of our continue statement, because if the item quantity is equal to 0, we don't need to perform any calculations.0496

There is nothing to add to the subtotal.0502

And as we learned about continue, that just means the loop is going to start over again.0504

It is going to pull the next item from the cartItems array and try to calculate it.0507

For items that do have quantities over 0, this is going to return false; so it will go down here and execute this statement,0511

which is a parameterized version of all of these hard-coded addition statements that we had in our last function.0519

So here, we have had three different statements for three different items.0530

Here, we dynamically use the item ID that we extract from cartItems and the itemQty, so that, on each iteration of the loop,0533

it is going to add to the subtotal the quantity related to any item that is in the cart.0542

And so, in this case, we can have as many items in the cart as we want, and we don't have to update this function with a new line0550

that says, "OK, now add the price for item 4."0555

The other thing that we have done in this version is: we are going to make use of while loops in a function called isValidCart.0564

In our last version of checkout.php, when we validate the cart, our validation scheme is a simple validation.0572

We are just saying, "As long as one item in the cart has a quantity greater than 0,0581

then we are going to say that the cart is valid and continue to calculate the cart total."0585

What we can do is: if you see right here, we have a hard-coded boolean expression for each item that is in the cart.0591

So again, if (let's say) we add a fourth item to the store (let's say it's 1004, we are going to have to add another whole boolean test down here.0602

And again, this is not going to be scalable, because every time we add an item to the store, we are going to have to change that.0617

What we can do instead is replace all of this with a function called isValidCart.0623

And if we look at the new version of checkout.php in version 13.0, we can see, we replace that boolean expression with isValidCart;0627

we pass to it all of the items in the cart, which is in this associative array cartItems.0635

It is going to return true or false: it is going to return true if any of the items are greater than 0.0639

And in this new function, we are going to make use of a while statement0644

so that we can loop over any items that are in the cart, not just those three that we have hard-coded.0647

So, if we go and look at utilLIB.php, we have added this new function isValidCart that takes the associative array cartItems.0654

Again, how the function works is: if any item in the cart has a quantity greater than 0, then the cart is considered valid.0669

So initially, we create a boolean flag called isValid, and we set it equal to false: we say the cart is not valid.0675

Then, what we do is loop over all of the items in the cart by looping over the cartItems array.0681

And if any of those items has a quantity greater than 0, then we update our boolean flag to true, because the cart becomes valid,0688

so that, when we return isValid, the checkout.php is going to know that the cart is valid.0695

And as you can see up here again, we have used the list construct and the each function to loop over this cartItems array.0702

And because, as far as cart items, the key is the item ID, and the values in that array are the quantities,0709

and all we are really interested in is the quantities, we don't need to set a value that would extract the key from cartItems0715

when we are looping over the cartItems array, because we don't the item ID; all we need is the quantity.0725

So here, you can see where we have used the syntax where you leave that first item blank.0730

So, instead of maybe having itemID here (which we don't need in this code down here), we just need itemQty.0733

The syntax for leaving that out and making sure itemQty gets set to the value is to just put a comma there, with no value.0740

And so, that is how isValidCart works.0749

Now, what that is going to do is: if we add items to our store, or add items to our catalog, now our viewCart.php is going to0751

automatically add a row for each item that is in the store.0759

When the user submits the cart, this function--this validation function--is going to loop over all of those new items0763

to see if any of them have a quantity greater than 0.0768

Let's take a look at how this works in action.0772

Right now, we have a shopping cart, and if we submit it without adding any items to the cart--all quantity 0--0778

and we hit Checkout, we are going to get an error saying "you need to add at least one item to your cart."0783

Right now, we have three items in our cart.0789

This table, as we just learned, is now created dynamically be iterating over the cartItems array.0792

When we submit the checkout form, the isValidCart function is running by dynamically looping over all of the items submitted on the cart.0799

Additionally, when we calculate the cart total, if we go ahead and (let's say) put a quantity 1 here,0808

calcCartTotal is going to be run, and it is going to loop over all of the items that were submitted in the shopping cart.0814

And for any that have a quantity greater than 0, it is going to add that to the subtotal.0818

So here, we get $16.50 as our total.0822

That is how it works: now, let's go and add three more items to the store.0826

Our current catalog.php has three items in the store: 1001, 1002, 1003.0836

Well, I have gone and created an updated version that we are going to set catalog.php to, and this version has six items in the store.0844

And what we are going to see is: just by updating this catalog.php, and updating this itemCatalog array,0853

our shopping cart is automatically going to be able to add rows for all of those new items.0860

We are going to be able to validate the shopping cart for all of those new items added,0864

and we are going to be able to calculate the total for all of those new items that were added.0870

I'll just close these down; what I'll do is go ahead and rename the old catalog and set the new one to just catalog.php, so it will actually get read.0874

Now, when we go and reload our Shopping Cart page, I think it's pretty impressive: just by hitting refresh,0896

we can see that the three new items that were added to catalog.php automatically get added to our shopping cart.0903

Additionally, if we submit 0 items for all of our shopping cart, isValidCart is going to work for all of those new items that were added,0910

because it goes over all of the items in the cart, including all of these new ones--not just the ones we had hard-coded into the script.0918

Additionally, it is going to calculate the cart total for these new items, as well.0926

Let's say we want to buy one winter jacket, which is a new item, and we hit Checkout.0929

You can see that the calcCartTotal function worked fine, and it was able to calculate the total for that new item that we added to the store.0937

So, it is pretty impressive how the flexibility of our code has increased.0944

And now, just by changing the data behind our website, this website can automatically cope with that and make changes.0948

The other thing is that now we have added these new items to catalog.php, I just want to mention that I have also added the links to them on the homepage.0959

What I have done is put one of each of the new three items in each of our three departments.0967

So, if we go and look back at the homepage now, you can see that there are three new items added.0974

For example, if we click on DVD Player, which is a new item, DVD Player shows up.0980

And this, again, is an interesting point, in that we had created item.php many versions ago.0985

And now, because that item.php is able to display the information for any item, just by adding a new item to the catalog0995

and updating the itemID query string that we pass to item.php, we are able to print out the information for all of these new items.1004

That is opposed to having to add three whole new item pages to our website.1014

Maybe we would create three more new HTML pages, one for each item.1018

We haven't had to add any new pages--no new PHP pages, no new HTML pages.1022

All we have done is updated our data in the background, which is in that itemCatalog.php.1026

And because of the dynamic nature of item.php, we are able to automatically display the new items, as well.1031

That is pretty impressive, as well.1038

Now, in version 13.1, what we are going to be doing is making one small change.1041

We are going to be changing outputItemLink; and what we are going to do is remove its global dependency on the item catalog, which is defined in catalog.php.1047

If we look at our current version of outputItemLink (this is version 13.0), we can see that it has the dependency on the global variable itemCatalog.1057

And it uses this global variable to load up an item's information from the catalog.1069

So, for example, because it depends on a global array existing called itemCatalog...let's say we want to reuse this function in another web application.1075

Maybe we add it to somebody else's website, and they have added all of the items in their catalog to an array called items.1087

And this right here is how you would access that.1093

Well, because our version is hard-coded to be dependent on itemCatalog, it is no longer going to work in there.1097

So, what we are going to do is: instead of using this global array to look up the item's information,1106

we are actually just going to change it so that we just pass the item information directly to the function.1113

And then, we can eliminate that global dependency.1121

So, the way that looks in the finished version 13.1 is: we can see, we have added a new parameter called item,1124

which is an associative array containing information about the item (name, price, file extension, and so forth).1131

The rest of the function is going to work the same, but now we have eliminated that dependency.1139

We can move this function library to another application, and it will work.1142

Now, the reason that change was made is: if we look at the old viewCart.php, what we can see here is that,1148

when we have been looping over the item catalog, we have been pulling out the current item ID, or the item ID of each item1155

in the item catalog; we have also been pulling out the associative array that contains all of that item information.1163

If we already have that information in this array, we use it down here to output the item's price.1168

So, instead of just passing the item ID to our outputItemLink function, we already have that information in a variable that is loaded;1174

why not just pass it in, in this way?1182

So now, it is going to set that variable item: what it is going to do is: now we don't have to look up the item in the global array anymore.1186

That global dependency is going to be gone, and we can make use of this new function.1194

In our new version of viewCart.php (let's scroll down), now we simply pass two parameters to outputItemLink,1199

the second one being the current item, which is the associative array containing the current item's information1208

that we have already extracted from the itemCatalog.php in here.1214

It is going to make our function more reusable; we can use it in another web application without being dependent on that global variable.1218

Any time you can reduce those global dependencies, that is a good thing for the reusability of your code, which is always a goal when programming.1225

For the homework challenge, I just want you to review how we have used the while loops within viewCart.php in our functions1236

to remove static values and static HTML from our files, so that instead of (for example) having to manually go in1243

and statically add a new HTML row to our table for each item that we add to our store (if we added a fourth item to our catalog,1253

we would need to go to viewCart.php in our old version and add a new HTML row), now, by using a while loop,1262

we can just loop over the catalog, and it generates that dynamically.1269

So, make sure you can understand and see how those while loops really increase the flexibility,1272

and reduce the work that we have to do, as programmers or developers, because now we don't need to update the HTML file1277

(or in this case, the PHP page) every time we add a new item to the store.1287

And that applies for the isValidCart function, because now we can validate a cart with any number of items in it--1290

and then also for the calcCartTotal function, because we can calculate the total of the cart with any number of items in it1295

without having to go and change the function and say, "OK, I want you to also add the subtotal for item-1004, which we just added."1300

Also, just as far as outputItemLink goes, just understand how the global dependency that we have removed from that function1309

has increased the reusability of that function: now, we can use it in more places, and that is always a good goal for our coding.1316

That ends today's lesson; thank you for watching, and I look forward to seeing you next time.1323