Join Kevin Skoglund for an in-depth discussion in this video Assignment: Pages CRUD, part of PHP with MySQL Essential Training.
- View Offline
Throughout this chapter, I've walked you through the steps necessary to create all the CRUD you need to manage the subjects in our CMS. Now, we need to do the same things for the pages. They're going to work almost exactly the same way that the subjects did. And I'd like you to try and do it on your own. This is an opportunity to apply all that you've learned. And you can go back and review movies, to help you if you get stuck. Then in the next movie, I'll show you my code so you can compare. But don't just skip to the answer. Please do take some time and try to do it for yourself. It's the best way for you to learn it.
Let's go over what you're going to need to do. So for the Pages, you're going to want to add some links to manage_content.php that will let you create a new page on a subject. Or if you're looking at a page to be able to edit the page. You're going to need new PHP pages for new page, edit page and delete page. Now it's up to you whether you want to try writing those completely from scratch, or you can copy over the ones that we used for new subject. And then go through and make the edits necessary to make it work for pages.
You can mostly do a find and replace on those, but not 100%, there are going to be a few more changes they're going to need to make. In addition to that now that the context has changed. You'll remember also when we were working with subjects, for the new subject we actually had two pages, we had a new page and create page. So you'll have to decide, are you going to do two page submission, single page submission or some combination of those. In my solution, I'm going to be going with single page submission for all of them. There are a couple of pitfalls that I want you to watch out for.
The first is just remember you're going to have pages.content to work with now. That's going to be a new column from the database that you're going to need to display. You're going to need to validate it, and you're going to need to escape it before you insert it or update it in the database. Whenever you want to edit that on the form, you can just use an HTML textarea field to do that. That'll give you a nice, big area for someone to put in lots of text. The next thing to watch out for is subject ID. When we were working with subjects, there was no parent above it. But pages have a parent ID So when we're creating new pages, we need to know what subject they belong to.
You want to send the subject ID with that request to create a new page. And then you're going to want to make sure that you save that in the database as well, so that we have the relationship between the subject and the page. For existing pages, you can look it up. You don't have to actually send the value in the URL, you can just look it up in the database if you know what page you're working with. The next thing to watch out for is page position. With subjects, we were just looking at the position for all subjects. But with pages, we only want to count pages with the same parent subject.
So there's a lot of different pages that have the position of one, but only one page in each subject has the position of one. So you'll just want to be careful about that. Let me show you my finished result. So on managecontent.php I added an area on the subjects that actually just lists the different pages of the subject. But most importantly do that so that I can have a place for Adding a new page to this subject. So that's my place to create a new page. If I click on that, you'll see I get my Create Page form, has Menu name, Position Visible, and then a block for content. Let's create a simple one here, I'll call it Delete me and its position is going to be 3.
Notice that it's 3, right, it's position number 3 because there are two above it. It doesn't include all these other pages that are there. Visible, Yes, and say this is a test. And then I'll click Create page, Page created. And you can see here it is, Delete me is right here. I can click on that, I can Edit Page. This is a test.. and I'll click Edit page. You'll see that it updated it. And then last of all, we can delete it, and I did that. I said that there's an Edit Page button here on the, this is manage content on php again, edit page, and then delete page actually shows up down here.
Now it's up to you, you could put delete on the previous page if you wanted to. But I'll put delete page, and it says Are you sure? Okay, and now the page is deleted. So those are the basic parts of the CRUD you want to create. You're going to make sure that you have the new page. That you can edit the page. That you can delete the page. And just that you make some small changes to where we read it. So that you have the ability to get to links like Edit Page and Add a New Page to the subject. I'm confident that you're going to be up to this task. So roll up your sleeves and get started. And once you're finally ready, in the next movie I'll show you the code that I wrote.
- What is PHP?
- Installing and configuring PHP and MySQL
- Exploring data types
- Controlling code with logical expressions and loops
- Using PHP's built-in functions
- Writing custom functions
- Building dynamic webpages
- Working with forms and form data
- Using cookies and sessions to store data
- Connecting to MySQL with PHP
- Creating and editing database records
- Building a content management system
- Adding user authentication