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

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:10
    • Lesson Overview
  • Version 15.0 Changelog 0:33
    • outputLink (), outputImg (), is_array () and is_string ()
  • Version 15.0 Coding Example 1:31
    • Version 15.0 Coding Example
  • Version 15.1 Changelog 7:55
    • Removing Redundant Code and Adding New Function Called outputHtmlTag ()
  • Version 15.1 Coding Example 8:37
    • Version 15.1 Coding Example
  • Version 16.0 Changelog 14:55
    • Creating emailOrder() Using New Constants ORDER_EMAIL_FROM and ORDER_EMAIL_SUBJECT
  • Version 16.0 Coding Example 16:30
    • Version 16.0 Coding Example
  • Version 16.1 Changelog 32:21
    • Creating formatAsDollars ()
  • Version 16.1 Coding Example 32:57
    • Version 16.1 Coding Example
  • Homework Challenge 36:27
    • 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 going over our final updates to our store web application.0005

In the lesson, we are going to have four different updates to the store: 15, 15.1, and 16.0 and 16.1.0011

What we are going to do is incorporate some of the material we learned in our lecture on variable-length parameter lists,0022

and then also just add some additional features that are going to improve our web application.0028

In version 15.0, what we are going to do is update the outputLink and outputImage functions we have in the outputLIB.php, our output function library.0035

Currently, these functions take in several different attributes, and they each have two optional attributes: one for ID and one for class.0047

which, you will remember, is so that a user calling these functions can optionally provide0054

the HTML ID or class attribute to use for CSS or Javascript operations.0060

What we are going to do is replace those optional attributes with a single optional attribute, which is going to accept an array.0065

And in that array, it is going to allow name/value pairs so a user can set any sort of options they want for a particular tag.0072

In this case, it would be the image and the a tag.0081

We are going to make use of the new functions we learned about called is_array and is_string to perform some input validation.0085

Let's take a look at some of the code.0093

If we look at the last version of outputLIB.php, which is version 14.2, here we can see that the outputImg function, for example,0095

takes in three parameters: the name of the image file and the two optional parameters0106

for the ID and class attributes that we can optionally specify for the image tag.0110

Additionally, if we look down at outputLink, we can see, it takes four parameters: two required ones (one for the URL of the link,0117

one for the body of the link), and then it also has the same optional parameters (ID and class),0125

again, to provide those ID and class attributes to the a tag, if we desire.0129

Well, in our new versions, let's take a look at outputImg first.0136

What we have done is replaced the ID and class optional parameters with a single parameter called attributes.0142

And what attributes is: it is an associative array that is going to contain name/value pairs0149

that allow a user to set any HMTL attribute pairs for the image tag that they desire.0156

So, they can do class and ID, just as before; but they can also add other parameters, as well, such as maybe the image size, width, or height.0161

The way the file changes is: we start similarly by creating the image tag and setting the source attribute to the location of the image file.0171

But then, things change a little bit: if we look at the old version, what we did is tested to see if the ID or class parameters were empty.0182

And if they weren't empty, then we went ahead and appended the ID attribute, along with the provided value, to the image tag.0190

And we did the same thing for class.0198

What we do in the new version is: we make use of a foreach loop to loop over an attribute's array that the user can optionally provide.0201

And what we do is: for any attribute name/value pair that the user provides in this associative array, we add that to the image tag,0209

so they can still the ID attribute; they can still add the class attribute.0217

A couple things to notice: the input validation function, or test--we tested the attributes array that they provided.0222

It is not empty, which means that it is not an empty array.0229

And then, we also test to make sure that it is an array, because when we loop over it, we want to loop over an array.0233

This makes use of this new is_array function that we learned about, that tells us about data types.0238

If it is an array, and if it is not empty, then we loop over the attributes array.0243

And then, we also perform tests on each name/value pair provided.0249

Name/value pairs in HTML attributes are both strings, so we test that, for each element of this associative array attributes,0253

that an attribute name exists and an attribute value exists, which are the key and the values of that array,0262

