Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Throughout this chapter we've been making improvements to our navigation. And our navigation code has grown as we've added more features to it. By now it's become a pretty large block of code on our PHP page. To me, this is a signal that it might be time to do another round of refactoring. Now there's two ways that we could go with this refactor. First, we could move all of our navigation to a separate PHP page. Maybe something called navigation.php. And then we could use include to include that PHP page in whatever page we wanted to have our navigation displayed in.
Or, the other choice is, we could move all of our navigation to a function, and then when we call that function it would generate the HTML that we need for the navigation. Either one of these approaches would work. It's really just a matter of which coding style you prefer. Would you rather work in HTML most of the time with some PHP sprinkled in, or would you rather work in PHP most of the time with some HTML sprinkled in? And I think that the first one, using an include page, I think you can probably just do that on your own. It's pretty easy, just cut and paste that over to another page and use an include statement.
I'm going to show us how to do it by using a function. Because I think we can learn a little bit more by doing it that way. So, the page that has our navigation now is managecontent.php. We're going to open that up and I'm also going to go ahead and open functions.php because that's where were going to put our navigation function. And TextMate lets me just fold up all of these just by clicking right here on the side bar, and then I can make a new function which will be called navigation. Makes sense right this is where I'm going to put all of my navigation code. So, let's find the navigation over here. What I'm going to do is grab everything from this ul the top all the way down to the ul at the bottom.
That's what I'm going to cut, so I'm just going to cut that out now my navigation is just this div that's left. And I'm going to come over here and the first thing I'm going to do is I'm just going to paste this in, right in the middle. Now that's not going to work because I'm inside PHP here, this is all PHP code, and I just dropped in some HTML. So, I've gotta turn all of this HTML into strings in PHP, because I made the choice to work mostly in PHP with some HTML sprinkled in. So, the way that we're going to do that is we're going to just put double quotes around all of our strings, and then we're going to put output as a string. Then we're going to build up with all of the HTML in it. So, output is going to start out being equal to this. I have double quotes inside double quotes, so I need to escape those with a backslash, that tells it that this is just a double quote inside the string not an ending double quote.
So, I've done it now to the first one and even now I'll go through the rest of them. And I need to strip out the PHP, I have a PHP tag I don't need to turn that on because I am in PHP right? So, that will get a little bit cleaner same here. And then every time I have an echo statement instead what I need to have is output and then append equals. And that's going to then add that to the string that I've already started up here. Every time that I was echoing something before, from PHP, now I'm going to build it up in this output string. Now we could just do echo from the function, but I think that's a bad idea. I think it's better to have functions return something, and then we have the option to echo it once we work with it there.
Now, some people disagree, some people like to have functions that go ahead and just output from the function. But I think it's a better idea to build up this string instead. So, let's go through, and every time we see an echo, let's just go down the list we're just going to drop in our new output equals. We're not even going to really worry about the code yet. We're just going to indiscriminately go through all those echo statements. Okay. And we got them all. So now, let's come back and do a little more cleanup. So I've got my loop started. I've got an output. Then I've got my conditional statement here. That's all PHP, so that's okay. Now I don't need my PHP tag anymore.
Now I've got another bit of HTML so once again I need to figure out where the HTML part is. That's right here put double quotes around it and then put my output at the beginning. Once again I've got a double quote inside a double quote so I need to escape that. Lets make sure we have a semicolon at the end. And were just going to repeat this process all the way down the page. Just building up the string in this variable called output. So, urlencode, that's fine. We don't need our PHP tag here. Now these two characters our HTML. Don't be confused by that.
I also have a double quote in there that needs escaping. And then,we've got more PHP. See how this works? Do you follow the process? Once again, I've got some more HTML here. So, we're going to just do output equals and then we're back to PHP. Page set equals, and then we've got some more HTML one more time. Same thing put it in double quotes. Escape any double quotes that are inside of it. And then output append equals to the front of it. Okay now we've got our pages actually I'll put a little break here just so that's clear that's where the pages start.
And again I don't need my PHP tags let's just bring all of this back over a bit. That's all just PHP so its already okay and good to go. Here though I've got some more HTML. And let's come to here, and it goes all the way to there. Put double quotes around it, and the semicolon at the end. Escape, the double quote that's inside of it. And output, append equals in front of it. So, once again, building up another line in my string. So, then we'll remove that. Once again, these two characters, don't be confused.
Those are HTML. That's the ending that I need. Dollar sign, output and go, once again, a little more HTML here. I actually can bring that up and put it in the same HTML block, that's fine. Output, and, and I don't need the PHP tag, but I do need this closing to my loop, and then, I can free up the result. Alright, and I've got a little more HTML here. I'm going to put quotes around the whole thing, semicolon at the end. Do check and make sure that you've got semicolons at the end of all these lines.
It's real easy when we're doing lots of changes like this, to lose track of those. And then we've got MySQL free result, just indent these correctly. Subject set, don't need that anymore. And my last bit of HTML. Okay, so hopefully you followed along. You can go back and double check yourself you want to make sure you have semicolons at the end of all the lines. Make sure that any time that you have a double quote inside a double quote that you've escaped it. Make sure that you're always using append equals, to add things to output. So, now we should have something that will assemble a string and assign all the parts to it to output.
The last step is that we need to return that as the result of our function. The very last step is return output. Okay, so that's good, we've got that but we do need to make another change here which is our navigation needs a little more information. It needs to know what the currently selected subject idea is. That's right here. So, we need to look at that. All of these parts are part of the loop. But selected subject id and selected page id are variables that are in our global scope. So, we could call global on them and bring them in. However, I think that it's actually better in this case not to do that.
But to take these and put them up there as arguments to our navigation function. I'm actually not going to call it selective subject_id I'm just going to call it subject_id and page_id. And that will be the name of the local variable we will use inside our function. I will do that for both of those. So, now I just need to pass in those two values and it will be able to do the rest. That's all that it needs to know. It needs to know, what is the currently selected subject, if any? What is the currently selected page if any? Let's make a code note here. Navigation takes two arguments.
The currently selected subject id, if any and the second one, the currently selected page id, if any. Okay, so that'll tell me what I need to be passing in. So now I just need to call it. So let's take this and copy it. Come back over here and inside our navigation, we'll open up some PHP tags. And we'll echo, make sure you echo, our navigation and we're going to pass it in selected_subject_id and selected_page_id. Those two values are going to be passed in, so selected_subject_id becomes subject_id as a local variable once it gets inside the navigation.
Don't let that throw you. Alright, so let's save both of those, and let's try it out. Let's go to Firefox and let's see if we have any bugs in our code. Nope, it reloaded for me just fine. Now, if there's no change, everything is the same, but that's exactly what we wanted. We refactored it. Remember, refactoring the result should be that we have the exact same behavior, we just have better code behind it. Is this better code? I think it is, because now we have this one function called navigation which takes care of our navigation for us and look at our page now. Look how much cleaner this is. We have our code here at the top that loads everything, that figures out what subject and page we're on. But when we actually get down to the main content, that's it. Just one line.
Navigation. What are we doing here? Well, we're echoing the navigation. It's very clear and concise. It makes the code more manageable, more maintainable, easier to understand, and more flexible. And those are our goals when we're doing a refactor. So, we've done a lot of work on our navigation here, but I think we've finally got something that really works well for us as this navigation. And we're able to select content and now the next step is for us to start working with those selection over here on the right side, in that manage content area. What we need there is the ability to create, read, update, and delete our subjects and our pages.
And we'll learn how to do that in the next chapter.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92459 Viewers
82 Video lessons · 104136 Viewers
71 Video lessons · 75591 Viewers
56 Video lessons · 106967 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.