17 Apr 2015
Chapter 7
Exercise 7.1
- Every entry in the table has exactly one value of the appropriate type.
- No attribute in the table is redundant with the primary key.
5 is violated in dbSchedules.php
where persons is a comma-separated list of ids, like “allen7298111”.
6 is violated by dbPersons.php
. The id field is determined by:
// id (unique key) = first_name . phone1
$this->id = $f . $p1;
Which is not guarenteed to be truly unique and therefore could be redundant. There could easily be a situation in which two individuals have the same id.
Exercise 7.2
function get_shift_month($id) {
$shift_month=date("F",
mktime(0,0,0,substr($id,0,2),
substr($id,3,2),substr($id,6,2));
}
function get_shift_day($id) {
$shift_day=date("l",
mktime(0,0,0,substr($id,0,2),
substr($id,3,2),substr($id,6,2));
}
function get_shift_year($id) {
$shift_year=date("Y",
mktime(0,0,0,substr($id,0,2),
substr($id,3,2),substr($id,6,2));
}
function get_shift_start($id) {
$shift_start=date("h:i",
mktime(0,0,0,substr($id,0,2),
substr($id,3,2),substr($id,6,2));
}
Exercise 7.3
Months.php
<?php
include_once('RMHdates');
class Month {
private $id;
private $dates;
private $name;
private $weekday_start;
private $timestamp;
}
function __construct($mm,$yy) {
$this->timestamp = mktime(0,0,0,$mm,1,$yy);
$this->days = date("t", $this->timestamp);
$this->dates[0] = new RMHdate(
$mm."-01-".$yy,null,"","");
for($i=1;$i<=8;$i++)
$this->dates[] = new RMHdate(
#mm."-".($i+1)."-".$yy,null,"","");
for($i=9; $i<$this->days; $i++)
$this->dates[] = new RMHdate(
$mm."-".($i+1)."-".$yy,null,"","");
$this->name=date("F,Y", $this->timestamp);
$this->weekday_start=date("w", $this->timestamp);
$this->id=$this->dates[0]->get_id();
}
?>
dbMonths.php
<?php
include_once('Month.php');
include_once('dbinfo.php');
include_once('dbDates.php');
function setup_dbMonths() {
connect();
mysql_query("DROP TABLE IF EXISTS dbMonths");
$result=mysql_query("CREATE TABLE dbMonths
(id CHAR(8) NOT NULL, dates TEXT,
weekday_start INT, timestamp INT,
name TEXT, end INT, PRIMARY KEY (id))");
if(!$result)
echo mysql_error();
mysql_close();
}
/**
* Inserts a month into the db
* @param $m the month to insert
*/
function insert_dbMonths($m) {
if (! $m instanceof Month) {
die ("Invalid argument for dbMonths->add_month function call");
}
connect();
$query = "SELECT * FROM dbMonths WHERE id =\"".$m->get_id()."\"";
$result = mysql_query ($query);
if(mysql_num_rows($result)!=0) {
delete_dbMonths($m);
connect();
}
$query="INSERT INTO dbMonths VALUES
(\"".$m->get_id()."\",".
get_dates_text($m->get_dates()).",\"".
$m->get_weekday_start()."\",\"".
$m->get_timestamp()."\",\"".
$m->get_name()."\",\"".
$m->get_end()."\")";
$result=mysql_query($query);
mysql_close();
if (!$result) {
echo ("unable to insert into dbMonths: ".$m->get_id(). mysql_error());
return false;
}
else foreach($m->get_dates() as $i)
insert_dbDates($i);
return true;
}
/**
* Deletes a month from the db
* @param $m the month to delete
*/
function delete_dbMonths($m) {
if (! $m instanceof Month)
die ("Invalid argument for delete_dbMonths function call");
connect();
$query="DELETE FROM dbMonths WHERE id=\"".$m->get_id()."\"";
$result=mysql_query($query);
mysql_close();
if (!$result) {
echo ("unable to delete from dbMonths: ".$m->get_id(). mysql_error());
return false;
}
else foreach ($m->get_dates() as $i)
delete_dbDates($i);
return true;
}
/**
* Updates a Month in the db by deleting it and re-inserting it
* @param $m the Month to update
*/
function update_dbMonths($m) {
if (! $m instanceof Month)
die ("Invalid argument for dbMonths->replace_month function call");
if (delete_dbMonths($m))
return insert_dbMonths($m);
else return false;
}
/**
* Selects a month from the database
* @param $id month id
* @return mysql entry corresponding to id
*/
function select_dbMonths($id) {
if(strlen($id)!=8) {
die ("Invalid month id.");
}
else {
$timestamp = mktime(0,0,0,substr($id,0,2),substr($id,3,2),substr($id,6,2));
$dow = date("N",$timestamp);
$id=date("m-d-y",mktime(0, 0, 0, substr($id,0,2), substr($id,3,2)-$dow+1, substr($id,6,2)));
}
connect();
$query = "SELECT * FROM dbMonths WHERE id =\"".$id."\"";
$result = mysql_query ($query);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
$result_row = false;
}
else
$result_row=mysql_fetch_row($result);
mysql_close();
return $result_row;
}
/**
* retrieves a month from the database
* @param $id = id of the month to retrieve
* @return the desired month, or null
*/
function get_dbMonths($id) {
$result_row=select_dbMonths($id);
if($result_row) {
$dates=$result_row[1];
$dates=explode("*",$dates);
$d=array();
foreach($dates as $i){
$d[]=select_dbDates($i);
}
$m=new Month($d, $result_row[2], $result_row[3], $result_row[4], $result_row[5]);
}
return $m;
}
/**
* the full contents of dbMonths, used by addMonth to list all scheduld Months
* @return mysql result array of Months
*/
function get_all_dbMonths() {
connect();
$query="SELECT * FROM dbMonths";
$result=mysql_query($query);
mysql_close();
return $result;
}
/**
* generates a string of date ids
* @param $dates array of dates for a month
* @return string of date ids, comma delimited
*/
function get_dates_text($dates){
$d="\"".$dates[0]->get_id();
for($i=1;$i<7;++$i) {
$d=$d."*".$dates[$i]->get_id();
}
return $d."\"";
}
?>
Laura Barber at 1:00PM