and then, also that each of them is a string; and we make use of the is_string function that we learned about in the last lesson0268

that tells us if the data type is string.0275

If they are both provided and if they are both strings, then we go ahead and add that attribute name and value to the image tag.0278

And so, what these functions do is: it allows us to perform some more intense input validation,0287

to make sure that we are only performing the function on acceptable inputs.0292

Now, if we go down and look at the outputLink function, we have done the same thing.0297

We have replaced the ID and class optional parameters with the attributes parameter.0304

And we basically have the same code here that we did in the outputImg function.0311

The only difference is that we create an a tag, or a link tag; we append the URL provided by the user.0316

We go ahead and use the same functionality to test attributes, to make sure it is an array,0324

to make sure it is not empty, to make sure that any name/value pairs provided are strings...0328

And we add those attributes to this a tag, and then we close the a tag and output the body that the user provides from the second parameter.0332

And we can see these new functions being used in two different spots.0343

For example, if we go to outputItemLink, which is our function that outputs on a shopping cart or the department home page,0347

a link to an item's page that contains a small image of the item, and a link that is the item's name,0354

we can see that in that function, we use outputLink twice.0362

And the second time that we use outputLink, we had been specifying the class attribute for the itemID link.0366

If we look at the old version of itemLink, the last version, we can see that we have used the old syntax for calling this outputLink function,0376

where we provided a null value for the ID, because as we know, with optional parameter, if you provide a second optional parameter,0384

you also have to provide a value for the first one.0393

So, we provided null for the ID, and then we provided itemLink as the class.0394

And then, in our new version, we replaced those two parameters with a single parameter that is an array,0401

that contains the attribute name class, and then its value, itemLink.0407

And so, that is how we make use of that new outputLink function.0412

Additionally, on item.php, which is our page that shows the item's information, we use the outputImg function to output the item's image.0416

And the item image that we output...we include in that image tag the ID HTML attribute and set it to item image.0425

Now, in our previous version of item.php (for example, in 14.2), we can see that we specified two parameters to the outputImg function,0435

the first one being the url, or the image file, and the second one being the value of the ID attribute.0451

Well, in our new function, we can accept an array of HTML attributes.0458

Here, we have gone ahead and created an array that submits the ID attribute, set to the value itemImage.0463

And so, that is how we make use of those new outputImg and outputLink functions.0470

Now, what you may have noticed when we created these outputLink and outputImg functions is that they both contain a lot of redundant code.0478

And we know from our experience that any time we have redundant code, or code that is used over and over again,0486

we want to extract it and put it in one spot, and make use of it--for example, by creating a function that makes use of that code.0492

That is actually what we are going to do in version 15.1.0500

We are going to take the redundant code in both of those updated functions, and we are going to place it in a new function called outputHtmlTag.0502

And this new function is also going to make use of the is_array function and the is_string function that we had learned about in the last lesson.0510

So, if we go and take a look at the outputLIB for version 15.1, we can see, we have created a new function here called outputHtmlTag.0518

What it does is accepts one required and two optional parameters.0530

The required parameter is the tag name: for example, for an image tag, it would be the string img.0535

For a link or an anchor tag, it would be the letter a.0541

It also allows an optional parameter that is an attributes array, which is the same thing that we just created for our outputLink0545

and outputImg functions, where a user can call the function and pass in any HTML attribute name/value pairs it wishes to set.0554

for whichever tag this function is going to create.0563

Additionally, the user can optionally provide a body to the tag.0566

For example, in our image tags, we don't have a body to the image tag; but for our link tags, we have a body.0570

For example, the name of an item is in the body of the anchor tag, and that is what the user was able to click on to go to the link.0576

What we do is: we are dynamically creating this tag "on the fly."0584

Basically, this starts off creating the tag, and we create the tag with the name passed in in the value tag.0590

And then, we go through and add any attributes provided via this optional attributes parameter, and add those to the tag.0597

For example, for the a tag, what we do is: this attributes associative array is also going to include the attributes0607

