Jump to content

Database Design for Calendar


appeland

Recommended Posts

Hello,
this troubles me for a couple of month now, I haven't started writing anything and it's not urgent to, it's one of them fun-to-do-sometime projects  ::)

I would like to create a staffing schedule calendar and I am wondering how to
do the database layout for this.
The easiest way would be to have one field in the DB per day and per employee but that'll obviously generate masses of data.

I also thought to have a database field per month per employee and then delimiting each day with some character, and sub- definitions of the day with another character.

Would you have any input into this please.

Thansk & Regards,
Andi
Link to comment
Share on other sites

Off the top of my head, I'd use two tables; Employees and Dates (with emp_id as foreign key)

Employees contains only employee info.

Dates contains three fields, week_id INT and emp_id INT shift_pattern INT.

Then use bitwise to denote which days people will work.

Can then use queries like:
[code]SELECT `employee`.`name`, `dates`.`shift_pattern`
FROM `employees`
JOIN `dates`
ON `employees`.`emp_id` = `dates`.`emp_id`
WHERE `dates`.`emp_id` = 1;[/code]

then use bitwise comparison to see which days someone works:

[code]<?php

$row = mysql_fetch_assoc($result);

$days = array(
  1 => 'Sun',
  2 => 'Mon',
  4 => 'Tue',
  8 => 'Wed',
  16 => 'Thur',
  32 => 'Fri',
  64 => 'Sat'
);

for ($i = 1; $i < 64; $i += $i)
{
    if ($row['shift_pattern'] ^ $i) echo "{$row['name']} is working on {$days[$i]}<br />\n";
}

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.