- October 16th, 2021
- Code
- By Steve Shead
First Post - how I built this website as a database driven site with PHP & MySQL
Updated on January 13th, 2022 by Steve Shead- October 16th, 2021
- Code
- By Steve Shead
- 0
- 0
First Post - how I built this website as a database driven site with PHP & MySQL
Hello! Here is the first post after pushing this site live. A lot of work has gone into this - not just the front end, but making this a database driven website with a backend admin panel dashboard - basically a lightweight content management system.
I started out thinking it would be a mainly static website, but then I also wanted to be able to post articles and didn't want to go the Wordpress route. I have nothing against Wordpress, just didn't think I would need that amount of power for what I wanted to achieve here. Fast forward a month or two, and here we are. So, what happened?
As I worked through the process of putting this together I knew I wanted the basic ability to post articles. I could have just added static posts but that felt messy - almost felt like cheating. I could have gone a number of different routes but decided that building a really simple CMS would work, would be fun to try and would help me as I learn more about PHP and MySQL.
Folks keep asking me why I wanted to build a CMS when there are so many free and open source alternatives out there. The simple answer is I wanted to test my coding skills, and learn. I was getting tired of doing online courses and was actually getting stuck in the loop of doing one course after another. I needed practical experience. What better way to do that than to build 'something' from scratch. Along came the concept of the backend of this website.
There is so much to unpack about learning along the way, not the least of which is understand the value of creating the process flows for the different functions like logging in, registering, adding / editing / deleting (CRUD) any of the functionality I was intent on building. I also had to pay attention to the database design, and that was new to me. Note also whilst a lot of this is hand written, I've used snippets I've found around the web, and articles I've sourced that show how to build things like the commenting system. That being said, I take the code and enhance it to what I need it to do. A good example is the commenting system. It was built for static HTML pages. I had to work out how to get it to work using the id's of a post, as well as altering the code to lock in the username so folks can only comment as themselves.
Though initially the intent was a light weight CMS of sorts, as I went through the process I wanted to add more functionality - how about categories? Or how about comments? Again, I could have used plugins and open source alternatives but I was intent on learning as I went along - trial by fire of sorts. The more I got into it the more I was enjoying it. When my code failed I would spend hours working out why, and the jubilation I felt when I solved a complex issue surprised me. Also the fact that there was no way I was giving up felt empowering.
So where did I end up? You can see the front end of the website - what you don't see is the backend, and all the functionality built in. Turns out this is not a lightweight CMS anymore. Here's what it looks like.
* Note the dashboard above is from the dev environment.
Here's some of the functionality - high level.
Configuration: most of the configuration details are in a database table called configuration. This includes site name, site description, site root, date format, database name, database user, and a fair amount more. If you change the details it writes back to the database, and the changes made are retrieved with functions. I know that sounds odd, but I'm struggling to find the right words to describe it, and documenting like this is not my forte!
Registration: this area controls variables for the user registration process. You can disable registration, require email activation (or not), all the way through to setting password length and complexity etc. Similarly to configurations, these are called through functions.
Site Settings: this area is where you set session timeouts, cookie expiry, cookie path and more.
Posts: you can add, edit and delete posts. I've used the free version of TinyMCE to allow formatting of posts. I haven't added moderation of posts since the use case is for me right now, but I might add that, and the ability to have draft posts in the future.
Categories: similar to posts you can add / edit / delete categories. I put the 'slug' and 'description' fields in there in case there is a use for it in the future.
Users: there's a lot in this one. You can add / edit / delete users. There's functionality to have different user access levels. There is one Super Admin who has admin over all areas of the backend, then there are admins that you can promote from registered users, who can admin the basics. You can change what they can and cannot do by adjusting the level of admin access on a page. for instance if the user admin level is less than 9 (arbitrary number), redirect to the home page.
if ($user_details['userlevel'] < 3) {
header("Location: /");
}
Likes: this was a fun one that took some time to work out. Logged in users can 'like' a post - once clicked the like link will change to a heart. Since it's registered to the user in the database they can only 'like' the post once. If they click again it will unlike the post. There were some teething problems with this that I had to solve - that was fun.
Comments: Registered users can comment on posts, as well as up vote or down vote comments. Replies to comments are nested, and comments are tied to the logged in user that posts them. That is to say you can't change the username to post the comment as someone else. There is also the ability to filter words. For instance you can set up a filter that changes the word 'yeah' to 'yes'.
Alerts: if a user signs up or someone comments on a post an 'alert' will automatically be created, and will show in the admin panel on the left sidebar, as well as the top bar. These are generated by the system only. The Super Admin will also receive an email alert for both circumstances also. There's more I want to do with automated alerts, time permitting.
Notifications: this is meant to be a tool any backend admin can use to let other admins know if something is going on. Admins can create alerts, informational or comment notifications. These will also appear in the left sidebar and the top bar under the notifications menu.
Profile: for users that are admins, they can update their profile from the admin dashboard - those that are not admins but have user accounts can update their profile from the front end. It's pulling the data from the same place. I could have just left it as frontend, but you never know what I might use the dashboard for in the future.
There are a few other system functions that I've tied into the admin panel. Each one presented a challenge that I enjoyed solving. That being said, there's a few more things I want to do, so it will evolve over time.
Last comment is, this was with a mix of OOP and procedural PHP, so there is some clean up to do. As I continue to build I'll need to start moving towards OOP - that will take a while. The code isn't too bad though, at least I don't think it is.
Wow - that's a long post and it's only scratching the surface of what was built. I'll stop here for now. Take a look around and let me know if you find any issues. Feel free to send over any ideas or suggestions to make it better, or anything else you'd like to let me know. I'm learning, and all input is gratefully received.
Happy coding!
Cheers
~Steve
Delete Post?
Are you sure you want to delete this post? This action cannot be undone!
Husband, father, grandfather, C Suite leader, engineer, designer, photographer, videographer, musician, composer, pilot, geek, daring to be different - yeah, busy!
All author posts