that we hard-coded before (into the image tag and the anchor tag, for example).0617

For example, in the image tag, we always have a source attribute, and in the a tag, we always have an href attribute.0623

So now, what we do is: because we have created a generic HTML tag function, and href and source are both HTML attributes,0629

we add them to an attributes array that we pass to this new outputHtmlTag function.0637

And then, this outputHtmlTag function loops over these attributes, just like it did in the last versions we created0644

of outputImg and outputLink, and appends them to whatever tag it is dynamically creating.0651

Additionally, if the user provided a body, and the body is a string (because in HTML, the body has to be a string),0656

then we go ahead and add the body to the tag, and then create a closing tag after the body.0664

And if not, we just go ahead and close the tag without any body.0670

And then, we return the well-formed HTML tag.0674

So, if we go and look at outputImg, it has changed a lot from the last version.0678

In our last version, we started creating the image tag, building up a string that includes a source attribute, and so forth.0684

And then, we looped over all of the different attributes that we may want to pass into the function.0696

Well, now what we do is: we still allow this function to be passed a number of attributes to add to the image tag,0701

but then, what we also do is: we are adding an attribute to this attributes array.0710

And if the attributes array wasn't passed in (for example, the user didn't provide maybe an ID or a class attribute),0716

it goes ahead and creates that array, and adds the source attribute, which we always need for our image tag.0721

And we set it equal to the path to the image file, which uses the IMAGE_DIR constant and the image file parameter passed to the function.0726

Then, what we do is: using this attributes array, which contains any attributes that were already passed into the function,0736

along with the added source attribute, we pass that to the outputHtmlTag, and then include as the first argument to the tag the string Img.0741

And what that is going to say is "I want outputHtmlTag to create an image tag, and I want it to have all of the attributes0751

contained in this attributes array," which is going to always include the source attribute, as well as0758

any additional attributes a user provided that called this outputImg function.0763

Similarly, if we look down at the outputLink function, its content has been changed.0768

Before, this is what it looked like, where it started building up the a tag on its own, and then it looped over all of the different attributes0777

that were possibly provided to the function, and then it closes the link tag with the body.0788

Well, in the new version, what it does is: it accepts the same amount of parameters:0794

the URL, the body of the link, and also any attributes a user wishes to add to that a link.0800

And then, what it does is: similar to the new outputImg function, it adds to this attributes array the href HTML attribute and sets it equal to the URL value.0807

And because that is an attribute that we need to have in our anchor tag, so that the link will work,0818

we add it to this attributes array, and then we call outputHtmlTag.0823

With the first parameter, we provide the letter a, which is the name of the tag we want to create, which is an anchor or link tag.0829

We provide it the attributes array, which contains any attributes passed into this outputLink function,0834

as well as always providing the href attribute, along with its URL value.0840

And then, we also provide the body that was supplied to the outputLink function to the outputHtmlTag, because our links have a body to them.0844

And you will notice, there is a difference there and in outputImg.0853

And in outputImg, when we call the outputHtmlTag, we only passed it two parameters or two arguments,0857

because the image tag, in this case, doesn't have a body.0862

And so, that is how we have taken that redundant code that looped over the different attributes0868

that we could provide to an outputImg function or an outputLink function, and consolidated it into one file that we can use now.0873

We could use it to create any kind of HTML tag that we want, not just anchor tags or image tags.0881

We could use that in many different ways.0886

And so, that demonstrates how we can create a function that we can reuse in multiple spots.0890

In version 16.0, we are going to add some functionality--the last functionality we are going to add to the web application.0897

We are going to create a new function called emailOrder.0905

And what that is going to do is: when a user submits an order on checkout.php, after they have added items to their Shopping Cart0908

and then when they submit their shipping information, an email is going to be created that gets sent to the store administrator0915

that is going to contain the order's information (such as all the items that were in the order, the contact information,0923

the shipping information of the customer...), and it is also going to include an order timestamp that shows when the order was created.0930

And so, before, when an order was created, we simply output a message to the user.0937

