3.4 Add the Dream Display Logic
Next, we need to fill in a little PHP logic that we can use to actually display the data that will come back from our dreams file. We will be using basic constructs like conditionals, loops, arrays, and a cool function that pulls apart strings into arrays, named
1.Introduction2 lessons, 07:04
2.Getting Started3 lessons, 13:53
3.Working With Files6 lessons, 31:06
4.Conclusion1 lesson, 01:33
3.4 Add the Dream Display Logic
Let's talk a little bit about how we want to display this data. So we did kind of mention a little bit about creating a definition list and having some PHP in here and calling our function, but let's kind of design how we want this to work. So really the easiest thing for us to be able to do at this point, would be able to call our function and get back an array of combinations of our name and our dreams. So I would really want for each line that shows up in my file, I wanna get that number of lines back in an array, so that I could just kind of loop through all of them and display them here to the end users. So without actually writing the read_dreams yet, let's go ahead and see what something like that might look like. So what I would like to do is get the contents of the file, so I'd like to say file contents, and read_dreams. So now I have an array of the combination of the name and the dream in file contents. And now I wanna be able to display that. So the first thing that we're gonna do is we have to always remember to write code defensively. So what happens if there's no file or it's empty and we just get back something like an empty string. So let's go ahead and check for that first. We'll say if the file contents is not equal to the empty string which means there's something actually there, then we wanna to look through some stuff. So we'll say for each file contents as you can call this anything you want. We'll call it maybe line for each line. Then we're gonna want to get that data and split it apart. Remember we created this little demarker in the middle here of the double colon. So now we know how to split that. So let's go ahead and say pieces or you can call this anything that you want. Pieces is gonna be equal to. We're gonna use the explode function which takes a string and you say what you want is use as your delimiter. In our case, it's gonna be that double colon and what you wanna pass in which is gonna to be our line. And what's gonna happen is explode is gonna return an array of all of the individual pieces that are broken up if we were to remove this delimiter. So now I have two pieces in here, we can make that assumption. You could obviously write some code in here to double check that, but we're gonna make the assumption that that works properly. And then we'll do a couple echo lines. We'll say echo. We'll say the definition term is going to be the first part of that pieces at the index zero. And then we'll create the closing tag. And then we'll need to do the actual definition. And that's gonna be the second item that was in the pieces array right there as well. And we'll close that out, definition, there we go. And we'll go ahead and save it, now what happens if there's nothing there though? That's the other thing we wanna be able to take care of. And actually right now because we don't have any logic yet, we're gonna work on that in the next lesson. Let's just go ahead and return the empty string for now. But what happens if it is the empty string? Well, let's put a little else block in here, and we'll simply echo an h3 here, and we'll say, no dreams yet. So that at least we can see if we got something or if there's nothing there or whatever. So now let's go ahead to save that. So as you can see, we wanna get the dreams, we want to see if there's anything in there, if there is something in there we wanna loop through them, we want to explode each line on our delimiter. And then echo out the term and the definition being the person's name and their dream and we'll just loop through all of those. But if there's nothing there before we get the empty string which is all we're doing right now, then we just want to say there's no dreams yet. All right, so let's go back over to our page here and go ahead and refresh. And as you can see we have no dreams yet. So now we're to the point where we're ready to start reading some data from the file, and just like anything else, there's several different ways you can do that. So over the next couple of lessons, I'm going to show you the basic fundamental operations that go into reading a file using PHP and then once again we'll use a couple shortcuts that are there but only after we fully understand what is a shortcut for.