简体   繁体   中英

Display mysql data for Current Week in 7 day calendar

I need a guiding hand, how do I display the " current week" starting on Monday , in a 7-day calendar by a 7-room grid, for a small theatre I volunteer at, various voluntary town groups use the rooms.
My database name=rooms, table=roomdiary, it has 7 fields (field_1 to Field_7).

I can populate each day with data from the dbase using the "Select" query & php isset code where required. But I am stumped on how to correctly display only " current week " data in the right room on right day. >

////// Mon... Tues . Wed .. Thu .. Fri . Sat .. Sun
room 1 data . data . data . data . data  data . data
room 2 data . data . data . data . data  data . data
room 3 data . data . data . data . data  data . data
room 4 data . data . data . data . data  data . data
room 5 data . data . data . data . data  data . data
room 6 data . data . data . data . data  data . data
room 7 data . data . data . data . data  data . data

I apologise for having not posted any code, as I thought any generic advice would be more than helpful and help me learn. Any guidance would be appreciated.

  1. Redesign Your database, make table rooms (room_id, room_name) and table schedule (room_id, date, data, week_nr, year)

  2. Always write with date also week_nr to database - date('W', timestamp)

  3. If you don't want to store week_nr in database, then try strtotime('Last monday', timestamp) and strtotime('Next sunday', timestamp) and select range from database dates

I recommend you split this problem in two

  1. Calculate start of display period (="Which date is the current Monday")
  2. Select and display your records

The first point is quite easy using PHP's date() or getdate() functions, the second can be achieved by SELECT ... WHERE datecolumn BETWEEN monday_date AND sunday_date .

This helps modularize your program: What if you want to add future display options? What if your theater decides not to play on mondays?

Additionally I recommend against harcoding your 7 rooms into 7 fields: A relational approach would have a "rooms" table (currently with 7 rows) and a "performances" table linking a room and a date. If you add a "programs" table and also link it to the "performances" table, you already get the program data for free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM