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

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (2)

1 answer

Last reply by: Russell Burnham
Sun Jan 12, 2014 3:31 AM

Post by David Zhang on May 13, 2012

Hi Matthew,

I am including newline in my code but when the string is written into the file it's not creating a newline for the new item or string. I have also ran into the same problem in lecture 6's homework challenge.

I tested your files and they work but mine don't please help.

Thank you.

Web Application Development

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:08
    • Lesson Overview
  • Updated Open File Functions 0:19
    • openItemsDataFile ( ), openDeptsDataFile ( ), and openLastAddedFile ( )
  • insertItem() 6:25
    • insertItem() Overview
    • Functions: createItemDataString ( ) and updateLastItemAdded ( )
  • addItemToDepartment () 16:03
    • addItemToDepartment () Overview
    • Functions: createDeptDataString ( ) and updateDepartment ( )

Transcription: Web Application Development

Hello again, and welcome back to Educator.com's Advanced PHP with MySQL course.0000

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

Today, we are going to be starting with a new version of the web application, version 3.0.0010

And we are going to be incorporating what we learned in our last lesson about how to write to files.0014

And what we are going to be doing is: in our last web application (if we look at the last version of it),0022

we had created an admin site where we could add an item to the store and add an item to a department.0031

And what we did was set up forms that didn't really perform the function; they were just basic forms that were a template.0037

And in this lesson, what we are going to do is actually implement the functionality, 0046

which is going to involve writing to files (for example, our items.txt file and our departments.txt file).0050

If we look at (for example) our old version, version 2.0 of the admin site, we had created this addItem form0056

where we could add an item to the store, and select the image file extension, provide a description, a price, a name, and so forth.0063

And then, when we click on Add Item, it is not actually adding it to the store.0083

But it is saying that it would have created a new item in the store with item number 1007.0086

And then, it just outputs the information that would have been stored for that item.0094

Now, one thing to note is that, if you remember from version 2.0, we have a getLastItemAdded function.0099

And what that function is: it reads our file we added in the last web application lesson, called lastAdded.txt.0109

And lastAdded.txt just contains simply the item ID of the last item added to the store.0117

So, when we add a new item to the store, we read from this file; we increment it by 1, set that as the ID of the new item,0124

and then we go ahead and update the file; and we are going to 0131

update the lastAdded.txt file--that is what we are going to be doing in this lesson.0134

This item 1007 is generated by that getLastItemAdded function.0139

We also had an admin page called addItemToDept, where we could add, for example, item 1001 to department 3.0144

And it wasn't actually implementing the functionality; it was just saying, "Item 1001 would have been added to department 3."0155

What we are going to do in this lesson is: we are going to make this functional.0161

We are going to write to our departments.txt file, adding the item number to a particular department's line.0167

In this case, for example, if we go and look at departments.txt, for department 1, we would be adding an item number to it, 1001.0174

So now, 1001 would be part of the third department, which is the Sporting Goods department.0188

Because we are going to be writing to files, one of the things we are going to be doing in this lesson is updating 0200

our three functions that are used to open our items data file, our departments data file, and our lastAdded data file.0206

The way it worked in the last web application: if we look at, for example, openDeptsDataFile, 0215

it opens each file with this r access mode, which is a read-only access mode.0224

Because we are going to be (put that back down) writing to files, we need to have the option of opening the files in non-read-only form.0230

And if you notice, for openItemsDataFile, we had specified r as the access mode, as well.0240

And for openLastAdded file, we specified r, as well.0247

What we are going to do in our new version of the web application is modify these functions slightly,0251

so that they take an optional parameter that is going to allow us to specify another access mode, other than read.0257

Read will be the default access mode, but we can also specify, for example, append mode or overwrite mode.0264

So, if we look at these updated functions of, for example, openDeptsDataFile in version 3.0,0270

we can see that it takes an optional parameter now, called accessMode.0283

And the first thing we do is test: if accessMode hasn't been provided, if it has a default value of null, 0287

