fruitwerks
08-06-2009, 05:42 AM
lots of info I'm not going to share, but I am working on updating my reptile records website.
here is the db structure so far.
--
-- Table structure for table `animals`
--
CREATE TABLE IF NOT EXISTS `animals` (
`idx` int(10) unsigned NOT NULL auto_increment,
`animal_id` mediumint(8) unsigned NOT NULL,
`bday` date NOT NULL,
`pdate` date default NULL,
`sdate` date default NULL,
`pprice` int(10) unsigned default NULL,
`sprice` int(10) unsigned default NULL,
`mom` smallint(6) default NULL,
`dad` smallint(6) default NULL,
`bin` varchar(10) NOT NULL,
`genetic` varchar(45) default NULL,
`aname` varchar(45) NOT NULL,
`foil` varchar(12) default NULL,
`feedfreq` tinyint(2) unsigned NOT NULL,
`species` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `animals`
--
INSERT INTO `animals` (`idx`, `animal_id`, `bday`, `pdate`, `sdate`, `pprice`, `sprice`, `mom`, `dad`, `bin`, `genetic`, `aname`, `foil`, `feedfreq`, `species`) VALUES
(2, 33, '2008-07-16', '2008-10-22', NULL, 500, NULL, NULL, NULL, 'm6104', 'kahl albino', 'frost', NULL, 7, 1),
(3, 28, '2007-07-16', NULL, NULL, 800, NULL, NULL, NULL, 'ap401', 'hypo het snow', 'lola', NULL, 14, 1);
-- --------------------------------------------------------
--
-- Table structure for table `events_ref`
--
CREATE TABLE IF NOT EXISTS `events_ref` (
`event_id` int(10) unsigned NOT NULL auto_increment,
`events_idx` smallint(5) unsigned NOT NULL,
`ev_name` varchar(45) NOT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
--
-- Dumping data for table `events_ref`
--
INSERT INTO `events_ref` (`event_id`, `events_idx`, `ev_name`) VALUES
(1, 1, 'feeding'),
(2, 2, 'poo'),
(3, 3, 'pee'),
(4, 4, 'shed'),
(5, 5, 'weight'),
(6, 6, 'length'),
(7, 7, 'vet'),
(8, 8, 'notes');
-- --------------------------------------------------------
--
-- Table structure for table `feedings`
--
CREATE TABLE IF NOT EXISTS `feedings` (
`feedings_id` int(11) NOT NULL auto_increment,
`animal_id` smallint(6) NOT NULL,
`perm_id` smallint(6) NOT NULL,
`feed_date` date NOT NULL,
PRIMARY KEY (`feedings_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `feedings`
--
INSERT INTO `feedings` (`feedings_id`, `animal_id`, `perm_id`, `feed_date`) VALUES
(1, 28, 18, '2009-08-03'),
(2, 33, 9, '2009-07-06');
-- --------------------------------------------------------
--
-- Table structure for table `foodinv`
--
CREATE TABLE IF NOT EXISTS `foodinv` (
`foodinv_id` int(11) NOT NULL auto_increment,
`type` varchar(45) NOT NULL,
`qty` smallint(6) NOT NULL,
`weight` smallint(5) unsigned NOT NULL,
`perm_id` tinyint(4) NOT NULL default '0',
PRIMARY KEY (`foodinv_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
--
-- Dumping data for table `foodinv`
--
INSERT INTO `foodinv` (`foodinv_id`, `type`, `qty`, `weight`, `perm_id`) VALUES
(18, 'bunny 1-1.25lbs', 3, 510, 16),
(19, 'med rat', 9, 130, 9),
(20, 'rat pinky', 8, 35, 15);
-- --------------------------------------------------------
--
-- Table structure for table `master_ev`
--
CREATE TABLE IF NOT EXISTS `master_ev` (
`ev_id` int(11) NOT NULL auto_increment,
`animal_id` smallint(5) unsigned NOT NULL,
`events_idx` tinyint(3) unsigned NOT NULL,
`ev_date` date NOT NULL,
PRIMARY KEY (`ev_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `master_ev`
--
INSERT INTO `master_ev` (`ev_id`, `animal_id`, `events_idx`, `ev_date`) VALUES
(2, 28, 1, '2009-08-03'),
(3, 33, 3, '2009-07-06');
-- --------------------------------------------------------
--
-- Table structure for table `notes`
--
CREATE TABLE IF NOT EXISTS `notes` (
`notes_id` int(11) NOT NULL auto_increment,
`animal_id` mediumint(8) unsigned NOT NULL,
`note` text NOT NULL,
`note_date` date NOT NULL,
PRIMARY KEY (`notes_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
-- Dumping data for table `notes`
--
-- --------------------------------------------------------
--
-- Table structure for table `species`
--
CREATE TABLE IF NOT EXISTS `species` (
`species_id` int(11) NOT NULL auto_increment,
`species_idx` tinyint(4) NOT NULL,
`species` varchar(45) NOT NULL,
`subspecies` varchar(45) NOT NULL,
PRIMARY KEY (`species_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `species`
--
INSERT INTO `species` (`species_id`, `species_idx`, `species`, `subspecies`) VALUES
(1, 1, 'Boa Constrictor', '');
At the moment, the data in there is enough to poke around, I'm working on the rendering of the info before I start on 'client side' type input and fancy stuff.
Here is the output of the events record, this is where I am a bit hung up. Not sure if it is my code or the db design.
event food item aname date
pee 9 frost 2009-07-06
feeding 18 lola 2009-08-03
now here is the biggest problem I am having and I am not sure what way to approach. Even though the event type is not 1 (feeding) it shows a feeding record (code for the item anyway, not that far along yet).
here is the code for that page
<?php
include "dbcore.php";
// select food db order by weight
$query = "SELECT * FROM master_ev left join events_ref on master_ev.events_idx = events_ref.event_id
left join animals on master_ev.animal_id = animals.animal_id
left join feedings on master_ev.animal_id = feedings.animal_id oder by ev_date";
// error check
$result = mysql_query($query) or die(mysql_error());
// construct data
echo "<table border='1'>";
echo "<tr> <th>event</th> <th>food item</th> <th>aname</th> <th>date</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['ev_name'];
echo "</td><td>";
echo $row['perm_id'];
echo "</td><td>";
echo $row['aname'];
echo "</td><td>";
echo $row['ev_date'];
echo "</td></tr>";
}
echo "</table>";
?>
If there is a feeding record it will show and override the other event. I need to keep each event separate. I will eventually filter these results by animal, but not right now.
The feeding and notes event types are special because they require their own tables. Is there a better way I can structure my table to make this a bit better or does it need to be done with the coding? If it is an issue with the db structure please be specific.
Thanks!
here is the db structure so far.
--
-- Table structure for table `animals`
--
CREATE TABLE IF NOT EXISTS `animals` (
`idx` int(10) unsigned NOT NULL auto_increment,
`animal_id` mediumint(8) unsigned NOT NULL,
`bday` date NOT NULL,
`pdate` date default NULL,
`sdate` date default NULL,
`pprice` int(10) unsigned default NULL,
`sprice` int(10) unsigned default NULL,
`mom` smallint(6) default NULL,
`dad` smallint(6) default NULL,
`bin` varchar(10) NOT NULL,
`genetic` varchar(45) default NULL,
`aname` varchar(45) NOT NULL,
`foil` varchar(12) default NULL,
`feedfreq` tinyint(2) unsigned NOT NULL,
`species` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `animals`
--
INSERT INTO `animals` (`idx`, `animal_id`, `bday`, `pdate`, `sdate`, `pprice`, `sprice`, `mom`, `dad`, `bin`, `genetic`, `aname`, `foil`, `feedfreq`, `species`) VALUES
(2, 33, '2008-07-16', '2008-10-22', NULL, 500, NULL, NULL, NULL, 'm6104', 'kahl albino', 'frost', NULL, 7, 1),
(3, 28, '2007-07-16', NULL, NULL, 800, NULL, NULL, NULL, 'ap401', 'hypo het snow', 'lola', NULL, 14, 1);
-- --------------------------------------------------------
--
-- Table structure for table `events_ref`
--
CREATE TABLE IF NOT EXISTS `events_ref` (
`event_id` int(10) unsigned NOT NULL auto_increment,
`events_idx` smallint(5) unsigned NOT NULL,
`ev_name` varchar(45) NOT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
--
-- Dumping data for table `events_ref`
--
INSERT INTO `events_ref` (`event_id`, `events_idx`, `ev_name`) VALUES
(1, 1, 'feeding'),
(2, 2, 'poo'),
(3, 3, 'pee'),
(4, 4, 'shed'),
(5, 5, 'weight'),
(6, 6, 'length'),
(7, 7, 'vet'),
(8, 8, 'notes');
-- --------------------------------------------------------
--
-- Table structure for table `feedings`
--
CREATE TABLE IF NOT EXISTS `feedings` (
`feedings_id` int(11) NOT NULL auto_increment,
`animal_id` smallint(6) NOT NULL,
`perm_id` smallint(6) NOT NULL,
`feed_date` date NOT NULL,
PRIMARY KEY (`feedings_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `feedings`
--
INSERT INTO `feedings` (`feedings_id`, `animal_id`, `perm_id`, `feed_date`) VALUES
(1, 28, 18, '2009-08-03'),
(2, 33, 9, '2009-07-06');
-- --------------------------------------------------------
--
-- Table structure for table `foodinv`
--
CREATE TABLE IF NOT EXISTS `foodinv` (
`foodinv_id` int(11) NOT NULL auto_increment,
`type` varchar(45) NOT NULL,
`qty` smallint(6) NOT NULL,
`weight` smallint(5) unsigned NOT NULL,
`perm_id` tinyint(4) NOT NULL default '0',
PRIMARY KEY (`foodinv_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
--
-- Dumping data for table `foodinv`
--
INSERT INTO `foodinv` (`foodinv_id`, `type`, `qty`, `weight`, `perm_id`) VALUES
(18, 'bunny 1-1.25lbs', 3, 510, 16),
(19, 'med rat', 9, 130, 9),
(20, 'rat pinky', 8, 35, 15);
-- --------------------------------------------------------
--
-- Table structure for table `master_ev`
--
CREATE TABLE IF NOT EXISTS `master_ev` (
`ev_id` int(11) NOT NULL auto_increment,
`animal_id` smallint(5) unsigned NOT NULL,
`events_idx` tinyint(3) unsigned NOT NULL,
`ev_date` date NOT NULL,
PRIMARY KEY (`ev_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `master_ev`
--
INSERT INTO `master_ev` (`ev_id`, `animal_id`, `events_idx`, `ev_date`) VALUES
(2, 28, 1, '2009-08-03'),
(3, 33, 3, '2009-07-06');
-- --------------------------------------------------------
--
-- Table structure for table `notes`
--
CREATE TABLE IF NOT EXISTS `notes` (
`notes_id` int(11) NOT NULL auto_increment,
`animal_id` mediumint(8) unsigned NOT NULL,
`note` text NOT NULL,
`note_date` date NOT NULL,
PRIMARY KEY (`notes_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
-- Dumping data for table `notes`
--
-- --------------------------------------------------------
--
-- Table structure for table `species`
--
CREATE TABLE IF NOT EXISTS `species` (
`species_id` int(11) NOT NULL auto_increment,
`species_idx` tinyint(4) NOT NULL,
`species` varchar(45) NOT NULL,
`subspecies` varchar(45) NOT NULL,
PRIMARY KEY (`species_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `species`
--
INSERT INTO `species` (`species_id`, `species_idx`, `species`, `subspecies`) VALUES
(1, 1, 'Boa Constrictor', '');
At the moment, the data in there is enough to poke around, I'm working on the rendering of the info before I start on 'client side' type input and fancy stuff.
Here is the output of the events record, this is where I am a bit hung up. Not sure if it is my code or the db design.
event food item aname date
pee 9 frost 2009-07-06
feeding 18 lola 2009-08-03
now here is the biggest problem I am having and I am not sure what way to approach. Even though the event type is not 1 (feeding) it shows a feeding record (code for the item anyway, not that far along yet).
here is the code for that page
<?php
include "dbcore.php";
// select food db order by weight
$query = "SELECT * FROM master_ev left join events_ref on master_ev.events_idx = events_ref.event_id
left join animals on master_ev.animal_id = animals.animal_id
left join feedings on master_ev.animal_id = feedings.animal_id oder by ev_date";
// error check
$result = mysql_query($query) or die(mysql_error());
// construct data
echo "<table border='1'>";
echo "<tr> <th>event</th> <th>food item</th> <th>aname</th> <th>date</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['ev_name'];
echo "</td><td>";
echo $row['perm_id'];
echo "</td><td>";
echo $row['aname'];
echo "</td><td>";
echo $row['ev_date'];
echo "</td></tr>";
}
echo "</table>";
?>
If there is a feeding record it will show and override the other event. I need to keep each event separate. I will eventually filter these results by animal, but not right now.
The feeding and notes event types are special because they require their own tables. Is there a better way I can structure my table to make this a bit better or does it need to be done with the coding? If it is an issue with the db structure please be specific.
Thanks!