This makes it a little more realistic in functionality, in that we could use this to email a store administrator to manually process an order.0942

And the order email contains information needed to do that.0950

In order to do this, we are going to be creating two new constants: ORDER_EMAIL_FROM and ORDER_EMAIL_SUBJECT, in config.php.0955

And we are going to use these constants in our emailOrder function to use in the mail function provided by PHP.0962

One other thing that we are going to do is: in checkout.php, in our current version, when we echo the user's shipping information0970

that they supplied to us, we always output the apartment line.0979

Now, we are going to optionally only do that if the user provides that; and that makes the application a little more real-world-oriented.0983

Let's take a look at the emailOrder function.0992

The emailOrder function is a utility function, so we create it in utilLIB.php.0996

And here is what it looks like: it takes four required arguments.1001

The first required argument is an associative array of all of the items in the user's Shopping Cart that a user is going to purchase.1006

It contains the order total, or the total that gets calculated by the calcCartTotal function, including sales tax.1013

It accepts a custData parameter, which provides all of the shipping information that a customer provided on the checkout.php form.1020

And then, it also includes the item catalog, which is going to allow us to look up item information1030

so that, when we create our email to the store administrator, we can include things such as an item's ID and an item's price and so forth.1034

The first thing we do when we enter the function is: we use the date function and use the ORDER_DATE_FORMAT constant1042

that we had previously created to generate a timestamp for the order when it was completed.1049

Then, what we do is: we start building up an email message to send to the store administrator, saying "an order was created on this order date."1054

And then, what it does is loops over all of the items in the cart; and this cartItems array is the same one that we have been using in checkout.php.1062

Its keys are the item ID's of any items in the shopping cart, and the values are the quantities of those items that the user wishes to purchase.1072

So, we loop over the items array, extracting the ID as the key and the quantity desired as the value,1080

and then, for each item that was purchased where the quantity is equal to 0, we output a line in our email.1089

And the email is going to say, "Item ID so-and-so was purchased; it has this price, and the quantity purchased was itemQty,"1095

which was what was provided by the cartItems array.1102

You can see here, we have a test condition that we only execute this code if itemQty is greater than 0.1106

And then, down here, you can see how we look up the item price.1112

Because the cartItems array only contains the item ID and its quantity, we need to look up the price of the item to include in our email.1117

So, we use this item catalog, which was passed in, which was that itemCatalog array that was created in our catalog.php form.1124

And we look up the item by its ID and then its price, using the price key.1132

And by passing this item catalog in, it shows how we have decreased our global dependency in this function,1137

because we could also access itemCatalog using the global keyword or the GLOBALS superglobal array.1146

But by passing it in, we eliminate that global dependency and allow this function to be reused in more spots.1153

Then, we go ahead and just output the order total right here, and then in the email, we loop over the customer data that was provided,1159

which includes all of the address information for the user.1168

And what we do is: because the information in custData is provided, it has keys which are variable names1171

(like firstName, for example, which is in this lower camel case notation).1179

When we output an email, we want to say "The first name was," and have a formatted output.1184

And we want to have first name, with a space in between, as opposed to this sort of variable name.1190

So, what we do is: we have created a switch statement that loops over each part of the address passed in from custData.1194

And if the current variable that you are looking over in the array equals either firstName, lastName, street, apt, city, state, or zipCode,1201

what it does is goes ahead and sets this string called formatted to the formatted output that we are going to put in our email.1211

When it gets to the end of the if statement, if formatted has been set (which means that there was an item in that custData array1220

that matched one of the cases in our switch statement--for example, a state was provided/a city was provided),1229

and also if the current address value that is being iterated over in the custData array is not empty,1234

then we go ahead and just output a line in our email address that has the formatted string that represents that part of the address.1243

So, for example, for the state, it is going to output State, with S capitalized, as opposed to just this variable name state.1254

For zipCode, it is going to output ZIP Code as two words, as opposed to this lower camel case notation variable.1265

And then, it is going to output whatever value the user provided.1271

