PHP/SQL prevent writing certain times into table - php

I guess my question might have a simple solution, but I am stuck.
I have a MYSQL-Table and a form. The user input fills in the form. The user has to provide a start time for the event. e.g. 8:00 o'clock and a duration of the event. The end time is calculated based on the duration. So if a event starts at 8, has a duration of 2 hours, there will be 10:00 be written in the end-Time column.
The problem is now, how do I prevent, that users can fill in events at 9:00 since the room is blocked from 8 - 10?
I appreciate your help!
Cheers!

after user submits form, you can take submitted time and check whether room is occupied in that time with this query:
SELECT COUNT(1)
FROM event as e
WHERE e.room = <room_number>
AND (<start_time_of_new_event> BETWEEN e.start_time AND e.end_time
OR <end_time_of_new_event> BETWEEN e.start_time AND e.end_time)
start_time_of_new_event would be submitted start time value
end_time_of_new_event would be submitted start time + duration
room_number -> i just assumed you have more rooms, where events can take place
if this query returns 1 or more, room is occupied in submitted time.
If you would like to disable possibility to submit time in which room is occupied, you could select occupied time slots before displaying the form. And in script decide which times can be booked

Related

Show posts for correct day with time zones

I am setting up a system using PHP and MySQL in which users can add posts to specific days (all posts will be grouped by day).
So When a user goes to the main page, the user will see all of the posts for that day.
Now the problem - Time-zones:
What if I have 2 users, who are 3 hours apart.
User A will post at 12:00 AM, which will add the post to that specific day.
User B, who is in a different time zone, so for this user, it will still be the previous day, say 9:00 PM, will not see the post from user A since user B is still in the previous day in his time zone - this is fine.
Now, when user B gets to the next day (the same day as user A, meaning + 3 hours), he should see all of the posts for that day, but here is the problem:
For user B, user A's posts were added to the previous day, which means that user B will not see user A's post (when user A posted, it was 9:00 PM for user B on the previous day).
So the problem is with the time of the post, I'm not sure how to set this up, and I may be thinking about it all wrong - I actually have a feeling that I am missing something very basic in the logic.
I have searched for similar puzzles but did not really find anything that helped.
Any ideas and/or solutions will be greatly appreciated!

MySQL - automatically update row after x seconds of inactivity (set expiration time)

For an art project I am trying to set up an order site.
The concept allows users to book a max. of two time slots with each artist. There are 12 slots per artist, but each slot with a specific definition (so each is unique). The slots are only available for a very limited time and hopefully booked fast. So there will be a lot of requests in a short period of time. I have to make sure each article/slot is only offered to a single user at a time and cannot be double booked.
My idea was, to check for the next unbooked slot(s) (status="free) and on that request update the status of the corresponding row in the table to status="locked". If the user proceeds to actually book the slot, the status is updated to "booked".
If a user clicks "cancel" I can release the article by updating the row to status="free".
However, it is not unlikely that users just abandon the site and I don't see a way to check for that. The slot would remain "locked". I was thinking, there might be a way to automatically reset the status e.g. 120 seconds after is was "locked" and show a countdown to the users. This could even enhance the excitement factor.
I don't think a cron job would work as I need the anchor to be the last update of the row and not a specific datetime.
I looked into MySQL events but understood that I cannot manipulate the data of the table it is attached to.
I would greatly appreciate any ideas.
Thanks,
Sam
In your db your status table add a datetime field.
When someone lock a slot you also save the current time using NOW()
When someone consult the slots you perform and update and free the inactive slots
Update slots
SET locked = false
WHERE `datetime`> NOW() - INTERVAL 15 MINUTE;
SELECT *
FROM slots
WHERE locked = false;

Building a scheduler with sql, php. Most efficient way

I am creating a system that requires a schedular for a particular task. Users may pick from times 24 hours a day, 7 days a week.
I came up with a few options for the database storage, but I don't think either one is the most efficient design, so I'm hoping for some possible alternatives that may be more efficient.
On the user side I created a grid of buttons with 2 loops to create the days, and the times, and I set each a unique value of $timeValue = "d".$j."-t".$i;
So d1-t0 will be Saturday at Midnight d3-t12= Tuesday at Noon, and so forth.
So, in the database I was first going to simply have a ID, day, time set up, but that would result in a possible 168 rows per event
Then I tried an ID, day, and time 0-23 (a column for each hour of the day) And I was simply going to have a boolean set up. 0 if not selected, 1 if it is.
This would result in 7 rows per event, but I think querying that data might be a pain.
I need to perform a few functions on this data. On each day, list the number of selected times into an array. But I don't believe having a select statement of SELECT * from schedule where time0, =1 or time1= 1 .... ect will work, nor will it produce the desired array. (times=(0,3,5,6,7...)
So, this isnt going to work well.
My overall system will need to also know every event that has each time selected for a mass posting.
"Select * from table where time = $time (0-23) and day= $day (1-7)
Do action with data...
So with this requirement, I'm going to assume that storing the times as an array within the database is likely not the most efficient way either.
So am I stuck with needing up to 168 rows of data per event, or is there a better way I am missing? Thanks
Update:
To give a little more clarity on what I need to accomplish:
Users will be creating event campaigns in which other users can bid on various time slots for something to happen. There will likely be 10-100 thousand of these campaigns at any one time and they are ongoing until the creator stops them. The campaign creators can define the time slots available for their campaign.
At the designated time each day the system will find every campaign that has an event scheduled and perform the event.
So the first requirement is to know which time slots are available for the campaign, and then I need the system to quickly identify campaigns that have an event on each hour and day and perform it automatically.

Conditions in MySQL/PHP

I have a website on PHP which works as "appointment management System". I have 5 Doctors in my clinic. I have added them in list (web). Now when a patient comes in clinic i fill a form which contains (Name, Add, Phone number, Appointment with Dr.(Selecting from a list), appointment date and time. Now my Question is how can i restrict this form if we have already an appointment is booked on same date and same time like 1-Oct-2015 and start time 2pm to 3pm with a patient. Form can not duplicate/override previous appointment and gives me notification that in this timing already an appointment is fixed with doctor 1. ??? plz help
Since you haven't provided anything code wise to show whatever you have or have tried at the moment, my answer will be very straightforward: check your database for a duplicate record with that same date and time range before saving.

Calendar with time slot reservation

I'm very new in this site. Spend a lot of time for searching an answer for my target, but unsuccessfully. So, there it is (sorry for my english):
I'm quite new in php, but already created nice stuff. But now I'm interested in calendar with time slot reservation. I saw good exemple then I registered for my doctor. There i was able to choose a day from calendar and then select a free time range (e.g. from 12:00 to 12:30).
First idea for this is some calendar in PHP and table in MySQL. In MySQL table few rows with id, name and quantity. id - unique number of row, name - date and time slot, and quantity - (1 = free cell, 0 = unable). In PHP calendar after selecting a free time slot, in DB quantity goes from 1 to 0 and apear unavailable for other users.
But if where are a lots of "doctors"? How to make calendar, DB for thousands of them?
For the beggining maybie someone have some example how to make calendar with time reservation (e.g. 1 hour) in the easiest way? Or suggest something?

Categories