When people want to build a website for someone, they usually need to create a
Content Management System (CMS) that allows the client to manage their own
content. And while there are thousands of prebuilt CMSes that can get the job
done, it can be very helpful to learn how to build one yourself.
If your grandmother wants a website to sell pies, then she only needs a way to
add, update and remove various flavors.
management systems. The first uses a separate area for all administrative
controls. This is my personal choice because it allows the admin area to scale
and provide more features.
The second is what I would call an inline approach, where all backend controls
are done inside the same frontend website. Implementing this approach it
helps keep your code DRY (don't repeat yourself) by keeping you from writing
the code to display pages and content twice.
Each of of these methods have their benefits and detriments. By separating the
administrative controls you have to rewrite most of the code to display the
content again. But, when using an inline approach it's difficult to add a page
to statistically show popular products (or pies). As a result, every website will need a
system tailored to improve the usability for the client.
Therefore we only need three pages.
more files later on, but this should be plenty to get you started.
an easy way to manage our favorite websites.
This table will hold all the information we need about our favorite website.
Where to access it (the url field), what the title is, and who is the primary
author on the website.
This article is not designed to teach you SQL, so you may need to copy and
paste your way through for the time being. W3 Schools has some SQL tutorials
that you may find helpful.
but I think we'll find PHP Data Objects the easiest to use.
information. The code below will go ahead an insert two rows into our table with
some websites that I like.
need to make this website functional.
This is all we need for our website class
adding and editing website details.
There are two important things to note inside of our form.
Notice how we check to see if a form has been submitted right at the beginning.
This let's us choose whether we want to try and insert a new row or display
the form.
Both sides of the
This is cool because it will automatically fill in the inputs if the value has
been added to the object.
This will create a link to
This requires a little more logic than when inserting a new record because we
have to find a current record and then update it.
Most of what I've done in this file is the same as the last few examples,
perhaps the most complex part is at
basically it's concatenating the two arrays together resulting in
There really isn't anything really special in this part. Once you've added a
page for inserting and updating, deleting is as easy is falling off a log.
to practice, practice, practice. Once you have a basic understanding of how to
write/run different queries based on what needs to be done you'll be good to go.
For practice I recommend creating a few basic websites to challenge yourself.
A blog that has posts, comments and categories would be a great start.
Content Management System (CMS) that allows the client to manage their own
content. And while there are thousands of prebuilt CMSes that can get the job
done, it can be very helpful to learn how to build one yourself.
What a CMS should do
They don't have to be extremely difficult and should only do as much as needed.If your grandmother wants a website to sell pies, then she only needs a way to
add, update and remove various flavors.
How a CMS should work
There are two popular methods (that I'm aware of) for creating contentmanagement systems. The first uses a separate area for all administrative
controls. This is my personal choice because it allows the admin area to scale
and provide more features.
The second is what I would call an inline approach, where all backend controls
are done inside the same frontend website. Implementing this approach it
helps keep your code DRY (don't repeat yourself) by keeping you from writing
the code to display pages and content twice.
Each of of these methods have their benefits and detriments. By separating the
administrative controls you have to rewrite most of the code to display the
content again. But, when using an inline approach it's difficult to add a page
to statistically show popular products (or pies). As a result, every website will need a
system tailored to improve the usability for the client.
Getting Started
The CMS we will be creating will be more of an inline approach. It will be a very basic site that allows me to store a list of my favorite websites.Therefore we only need three pages.
- A home page that shows all of my favorite websites.
- A form to that lets us create and edit websites.
- A page that deletes a website from our list.
more files later on, but this should be plenty to get you started.
|
Creating our Database
We won't be creating a large database for this tutorial. We only want to providean easy way to manage our favorite websites.
|
Where to access it (the url field), what the title is, and who is the primary
author on the website.
This article is not designed to teach you SQL, so you may need to copy and
paste your way through for the time being. W3 Schools has some SQL tutorials
that you may find helpful.
Connecting to the Database with PDO
PHP offers multiple interfaces for connecting and interacting with databases,but I think we'll find PHP Data Objects the easiest to use.
|
Add Some Data
To make it easier for us begin working with our database we need someinformation. The code below will go ahead an insert two rows into our table with
some websites that I like.
|
|
Displaying our Favorite Websites
Now thatindex.php
has been cleared we can go ahead and add the code that weneed to make this website functional.
|
|
Creating the Form
We are going to create a single file for the form because it will be used whenadding and editing website details.
|
|
- Each input is set into a
website[]
array
|
This let's us choose whether we want to try and insert a new row or display
the form.
Both sides of the
if
statement create the $website
variable for the form.This is cool because it will automatically fill in the inputs if the value has
been added to the object.
Editing a Website
The first thing we need to do is add a link toedit.php
|
edit.php?id=xxx
which we will be able to grab with$_GET['id']
.
|
have to find a current record and then update it.
Most of what I've done in this file is the same as the last few examples,
perhaps the most complex part is at
|
|
Deleting Records
The first thing we need to do is add a link todelete.php
in index.php
|
|
page for inserting and updating, deleting is as easy is falling off a log.
Conclusion
The hardest part of creating a CMS is learning the SQL, which means you'll haveto practice, practice, practice. Once you have a basic understanding of how to
write/run different queries based on what needs to be done you'll be good to go.
For practice I recommend creating a few basic websites to challenge yourself.
A blog that has posts, comments and categories would be a great start.
No comments:
Post a Comment