We have built up this email that is going to contain all of the information about the items in the order.1276

It is going to contain an order timestamp; and then, it is also going to contain the shipping information that a user provided on checkout.php.1281

Then, down here, we call the mail function, which we have used before.1289

We send it, in this case, to the admin email, the store administrator's email, which we get using the ADMIN_EMAIL_TO constant that we had previously defined.1294

We include the email message we built up as the message to be sent.1305

And then, for the subject and the From email address, we use these two new constants that we have created in config.php.1309

And if we look at config.php, we can see, we have just added this constant ORDER_EMAIL_FROM,1318

which sets...when the store administrator receives an email, we have to set a from address for that,1324

so in this case, we just set it to the same as the admin email address,

And then, we also create a constant, ORDER_EMAIL_SUBJECT, which is the subject line that the store administrator is going to see1335

every time an order is processed, and it is just going to say "Educator Store: Order Received."1341

And as we know, the mail function returns true if it was successful and able to pass the email on to the outgoing mail server.1350

So, what we return for this function is: if the sending of the email was not able to be performed--it was unsuccessful--1359

we are going to return the value false; if it was successfully sent, we are going to return the timestamp that we created1368

all the way at the beginning of the function, which is the timestamp of the order.1375

So, this function is going to return two different values, and it is going to depend on the value of sent,1379

which is the return value of the mail function.1384

Here you can see: for our return expression, we use a ternary operator that says, "If sent is true" meaning the mail function1388

was successful, "we are going to return the timestamp of the order that was created; and if not, we are going to return the value false."1395

And we are going to be able to use that in checkout.php to appropriately output a message as to whether or not the order placement was successful.1401

If we go and look at the new checkout.php, we can see...well, first of all, let's look at the beginning of the file.1410

We can see that, when the action is completeOrder (which means that the user has submitted the shipping information),1429

now, instead of just outputting the user's shipping information, this is where we call the emailOrder function.1437

And we provide it the cartItems array, the calculated order total, the array of customer data, shipping information,1444

and then also a reference to the item catalog in catalog.php.1452

And as we just learned, emailOrder returns false if the email wasn't successfully sent, and it returns the timestamp of the order if it was.1457

Here we have a test that if order timestamp is not true (meaning it is false), then we are going to set our error flag1464

and output an error message saying, "There is an error in completing your order."1473

If order timestamp is not equal to false, which means it turns out that it is equal to the order timestamp,1477

then we just set the page title as "Thank you for your order."1483

And then, down in the output section, we can access this order date/time variable that has the value returned by the emailOrder function.1486

And one thing to note is that, when you go to checkout.php, and you provide the action completeOrder,1498

that form...if we look at the old version of checkout.php, all it receives is the order total that was calculated after the user had submitted their shipping information.1507

And it also receives the custData; what it doesn't receive is what was included in the shopping cart.1531

And so, because our emailOrder function needs to know what was included in the shopping cart to output in the email,1537

we are going to have to (it would be easier if we just look at the page), when we submit the order and hit Checkout...1543

we are calling the checkout.php page with the action checkout.1560

What that does is submits the cartItems to the checkout.php, calculates the total, and then outputs this form.1564

When we go ahead and submit this form, we invoke checkout.php with the action completeOrder.1573

And previously, what it did was just output the timestamp of the order, which we had generated within checkout.php,1593

output the total, and then output the user's address.1599

However, because now when checkout.php gets submitted with the action completeOrder (when the user1603

hits the completeOrder button here), the emailOrder function is also going to need1609

all of the cartItems data that was passed in to checkout.php when the cart was submitted.1614

So, what we have to do is make use of that form functionality, and what we are going to do is create hidden input elements.1620

within this shipping information form that is going to contain all of the information about the items that were in the cart.1628

So, when we submit checkout.php with the action completeOrder, all of that cart item information gets passed along,1638

so we can access it in emailOrder and look up what items were purchased.1644

So, if we go and look at checkout.php, and we look in the email output section for checkout,1649

