I am trying to make a car booking system using a jquery datepicker. I have the date picker, I have dates in a database(manually inserted in WAMP SERVER phpMyAdmin) that highlight those dates as pre-booked dates (Cursor disabled in css on mouse over) so you can’t book those ones.
I’m at the point of assembling all my parts but have become stuck on the last bit of how to store the dates. It seems from what I’ve read, that storing a bunch of dates in one row is a no no (plus I haven’t managed to do it).
- So, should I store a start and end date for each client on the one DB table row and generate the in between dates when the page/datepicker loads?
- If this approach how do I get and then join each row (many clients) of dates together?
- Or should I generate the in between dates and then store all the dates in a separate table with the client id when the datepicker selects start & end?
Could someone help with the correct approach and php code to get me there. I already have the mysqli connection etc.
My DB columns at present are start_date
& end_date
in tabledates_from_to
. My datepicker uses this format 2021-04-15
and the array needed looks like this '2021-04-15','2021-04-16','2021-04-17'
to highlight the dates.
2
Answers
After about a month I have the date-picker working, storing all dates in DB on their own row.
I have copied different bits of code from this site and manipulated it a little & at present I'm very happy with the hybrid outcome.
This is running on a WAMP server.
Below is my code to help other amateurs like me.
Seems like you have a 1:n relation between clients/customers and bookings. So yes you should store them in separate tables according to database normalization rules.
Assume you have a table clients (primary key client_id) and bookings, which replaces your dates_from_to table (with foreign key client_id) you can do the following JOIN:
For the next part I take the example array. Here is example code to insert one row for every day:
As an alternative you could use first and last day of the period and store everything in one row by replacing
booking_date
column bystart_date
andend_date
.Here is the version with mysqli and start/end date (from docs):
Hope this keeps you going.