Give Me A Blog Database

Now we have a great looking blog and all our blogs are stored safely in a database. But all of the blogs on the website come from static files. The database won't be any good for us if we can't publish the blogs in the database.

I need some kind of function to get the blogs from the database on to the website. So I'll create a function called get_blog_by_id and I'll pass in the blog id into the function and return an associative array of the blog fields.

Now the blog system queries the database on the home page and the blog page. And now instead of copying and pasting HTML for the blog preview and creating a new file for every blog I can just go to phpMyAdmin and add a new blog there. This way I'll have time to go play video games.

So now when I request to zachdyer.com?id=1 the $blog variable is created and I can use it within the view of the single blog page. Now I can see the blog from the database on the site.

But what about the featured blogs on the home page. Those are still hard coded into the index.php file. For now I think I'll just get all the blogs and display them on in the preview boxes on the home page. So I'll create a function called get_all_blogs and have it return all the blogs in an array. Now that I have the array I can loop over the array and generate the HTML for each blog preview box.

Now the blog system queries the database on the home page and the blog page. And instead of having to copy and paste HTML for a blog preview and create a whole new file for a blog, I just go to phpMyAdmin and add the blog there saving me time so I can get back to playing video games until I run out of money. When that happens I'll have to monotize the blog with Google Adsense. And if you want to learn how to make money on a blog that you created yourself then stay tuned for next time.

Comments