we can see that we have a form we created, that we have been using before, where we output this form1658

where the user can input their shipping information (their name, street, apartment, and so forth).1664

And then, this is actually the old version; and what we have is...we made use of forwarding the order total on to the completeOrder section of checkout.php.1671

Well, in the new version, we are adding to that form; so we are outputting the form; we are outputting a hidden input tag1681

that contains the order total; and then, what we are also going to do is loop over the cartItems array and, for each item1690

that was in the cart that has a quantity greater than 0, we are going to create a hidden input element1698

that is named items, which is the name we had given to the shopping cart items when we submit the viewCart.php form.1704

So, we are going to create an items array for each item, and set the key to the array equal to the item ID.1716

So, for any item that is in the shopping cart, we create a hidden input element for it.1724

We set its name equal to the items array, with the key being the item ID; and then we set the value equal to the quantity that the user set.1729

What that is going to do is: when we submit this form (and this form gets submitted to checkout.php),1736

but it gets submitted with the action completeOrder), all of these cart items' information is going to get passed along.1745

So, when we begin checkout.php all the way at the beginning, and we are processing the section (where is it?)1750

when action is completeOrder, and we go to use the emailOrder function, we will have access to this cartItems information, as well,1760

because they were forwarded along by including it in the bottom of that shipping form.1767

One other thing that we also did, just to clean up the look of the application, is: in the final page that shows a Thank You message,1773

which looks like this, if the user doesn't supply an apartment...before, we would still output apartment number.1782

For example, if we provided nothing, it would still be validated, because we said it was OK if the user doesn't provide an apartment.1793

And actually, this is the new version, so it works.1800

But in the previous version, it would output Apartment Number with a blank value.1802

What we have done here is updated checkout.php to include an if statement, so it checks if the custData array had the apartment value supplied.1808

And if it did, it goes ahead and outputs that line; and if it doesn't, then it just leaves it blank.1819

And so, to finish looking at this emailOrder function, let's look at the email that gets sent out when a customer submits an order.1825

And if we load up...I have an email loaded up in Thunderbird; we can see, the email says, "An order from the Educator Store was made."1835

It shows the timestamp that was created in the emailOrder function.1843

It shows a list of all the items that were in the shopping cart; when we looped over the cartItems quantity, we created a line for each item in there.1847

In this case, it had the item-1001, 1002...each with the quantity 1; and it lists their price.1856

Additionally, it lists the total that was calculated, including the sales tax.1861

And then, it also outputs the user's shipping and billing information.1867

From this, theoretically, the store administrator could go and, when they receive a manual payment for the order,1871

they could go ahead and process the order and ship it out.1876

And so, that is how that emailOrder function works to enable an order to be emailed to the site administrator when an order is completed.1879

In our last update to our web application, it is just going to be a formatting update.1892

One of the things that you might have noticed in previous versions of the web application...1898

for example, this was when we had checked out, just a moment ago, on version 16.0...1902

you can see that some of the dollar amounts aren't formatted correctly.1907

A dollar amount always has two digits after the decimal point.1912

Well, what we are going to do in 16.1...and if you go ahead and look at the cart, you can see, for example, here too,1916

that $15, the price of the t-shirt--it doesn't have a decimal point and two zeroes afterwards.1923

So, it is not formatted and does not look as nice as the other ones do.1928

We are going to create a function called formatAsDollars that is going to take a numeric value (in this case, a price of an item)1932

and then output a properly-formatted dollar string.1938

So, the function, again, that we are creating is going to be called formatAsDollars.1943

It is going to be located in utilLIB.php; it is going to format a numeric value; it is going to round it off, if it is not already rounded, to two decimal places.1947

It is going to always output two digits after the decimal place.1956

And then, it is also going to include the dollar sign in the formatted string.1960

And we use this function, for example, in our emailOrder function, when we send the email to our site administrator.1965

because that includes the total of the order; and then also, we are going to use it on all the pages where we display dollar amounts:1972

for example, the one we just looked at--the Shopping Cart--where we display the price of the items.1977

