You could use blob, or text and store them as base64 strings, but it would be better to stick to using image files for what they are built for, for a lot of different reasons, don't get me started on that

. Make your table data relate to the images in the file structure programmatically.
I think this is roughly what you are looking to make, I added the image_file column to the `building_type` table.

Download diagram:
http://wikisend.com/download/955210/example.mwb
(
http://dev.mysql.com/downloads/workbench/)
Code: Select all
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`player`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`player` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`city`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`city` (
`id` INT NOT NULL AUTO_INCREMENT,
`player_id` INT NOT NULL,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`, `player_id`),
INDEX `fk_city_player_idx` (`player_id` ASC),
CONSTRAINT `fk_city_player`
FOREIGN KEY (`player_id`)
REFERENCES `mydb`.`player` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`building_type`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`building_type` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`image_file` VARCHAR(45) NOT NULL,
`level` VARCHAR(45) NOT NULL,
`required_build_time` TIME NOT NULL,
`resource_generation_time` TIME NOT NULL,
`required_building_type_id` INT NULL,
PRIMARY KEY (`id`),
INDEX `fk_building_type_building_type1_idx` (`required_building_type_id` ASC),
CONSTRAINT `fk_building_type_building_type1`
FOREIGN KEY (`required_building_type_id`)
REFERENCES `mydb`.`building_type` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`building`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`building` (
`id` INT NOT NULL AUTO_INCREMENT,
`city_id` INT NOT NULL,
`building_type_id` INT NOT NULL,
`position` INT NOT NULL,
`build_start_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`id`, `city_id`),
INDEX `fk_building_city1_idx` (`city_id` ASC),
INDEX `fk_building_building_type1_idx` (`building_type_id` ASC),
CONSTRAINT `fk_building_city1`
FOREIGN KEY (`city_id`)
REFERENCES `mydb`.`city` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_building_building_type1`
FOREIGN KEY (`building_type_id`)
REFERENCES `mydb`.`building_type` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`resource`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`resource` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`building_resource`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`building_resource` (
`id` INT NOT NULL AUTO_INCREMENT,
`building_id` INT NOT NULL,
`resource_id` INT NOT NULL,
`amount` INT NOT NULL DEFAULT 0,
PRIMARY KEY (`id`, `building_id`, `resource_id`),
INDEX `fk_building_resource_building1_idx` (`building_id` ASC),
INDEX `fk_building_resource_resource1_idx` (`resource_id` ASC),
CONSTRAINT `fk_building_resource_building1`
FOREIGN KEY (`building_id`)
REFERENCES `mydb`.`building` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_building_resource_resource1`
FOREIGN KEY (`resource_id`)
REFERENCES `mydb`.`resource` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`building_type_resource`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`building_type_resource` (
`resource_id` INT NOT NULL AUTO_INCREMENT,
`building_type_id` INT NOT NULL,
INDEX `fk_building_type_resource_resource1_idx` (`resource_id` ASC),
INDEX `fk_building_type_resource_building_type1_idx` (`building_type_id` ASC),
PRIMARY KEY (`resource_id`, `building_type_id`),
CONSTRAINT `fk_building_type_resource_resource1`
FOREIGN KEY (`resource_id`)
REFERENCES `mydb`.`resource` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_building_type_resource_building_type1`
FOREIGN KEY (`building_type_id`)
REFERENCES `mydb`.`building_type` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`building_required_resource`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`building_required_resource` (
`building_type_id` INT NOT NULL,
`resource_id` INT NOT NULL,
`amount` VARCHAR(45) NOT NULL,
PRIMARY KEY (`building_type_id`, `resource_id`),
INDEX `fk_building_required_resource_resource1_idx` (`resource_id` ASC),
CONSTRAINT `fk_building_required_resource_building_type1`
FOREIGN KEY (`building_type_id`)
REFERENCES `mydb`.`building_type` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_building_required_resource_resource1`
FOREIGN KEY (`resource_id`)
REFERENCES `mydb`.`resource` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
I made you the following to split it into steps. It is scripted and doesn't follow OOP so should be relatively easy to understand.
Model Logic
Code: Select all
/**
* Creates the buildings image URL. Ideally make sure $path is coming from a config file.
* @param string $buildingImageName The file name of the building image
* @return string The url for the image of the building
*/
function getBuildingImageURL($buildingType = null, $path = '/images/buildings/', $emptySlotImageImageName = 'available_building_area.png') {
if (null == $buildingType) {
$buildingImageName = $emptySlotImageImageName;
} else {
$buildingImageName = $buildingType['image_file'];
}
return urlencode($path . $buildingImageName);
}
Code: Select all
/**
* Gets the building name by its type
* @param string $buildingType
* @param string $emptySlotName
* @return string A html safe building name
*/
function getBuildingName($buildingType = null, $emptySlotName = 'Empty Slot') {
if (null == $buildingType) {
$buildingName = $emptySlotName;
} else {
$buildingName = $buildingType['name'];
}
return htmlentities($buildingName);
}
Code: Select all
/**
* Gets a building type by its id
* @param integer $id
* @return array The building type with structured by its column names
*/
function getBuildingTypeById($id) {
$buildingTypeQuery = mysql_query("SELECT * FROM `building_type` WHERE `id` = {$building['building_type_id']}");
$buildingType = mysql_fetch_assoc($buildingTypeQuery);
if (empty($buildingType)) {
$buildingType = null;
}
return $buildingType;
}
Code: Select all
/**
* Gets a list of buildings related to the city id given.
* If the amount of buildings does not match the allowed buildings per city.
* The list is populated with empty values
* @param integer $id The id of the city.
* @param integer $buildingPerCity The amount of buildings allowed in a city.
* @return mixed An array of buildings and/or nulls
*/
function getBuildingListByCityId($id, $buildingPerCity = 9) {
$buildingQuery = mysql_query("SELECT * FROM `building` WHERE `city_id` = " . (int)$id ."");
$buildingResult = mysql_fetch_assoc($buildingQuery);
$buildingList = array();
foreach($buildingResult as $building) {
$building['type'] = getBuildingTypeById($building['building_type_id']);
$buildingList[] = $building;
}
$builtBuildingCount = count($buildingList);
if ($builtBuildingCount < $buildingPerCity) {
// create empty slots based on the amount of building records found
// minus the amount of buildings a city is allowed to have
for ($i = 0; $i < ($buildingPerCity - $builtBuildingCount); $i++) {
$buildingList[] = null;
}
}
return $buildingList;
}
Controller
Code: Select all
// controller logic
// Casting to int is quicker than escaping for MySQL
// when passing strings always use mysql_real_escape_string()
$cityId = (int) $_GET['cityId'];
$buildingList = getBuildingListByCityId($cityId);
Helper
Code: Select all
/**
*
* @param array $buildingType
* @return string A HTML image tag
*/
function buildingImageTag($buildingType) {
return '<img src="' . getBuildingImageURL($buildingType) . '" alt="'. getBuildingName($buildingType).'" />';
}
View
Code: Select all
// view logic
foreach ($buildingList as $building) {
echo buildingImageTag($building['type']);
}
I haven't tested this it could be full of typos and errors. I would recommend looking into an MVC framework to help you build this, or at least some form of ORM as the relationships will be hell to program an maintain.