I’m currently developing a website for a restaurant. At the website, there should be a widget where people can see if there are still places available or if everything is fully booked. It’s not the intention to create a complete booking app, therefore it would really just be a case of setting a date in the database ‘true’ (still places available) or ‘false’ (fully booked).
However, I’m wondering how I should add the dates to the database. The widget should display the booking status for 2 weeks in the future. Should I just fill the databases with a row for every day of the next 20 years? That doesn’t seem to be the most forward-looking approach. Something where the database adds a row daily so there are always 14 future days would seem to be the best option, but how can I realize that?
First thing… I’m not quite sure that you need a database for this.
If you will simply have the information for 14 days, a database could be a overkill. Especially if you are not going to keep previous records and just need two fields/columns (date and status)
You could simply use a text file for that.
Regarding adding 20 years ahead of information, that’s not necessary. Either you add a cron job or you use a script to see how many dates in the future the widget needs to add every time someone uses that widget. I suppose that, after all, they will need to use the widget at least once a day to update the status for that particular date.
Another option would be to add a variable to see if the information has already been checked that day and act accordingly.
You’re probably right that a database would be overkill. However, I’m already using a database for other parts of the website so the choice for a database was obvious. Are there any downsides to use a database instead of a simple text file?
Thinking about using a PHP script that checks the database every time a user visits the site although a cron job seems like an interesting approach as well.
A database isn’t really an overkill. Its just a database. A basic component of most sites in existence. Don’t overthink it.
Having said that you’re looking at the problem the opposite way of how you should be. You just need to store the days that have been booked in the database. The bare minimum simplest approach would be as follows.
restaurant_bookings
booked_on DATE
PRIMARY KEY(booked_on)
However, that schema doesn’t really lend well to change. So I would make it slightly more robust.