So, if we go ahead and look at the cart for 16.1, we can see that now the prices are properly output.1984

So, for example, if we add quantity 1 for a t-shirt, we can see: before, where it just output 16.5, it outputs the formatted dollar amount.1993

If we go and look at utilLIB.php in version 16.1, we have this formatAsDollars function.2007

It takes an amount; it uses the is_numeric function to test if an amount is a numeric value--2012

if it is an integer or a float or a string that represents an integer or a float.2019

If it is not, the default action of the function is: it is going to return a string that says "non-numeric."2024

That is maybe not the best way to handle errors, but with the skills that we have right now, that is one way of handling things.2032

We could test for that back in, for example, viewCart.php.2037

We could test to see that, when we try to format this dollar amount, if it returns maybe not output anything,2045

or maybe just to output $15 without the properly-formatted two zeroes at the end.2051

So, that is the first thing that it does: it tests if it is a valid number, and it goes ahead and rounds it to two decimal places.2059

And then, here we just make use of some of the string functions that we had learned about in previous parts of the course.2065

We use the explode function to dissect the amount passed in, based on the decimal point.2071

If there are two parts (meaning there is a part before the decimal point and a part after), then we separate them into a pre part and a post part.2078

And then, we perform some tests on the number of digits in that post decimal part.2085

For example, if there are no digits, as is the case with the cotton t-shirt in our last version (there is no decimal point,2089

and there are no digits afterwards), then what we are going to do in the function2098

is go ahead and output two zeroes after the decimal point.2101

For example, if there is only one digit after the decimal point, we are going to append a zero to it, as well.2104

And then, we just return the pre-decimal part with this updated post-decimal part, appended with a dollar sign.2109

And then, when we return that to whatever script calls that, that formatted dollar amount is there.2118

So, as you can see, if we take a look at item.php (let's look at the current version--if we look at 16.1 item.php),2123

when we output the price, you can see that we changed it so that it uses that formatAsDollars function.2140

We have used that in a number of different places in the script: viewCart.php, also on the department.php page...2146

And then, we have used it within our function libraries in utilLIB.php, for example, in version 16.0,2152

where we have the email order function that we created in the last version.2158

We can see that, when we output the total in that email from the orderEmail function, we use the formatAsDollars function.2168

That just cleans up our application a little bit--makes it look more well-polished and more like a real, live web application.2176

And that is the last update that we are going to be doing to the web application.2184

For the last homework challenge we are going to have in this course, I just want you to go over and make sure you understand2188

how we use the is_array and is_string functions in various places, throughout the updates to the web application,2193

as a means of validating function inputs, because that is something...when you develop real-world code, you are going to want2199

to be doing your own, and be always testing your function inputs to make sure that they are valid values, or they are valid data types.2204

Also, examine how outputHtmlTag, the new function we created, improves the web application2211

by removing the redundant code where we had an outputImg and outputLink functions.2216

It moves it into one spot and creates a function that we can now use to create any HTML tag we desire, dynamically.2221

And we can use this in the future to create any other tag that we wish.2227

Additionally, just review how we have implemented that forwarding action.2232

When a user submits checkout.php with the action checkout (which is when they submit their shipping and billing information),2236

how all of that information is included in hidden input fields in that form, so that, when the completeOrder section of checkout.php2245

is executed, it will have access to the cart items that were provided and can generate the order email2258

that lists all of the items that were purchased.2268

Additionally, just make sure you understand how the formatAsDollars function works,2271

and how we have to use that function in order to output numeric data as decimal strings,2276

because numbers, when you convert them to strings in PHP, if they are floats (for example),2283

and it's just 16.50, it is not going to output 16.50--we have to use a string formatting function.2289

And the reason for that is just that the automatic conversion of floats to strings doesn't add that extra 0 in there to make it two decimal places.2296

And so, just understand how the formatAsDollars function provides us that opportunity to format that numeric string.2304

Thank you for watching today's lesson; I look forward to seeing you next time.2312