Viewers: in countries Watching now:
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
The public side of our content management system is really coming along. In this movie, I want us to take a look at the right side of the content, that is, where the paragraphs of text, the main content of the website is going to exist. And see if we can make some improvements there. Currently if we go to index.php with no page or subject selected, we get, please select a subject or page. Not particularly user friendly. And then if I click on any one of these pages, the content here doesn't look that great. So let's see if we can make some improvements. I'll go to index.php and instead of please select a subject or page this is the default text. So this could be anything we wanted.
When you first come to the website this is what you see. It's essentially the home page. So we could put anything we want there, I'm just going to put a placeholder to say welcome. This could be an image or a slideshow or several paragraphs of text welcoming people to the site but that's what's going to go here. Now when we actually display the content that's what takes place in this block. If we have a current page then show the current page. Instead of just showing the content, lets also just put the top inside h2 tags, I'm going to put the menu name again. Let's see what that looks like.
So we come over here, index.php, welcome. And then about, now we get our mission and it says our mission over here. Our history and it shows our history. I think that that's a little nicer. Now let's come back over here and I want you to look at the content itself. Now at the moment we're calling HTML entities on it. But we don't have to. Let's talk about a couple of the options that we have there. You remember when we talked about escaping for HTML that we have this functions called htmlspecialchars and it's what renders the content safe for display in the HTML. You definitely need to call this method if you want to make sure that the content is not going to break your HTML.
However, it is optional. If you wanted to have HTML, if you wanted to allow HTML to be in that content block then you couldn't call this, right? This is making sure that there is no HTML in there. It renders it harmless. But if we wanted HTML, then it would be up to our admins to make sure that it wasn't going to break the site. PHP wouldn't be taking care of it for us anymore. It would now be a manual process. There's some risks involved with that but we could do that. We also have HTML entities.
And that's what we're using right now. It does everything that htmlspecialchars does but it goes one step further. And it also encodes things like accident characters, currency symbols, anything that can be turned into an html entity will be. But it offers the same safety features that htmlspecialchars does. Another possibility is not to just render them harmless but to remove them altogether. And we have strip tags, is a function in php that will go through looking for html tags and it'll just remove them. It doesn't render them harmless, they don't suddenly still show up in the text, whereas htmlspecialchars will take something like a div tag and it will then display the div tag.
Strip tags will just remove it, you won't see it at all. Another extremely useful function is nl2br. Right now, let's imagine that we don't allow html in our content area. If a user is typing in there, they might hit line return and expect those line returns to stay when we actually display the content. However, HTML doesn't care about line returns. It's white space independent. In order to have a line return in HTML, you need a BR tag. Well that's where this function comes in.
Nl2br converts new lines, that's the nl into br tags. So it converts new lines into br tags so that they're preserved, the way that the user might expect. Let's use that one so you can see how it works. So here where I'm displaying the content, what I want to do is put nl2br. And I want to do it outside of html entities. And think about why that is, html entities takes anything that was html and renders it harmless basically makes it stop being html.
And what we're doing here is generating HTML, so we don't want to generate the HTML and then have it rendered impotent immediately afterwards. Instead we want to render anything impotent that's in the content and then take the new lines and only have br tags. That's the only thing we're going to end up with out of it that's valid HTML. So let's save that. We don't have anything yet that has new line returns, at least I don't. So let's go to our history. Let's edit. Founded in 1898 by two interprising engineers.
Line return. Line return. And then I'll say more recently dot dot dot edit page. Now notice here, you can see what happens when we don't have an L2br. Right. It doesn't preserve the line returns because HTML doesn't care about it. I click edit page. You'll see that they're still there. They exist in the data, but HTML is not respecting them. We come over to the public side, so here I am on the public side of things again about Widget corp, our history, you'll see that nl2br now does preserve them. If I view source on that you can actually take a look and see here's the br tags, alright.
So all those new lines got turned into br tags, and that's often the behavior that users expect. When they're given a text area like that. They expect those line returns to be preserved in the HTML. At this point, we're almost done with the public side of our CMS. There's one last thing that I want us to consider and that is the visibility of the pages that we allow people to see over here. Right now we have visibility accounted for in our navigation. But we aren't checking to make sure that things are visible here on the content side before we actually display them.
Find answers to the most frequently asked questions about PHP with MySQL Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
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.