Hướng dẫn booking calendar php mysql
n this tutorial, we will build a nice booking calendar using PHP and MySQL. Booking calendars are very common applications, you will learn how to write PHP code that separates business logic from presentation through this tutorial. Show After this tutorial, you should be able to build a working booking calendar as shown below: Prepare the databaseAll the booking dates will be stored inside the MySQL database. Let's design a simple database table. Run the SQL statement below from your database console to create a table: bookings.
All bookings will be stored inside the table above, and each booked date is saved inside booking_date column. Build a Booking classIn this section, we will build a PHP class that interacts with the database. It is capable of listing all the bookings, inserting a booking as well as deleting a booking.
The code above will connect to a database when it is instantiated using PDO. When working with a database in PHP, we should always use PDO as it provides great security and friendly API.
This function will list out all the booking records and return them as an associated array.
This function inserts a booking into the bookings table. We are using PDO's prepare statements to do the insertion, which provides auto escaping.
This function deletes a booking from the bookings table by taking the primary key. That is all for Booking class, and the complete Booking.php is shown as below:
Build a Calendar classIn one of our previous tutorials, we have built a powerful calendar class that comes with hooks. In this tutorial, we will utilize it to accomplish our goal.
We are not going to explore the details of this class, since we have already covered it at https://www.startutorial.com/articles/view/php-calendar-class-with-hooks, feel free to read more there.
Two major APIs of this class we should take a look at:
The BookableCell connects to the Calendar class via a hook, we call BookableCell a plugin for Calendar. Putting it TogetherNow it is time to put everything together and present our booking calendar to the world.
Let's go through the code above from top to bottom.
Final and Source CodeIf you have followed along correctly. Head over to index.php from your browser. You should see a beautiful PHP booking calendar as shown below: If you follow along with the tutorial step by step, you will get all the source code in place. However, if you are feeling lazy or need to download the complete source code from us. You can do so by paying us a small fee. Your support will enable us to produce better and more in-depth tutorials. Download Source Code ($9) |