then we are just going to open the file in read mode.0294

So, this is going to allow our current calls to openDeptsDataFile to still work,0296

because they are not going to be providing a file access attribute.0301

If one is provided (meaning accessMode is not null), we are going to open the file in that access mode.0308

For example, we can open it in append mode, which would be specifying the lowercase letter a.0313

And we have done that update for openItemsDataFile (you can see here) and openLastAdded file, as well.0318

The default mode, if you call the function without an access mode, is going to be to open the file in read mode.0332

If you provide an access mode, our application is going to open it in whatever access mode you specify.0337

On our addItem.php form (let's go back to...), this is what the form looked like.0349

And what it did in our last application was just: it was sort of a dummy form.0366

It would just output, as we just saw, "Item 1007 would have been added; these are the attributes."0369

Well, now we are going to make it functional and make it actually add an item to the database.0374

What we are going to be doing is creating a function called insertItem.0387

And what that is going to do is: it is going to be passed an associative array of all of the information that is provided0392

for a new item on the addItem.php form, and it is going to go ahead and add that item to the items.txt file.0399

If you remember, our items.txt file--each line is a delimited string that specifies all of the different data fields for a particular item.0408

So, insertItem is going to create a new line in this items.txt file for the new item added to the store.0418

And to do this, we are creating two additional helper functions.0430

One is called createItemDataString, and what that is going to do is take the associative array of information0434

about the new item and put it into the form of that delimited string that our items.txt file requires.0440

And so, this is a function that is going to take in the array of data and output the formatted string.0448

We are also going to have a new function called updateLastItemAdded, because,0453

when we add a new item to the store, we are going to be reading that lastAdded.txt file0458

to find out what the ID of the last item added to the store was.0463

We are going to be incrementing it for the new item.0466

So then, what we need to do is go ahead and update that last added file, so that, when we try to add another item to the store,0468

it is going to have an appropriate, properly incremented number.0474

This updateLastItemAdded function is going to be able to do that.0479

Let's go take a look at the code for version 3.0 for insertItem.0486

And what you can see is: we pass it an associative array of item information.0497

And if we go and look at the new version of addItem.php (that's the old version),0501

what we do in the data processing section at the beginning of the file: if the action of the form is to add item0511

if the information provided for a new item is valid, then we are going to be calling this insertItem function,0518

and we are going to be passing it this associative array newItem,0523

which, as you can see up here, is our POST variable; it is an array that contains all of the information submitted on the form:0528

the item's name, its price, its description, and its image file extension.0535

When we call insertItem, we pass it that associative array; and the first thing that insertItem is going to do0540

is call this getLastItem function, which we had learned about in our last lesson, which reads that lastAdded.txt file0547

to find out the ID of the last item added to the store.0555

We are going to increment it by 1, and we are going to say that that is the ID of the new item.0558

Then, what we are going to do is: we are going to add to this item associative array that was passed into insertItem.0563

We are going to add this new item ID to that, because when we submitted that form, the new item didn't have an ID yet.0570

So, we are going to add that to it.0577

Then, what we are going to do is open that item's data file for writing.0579

And we are going to open it in append mode, because what we are going to be doing 0584

is just adding a new line to that file, at the end of the file, for this new item.0587

And as you can see here, we are making use of our new version of openItemsDataFile.0591

We are passing in an access mode: in this case, we are passing in the letter a, which is to open it in append mode.0597

Then, what we do is: before we write to the file, we need to create that delimited string of all of the item's information.0603

So, we are making use of this helper function I just mentioned, called createItemDataString.0609

And we pass it this associative array of all of the item's information, 0615

which contains everything that was submitted on that POST form,0618

as well as this new item ID that we generated.0621

If we go look at createItemDataString, it does a little bit of checking at the beginning, 0626

just to make sure that there are at least 5 items in this array that you are passing to it, because an item has 5 data fields.0631

And then, all it does is builds up this item data string, starting with the item ID, all the way through the image file extension.0639

And then, it returns the delimited string that it creates.0646

And then, after that, we add a new line to it, because every time we add a new item to the file,0652

we want to put it on a new line, so we end each line with a new line character.0659

And then, we go ahead and simply write that string--the new item's data string--with the appended new line character to the file,0663

and then go ahead and just close that items.txt data file.0671

Now, if it was successfully written (we test to see that it was successfully added to the items.txt file),0677

then what we need to do is go ahead and update our lastAdded.txt file,0684

because now, let's say, the new item in the store is item 1007; if another user tries to go and add a new item to the store,0690

they are going to get that the last item added was 1006; what we need to do is update it to 1007,0696

so when they add a new item to the store, it becomes 1008.0702

And that is where this helper function updateLastItemAdded comes in.0706

We call that function, specifying the ID of the new item.0710

So, we are going to add arrays to the old item ID from lastAdded.txt, and add the new item.0716

And then, we are just going to do a little checking to make sure it worked OK.0721

And then, if it worked out fine, we are going to return from this insertItem the ID generated for this new item.0725

If we go and take a look really quickly at updateLastItemAdded, what it does is:0732

we pass it the new item ID that we want set that lastAdded file to the value of.0737

And so, what we do is open the lastAdded file; in this case, we are passing it the access mode identifier w, which is overwrite mode,0743

which means we are going to open the file in write mode, and we are going to 0752

erase all of the contents and put the file pointer at the beginning.0755

So, when we open that file, we are going to erase the last item ID, which, for our example we have been working with, is 1006.0758

Then, all we do is go ahead and write the new item ID to the file.0765

So now, that file contains the updated ID of the last item added to the store.0770

Perform the little error checking, or make sure that the file was closed OK.0775

And then, if it was successfully updated with the new item ID, we return true; and if not, we return false.0780

And so, that is how the insertItem function works.0787

So now, let's go and take a look at what it looks like in practice.0792

This is the new version, version 3.0, of our form.0795

And the form looks exactly the same, but it is what is going on behind the scenes that is going to happen; it is going to insert a new item.0800

Let's see, if we add another basketball to the store, and then let's say it's going to have a GIF image,0806

and we go ahead and add the item (click Add Item), it is going to say, 0820

"The item was successfully added, and it has been assigned item 1007."0825

What it did was read that lastAdded.txt file, saw that the last item added to the store was 1006,0828

set the new item ID to 1007, and updated that file.0835

So, if we go and look at our data files, PSPAD is going to say, "Hey, these files that you already had open have been changed,0839

so do you want me to refresh it?"--say yes.0848

If we go in items.txt, we can see that a new line has been added (1007).0851

And then, it has all the information that we provided: Basketball #2, $24.99.0856

Additionally, lastAdded.txt has been updated so that, instead of saying 1006, it now says 1007,0860

because this last item we added to the store was, in fact, 1007.0868

Now that this item has been added to the store, if we go back and look at version 3.0 of the store,0876

and if we click on viewCart, which shows all of the items that we currently have in the store,0881

we can see, at the very bottom here, that we have this new item: Basketball #2.0887

Now, you will see that the image is not present, and that is because we didn't upload an image for this file.0891

And we are actually going to get to that in our next web application development lesson.0895

But for right now, we'll just leave the placeholder for that image.0900

And if we click on Basketball #2, it works with the item.php page; it displays all of the information about the item, and so forth.0902

And so, that is how we implement the functionality of adding a new item to the store.0911

And you can see that you can do that as many times as needed.0915

Let's say we add some apparel; now it says that the item was added to the store; it now has item 1008.0919

Again, if we go back and look at our refreshed files, we can see that lastAdded.txt has been updated to 1008.0938

And items.txt has been added, with a new line representing that new item in the store.0945

And when we go to view our cart, we can see that that other item, 1008, has been added to the store, as well.0950

The other form we had created when we did our last version of the admin application was: 0964

we created a form called addItemToDept.php, which as we saw earlier, is basically:0969

you enter the ID of an item you want to add to a particular department, 0974

and then you enter the department ID of the department you want to add it to, and it will go ahead and add that.0978

On our last form, it was just sort of a mock form that didn't really have any behind-the-scenes action.0983

Well, now we are actually adding that functionality.0988

We are creating a function called addItemToDept, which is going to add that item to the department.0990

Now, what that means is that it is going to make an update to the departments.txt file.0996

And so, for example, as mentioned before, if we wanted to add item 1001 to department 3,1003

the way that would look in our departments.txt file would be to update this last data field,1010

which is a comma-delimited list of all of the items in the particular department.1016

And so, our addItemToDept function is going to do this.1022

And if we look at addItemToDept, in the processing section of the form at the beginning, we just had a placeholder here.1030

We tested that, when a person submits an item ID and a department ID, we just checked to make sure that they both existed.1039

And then, we would output a message saying, "OK, this item would have been added to the department."1046

Well, now we are going to fill in this placeholder.1051

In version 3, what we are going to do is: we are going to be calling this addItemToDept function.1053

And that is going to take two arguments: one is the item ID of the item we want to add to the department,1057

and one is the department ID of the department we are adding it to.1063

We checked to make sure that it was added OK, and then at the bottom, 1066

we simply are outputting a message that says it was successfully added to the department.1070

In using this addItemToDept function, we are creating two additional helper functions.1076

One is createDeptDataString, which is just like what we had seen when we added a new item to the store, where we had createItemDataString.1082

We are going to have a function that is going to take an array of information about a particular department1090

and create that delimited string that follows the format in departments.txt.1095

And here, it is a delimited string with three different fields, where the third field 1101

is a comma-separated list of all of the item ID's for the items in the department.1106

And so, that is the createDeptDataString function.1118

The other thing that we are going to do is create another helper function 1121

that is going to go through the process of making that change to our departments.txt file, 1125

where it is going to add, for example, 1001 to that comma-delimited string for department #3.1131

So, that is what the updateDepartment does.1138

And one thing to note is that this updateDepartment is going to make use of a file function, which is provided in PHP.1140

What file does is: you provide it the name of a file, for example, and what it does is reads every line in that file1149

and creates an indexed array that contains each line of that file.1168

For example, if we used file to read departments.txt, file would return an array that contains three elements.1174

Each element would be one line in the file, so the first element would be this string here; the second element would be this string here.1183

And what that is going to allow us to do is do some processing on that departments.txt file.1191

Let's take a look at our addItemToDept function.1199

What it is going to do is: when we specify the item ID and the department ID of the item that we are adding1205

and the department that we are adding it to, we are going to go ahead and load the information for the particular department.1214

And we are going to use the getDepartment function for that.1220

And the reason we are doing that is that we are going to test to see 1223

if the item ID that the user has supplied--if that item is already in that department.1226

What we would do is extract the items array from the department that is part of this department associative array returned by getDepartment.1231

And that items array, again, is an indexed array of all the item ID's in the department.1242

We loop through it, and if the item ID we are trying to add to it already exists, 1249

we are just going to return true, because the item is already in the department.1252

If the item is not already in the department, then we are going to add that item ID to the departments array of item ID's, which has this key items.1256

And then, what we are going to do is pass this associative array that contains all of the department's information--1269

it contains its ID, its name, and that indexed array listing all of the items that are in the department--1273

in this case, it is going to include the ID of the new item we are adding to it--1281

we are going to pass that to the updateDepartment function.1285

And then, if the update works, we are going to return true; and if not, we are going to return false.1288

The action of writing to the file is going to occur in this updateDepartment function.1297

And if we go and look at that, we can see that the first thing we do is use this file function that we talked about and click on it in the documentation.1301

It will take you to the PHP documentation that says that it reads the entire file's contents into an array, as we had mentioned.1311

And so, what we are going to do is create an array called deptLines,1322

which is just going to be an indexed array of all the lines in departments.txt.1325

And then, what we are going to do is search through this array that we have of all of the different departments1331

in our store, until we find the line that contains the department we are trying to add it to.1341

What we do is loop over this array, and then we use the explode function to separate1347

all of the information that is contained in each line of departments.txt.1356

We are going to separate on this departments data separator, which is the vertical bar,1361

and we are going to extract the first element of the array that that explode function is going to return,1366

which is going to be the ID of the department.1371

And then, what we are going to do is: if the department ID of the department we pass to this function that we are trying to update1374

equals the ID of the particular line that we are looping over in the file,1386

then we are going to set this variable currDeptIndex equal to that file and break out of the loop.1392

What that is doing is: we are searching through the departments.txt file 1397

to find the line number that contains the information about the item we are looking for.1401

Then, what we are going to do is create a string that is an updated version of that department,1408

which is going to contain the new item ID of the item that has been added to the department.1414

And then, what we are going to do is go ahead and rewrite that whole departments file,1419

looping through all of the department lines that we read from it originally, as well as this updated line.1423

What we are saying here is: we are adding to the deptLines array, which contains all of the lines that we had read in from the departments.txt file.1430

And at the position where we found the department that we are looking for, we are going to add this department data string, followed by a new line.1439

And createDeptDataString works just like our createItemDataString.1449

What it does is creates that delimited line for a department, which contains the department ID, the name,1456

and then all of the items in the store, separated by vertical bars.1465

And then, it contains all of the items in the department in this third data field, separated by a comma.1468

And so, you can see here--the first thing it does is creates this blank department string.1477

We add the department ID to it, along with the vertical bar delimiter.1481

We add the name and the vertical bar delimiter, and then what we do is loop over that indexed array items1485

that is part of the department that contains all of those ID's, including the ID of the item we are trying to add to the department.1491

And then, we create that comma-delimited list, and then add that to this department string.1497

And then, we return the department string.1503

So, now that we have this new department string that has updated lists of items in it,1506

as mentioned, we are just going to open our departments data file.1513

And we are going to open it up in overwrite mode, which means we are going to erase all the contents of it.1517

And then, we are going to loop over all of the lines that were previously read from the departments.txt file,1522

as well as the new line that has the updated department string.1529

And then, we are just going to write them out to the file; do some error checking and make sure that the file has closed OK;1536

and just see that there were not any errors.1542

And so, it is kind of a little tedious (what we have to do), in that, in order to add this item to the file,1545

we have to read all of the items out, search for the line that the current department we want to alter is...1552

We have to change that, and then write all of those lines back.1559

And that is one way of doing it; and there are some more complicated ways, as well.1562

But as you can see, it is kind of a tedious process, and as we progress in the course, 1565

when we change our back end for this system from these flat data files to our database, it is going to make that a lot easier.1569

We are not going to be having to do all this tedious work, because the database is going to do it for us.1577

If we go and look at the new version of the web application in action, if we go to addItemToDept...1584

For example, we just added 1008; and if we go look at the store--here is item 1008, men's pants.1594

They show up in the viewCart, but let's say we want to add them to the Apparel department.1603

Well, currently, when we click on the Apparel department, it doesn't list this new item.1608

So, what we are going to do is use our new implemented functionality for adding an item to a department 1612

to go ahead and add that to our Apparel department, which is department number 1.1618

So, the pants item that we added was 1008; the department ID that we are adding it to (the Apparel department) is department 1.1623

When we click Add to Department, it is going to say it was successfully added to the department.1632

Now, if we go ahead and refresh our Apparel page, we can see that Pants now shows up in the department page.1635

Additionally, if we look over at departments.txt, it has been updated,1644

and you can see that this Apparel department line in our departments.txt file has had this 1008 item ID added to it.1649

And so, that is what happened when we ran that updated department function.1659

It went through and updated the line for this particular department.1664

And so, that is how we add a new department to the store.1668

So now, we have, in our admin website, a functionality implemented for addItem, as well as adding an item to a department.1670

And what we are going to do in our next web application development lesson is improve the addItem form,1680

so that we can actually upload an image, as well.1686

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