Jump to content


Photo

Database Design for Calendar


  • Please log in to reply
1 reply to this topic

#1 appeland

appeland
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 29 September 2006 - 09:14 AM

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


#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 29 September 2006 - 09:27 AM

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:
SELECT `employee`.`name`, `dates`.`shift_pattern`
FROM `employees`
JOIN `dates`
ON `employees`.`emp_id` = `dates`.`emp_id`
WHERE `dates`.`emp_id` = 1;

then use bitwise comparison to see which days someone works:

<?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";
}

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users