public function itemsByColor(Color $color) { $query = 'SELECT * FROM ItemColor JOIN Item ON Item.itemID = ItemColor.itemID WHERE red between :minRed and :maxRed And green between :minGreen and :maxGreen AND blue between :minBlue and :maxBlue'; $statement = $this->db->prepare($query); $statement->bindValue('redRangeBottom', $color->getRed() - self::SEARCH_RANGE); $statement->execute(array('minRed' => $color->getRed() - self::SEARCH_RANGE, 'maxRed' => $color->getRed() + self::SEARCH_RANGE, 'minGreen' => $color->getGreen() - self::SEARCH_RANGE, 'maxGreen' => $color->getGreen() + self::SEARCH_RANGE, 'minBlue' => $color->getBlue() - self::SEARCH_RANGE, 'maxBlue' => $color->getBlue() + self::SEARCH_RANGE)); return $statement->fetchAll(PDO::FETCH_ASSOC); }
require_once '../Color/Color.php'; use RosyMoth\Color\Color; $db = RosyMoth\Database\DB::getAdminInstance(); $truncateeColors = $db->prepare('TRUNCATE TABLE ItemColor'); $truncateeColors->execute(); $truncateItems = $db->prepare('TRUNCATE TABLE Item'); $truncateItems->execute(); $items = json_decode(file_get_contents('dummydata.json')); foreach ($items as $item) { foreach ($item->subtypes as $subtype) { $itemStatement = $db->prepare('INSERT INTO Item (vendorID, vendorItemID, displayName, subtype) ' . 'VALUES (1, :vendorItemID, :displayName, :subtype)'); $itemStatement->bindValue('vendorItemID', $item->vendorItemID); $itemStatement->bindValue('displayName', $item->displayName); $itemStatement->bindValue('subtype', $subtype->subtype); $itemStatement->execute(); $itemID = $db->lastInsertId(); foreach ($subtype->colors as $hexColor) { $color = Color::createFromHex($hexColor); $colorStatement = $db->prepare('INSERT INTO ItemColor (itemID, red, green, blue) ' . 'VALUES (:itemID, :red, :green, :blue)'); $colorStatement->bindValue('itemID', $itemID); $colorStatement->bindValue('red', $color->getRed()); $colorStatement->bindValue('green', $color->getGreen()); $colorStatement->bindValue('blue', $color->getBlue()); $colorStatement->execute(); } } } //$db = new PDO('mysql:host=localhost;dbname=mareofni_rosymoth', 'mareofni_rmAdmin', 'rosymoth100'); //$statement = $db->prepare('INSERT INTO Vendor (displayName) VALUES (:name)'); //$statement->bindValue('name', 'Fabric.com'); //$statement->execute();
<?php require_once '../Color/Color.php'; require_once '../Database/DB.php'; require_once '../Database/Finder.php'; use RosyMoth\Color\Color; $color = Color::createFromHex($_GET['color']); $finder = new \RosyMoth\Database\Finder(); $fabrics = $finder->itemsByColor($color); echo json_encode($fabrics);