Created
February 25, 2012 23:09
-
-
Save Moketronics/1911526 to your computer and use it in GitHub Desktop.
Basically complete tile product entry generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$sPage_title = 'Random Tile Generator'; | |
include ('./includes/header.html'); | |
require_once('../../../mysql_connect.php'); | |
if (isset($_POST['submitted'])) { | |
if (is_numeric($_POST['quantity'])) { | |
$aTile_colours = array('Bianco', 'Gris', 'Beige', 'Nero', 'Oro', 'Negro', 'Noce', 'Sequoia', 'Ivory', 'Sage', 'Azure', 'Argento', 'Chiaro', 'Rojo', 'Rosa', 'Accaico', 'Cream', 'Verde', 'Cielo', 'Mediterannio'); | |
$aTile_descriptors = array('Polished', 'Honed', 'Tumbled', 'Brillo', 'Matte', 'Travertino', 'Frosted', 'Clear', 'Crackle', 'Beveled', 'Rectified', 'Classic'); | |
$aTile_names = array('Lappanto', 'Tivolino', 'Darby', 'Fabulisimo', 'Barocious', 'Pietra', 'Select', 'Bokononist', 'Crema', 'Taborone', 'Copperworks', 'Landscape', 'Chromatic', 'Sun', 'Echo', 'Happle', 'Bagatelle', 'Torchlight', 'Rainforest', 'Diabolo'); | |
$aManufacturers = array(); | |
$result = mysql_query('SELECT manufacturer_id FROM manufacturer'); | |
while ($row = mysql_fetch_array($result)) { | |
array_push($aManufacturers, $row['manufacturer_id']); | |
} | |
$aColours = array(); | |
$result = mysql_query('SELECT colour_id FROM colour'); | |
while ($row = mysql_fetch_array($result)) { | |
array_push($aColours, $row['colour_id']); | |
} | |
$aCountries = array(); | |
$result = mysql_query('SELECT country_id FROM country'); | |
while ($row = mysql_fetch_array($result)) { | |
array_push($aCountries, $row['country_id']); | |
} | |
$aMaterials = array(); | |
$result = mysql_query('SELECT material_id FROM material'); | |
while ($row = mysql_fetch_array($result)) { | |
array_push($aMaterials, $row['material_id']); | |
} | |
$aSealants = array(); | |
$result = mysql_query('SELECT sealant_id FROM sealant'); | |
while ($row = mysql_fetch_array($result)) { | |
array_push($aSealants, $row['sealant_id']); | |
} | |
for ($i = 1; $i <= $_POST['quantity']; $i++) { | |
switch (rand(1, 6)) { | |
case 1: | |
$sTile_name = $aTile_colours[array_rand($aTile_colours)] . ' ' . $aTile_descriptors[array_rand($aTile_descriptors)] . ' ' . $aTile_names[array_rand($aTile_names)]; | |
break; | |
case 2: | |
$sTile_name = $aTile_colours[array_rand($aTile_colours)] . ' ' . $aTile_names[array_rand($aTile_names)] . ' ' . $aTile_descriptors[array_rand($aTile_descriptors)]; | |
break; | |
case 3: | |
$sTile_name = $aTile_descriptors[array_rand($aTile_descriptors)] . ' ' . $aTile_colours[array_rand($aTile_colours)] . ' ' . $aTile_names[array_rand($aTile_names)]; | |
break; | |
case 4: | |
$sTile_name = $aTile_descriptors[array_rand($aTile_descriptors)] . ' ' . $aTile_names[array_rand($aTile_names)] . ' ' . $aTile_colours[array_rand($aTile_colours)]; | |
break; | |
case 5: | |
$sTile_name = $aTile_names[array_rand($aTile_names)] . ' ' . $aTile_descriptors[array_rand($aTile_descriptors)] . ' ' . $aTile_colours[array_rand($aTile_colours)]; | |
break; | |
default: | |
$sTile_name = $aTile_names[array_rand($aTile_names)] . ' ' . $aTile_colours[array_rand($aTile_colours)] . ' ' . $aTile_descriptors[array_rand($aTile_descriptors)]; | |
} | |
$query = "SELECT tile_name FROM tiles WHERE tile_name = '$sTile_name'"; | |
if (!$result = mysql_fetch_array(@mysql_query($query))) { | |
$iTile_price = rand(175, 9500); | |
$iQuantity = rand(15, 10000); | |
$iOld_lot_qty = ($iQuantity - rand(0, (round($iQuantity / 2)))); | |
$iNom_height = (rand(1, 48) + (rand(1, 32) / 32)); | |
$iNom_width = (rand(1, 48) + (rand(1, 32) / 32)); | |
$iExact_height = round($iNom_height * 25.4); | |
$iExact_width = round($iNom_width * 25.4); | |
$iNom_thick = (rand(8, 20) / 32); | |
$iBox_tile_count = (rand(0, 50) * 5); | |
if (rand(0, 4) == 0) { | |
$iBox_tile_count = NULL; // Can be null, so this way ~20% of the time it will be sometimes | |
} | |
$iLocal_supply = rand(0, 3); | |
$iRating = rand(0,9); | |
if (rand(0,2) > 1) { | |
$sTile_notes = 'Some of the tiles will have notes like this. They can be pretty long! Here is a random number unlikely to be over 9000: ' . rand(0,9001); | |
} | |
$iPrimary_colour = $aColours[array_rand($aColours)]; | |
switch (rand(1,4)) { | |
case 1: | |
$aSecondary_colours = array(); // No secondary colour, but I'm making the array just in case. | |
break; | |
case 2: | |
$aSecondary_colours = array($aColours[array_rand($aColours)]); | |
$b = 0; | |
while ($iPrimary_colour == $aSecondary_colours[0]) { | |
$aSecondary_colours[0] = $aColours[array_rand($aColours)]; | |
$b++; | |
if ($b >= 5) { // limits tries at getting a non-duplicate value, doubling to suppress the number tiles with extra colours. | |
unset($aSecondary_colours[0]); | |
break; | |
} | |
} | |
break; | |
case 3: | |
$aSecondary_colours = array($aColours[array_rand($aColours)], | |
$aColours[array_rand($aColours)]); | |
$b = 0; | |
while ($iPrimary_colour == $aSecondary_colours[0]) { | |
$aSecondary_colours[0] = $aColours[array_rand($aColours)]; | |
$b++; | |
if ($b >= 5) { | |
unset($aSecondary_colours[0]); | |
break; | |
} | |
} | |
$b = 0; | |
while (($iPrimary_colour == $aSecondary_colours[1]) || ($aSecondary_colours[0] == $aSecondary_colours[1])) { | |
$aSecondary_colours[1] = $aColours[array_rand($aColours)]; | |
$b++; | |
if ($b >= 5) { | |
unset($aSecondary_colours[1]); | |
break; | |
} | |
} | |
break; | |
case 4: | |
$aSecondary_colours = array($aColours[array_rand($aColours)], | |
$aColours[array_rand($aColours)], | |
$aColours[array_rand($aColours)]); | |
$b = 0; | |
while ($iPrimary_colour == $aSecondary_colours[0]) { | |
$aSecondary_colours[1] = $aColours[array_rand($aColours)]; | |
$b++; | |
if ($b >= 5) { | |
unset($aSecondary_colours[0]); | |
break; | |
} | |
} | |
$b = 0; | |
while (($iPrimary_colour == $aSecondary_colours[1]) || ($iPrimary_colour == $aSecondary_colours[2]) || ($aSecondary_colours[0] == $aSecondary_colours[1]) || ($aSecondary_colours[0] == $aSecondary_colours[2]) || ($aSecondary_colours[1] == $aSecondary_colours[2])) { | |
$aSecondary_colours[1] = $aColours[array_rand($aColours)]; | |
$aSecondary_colours[2] = $aColours[array_rand($aColours)]; | |
$b++; | |
if ($b >= 10) { | |
unset($aSecondary_colours[1]); | |
unset($aSecondary_colours[2]); | |
break; | |
} | |
} | |
} | |
$iManufacturers = $aManufacturers[array_rand($aManufacturers)]; | |
$iCountry = $aCountries[array_rand($aCountries)]; | |
$iMaterials = $aMaterials[array_rand($aMaterials)]; | |
if (rand(0,1) == 0) { | |
$iSealants = $aSealants[array_rand($aSealants)]; | |
} else { | |
$iSealants = NULL; | |
} | |
$query = "SELECT series_id FROM series WHERE manufacturer_id='$iMaterials'"; | |
$result = mysql_query($query); | |
$iSeries = NULL; | |
if ($result) { | |
$aSeries = array(); | |
while ($row = mysql_fetch_array($result)) { | |
array_push($aSeries, $row['series_id']); | |
} | |
//Suppresses adding a series at a rate inversely prelated to the number of series associated with the tile. | |
if (rand(0, count($aSeries)) != 0) { | |
$iSeries = $aSeries[array_rand($aSeries)]; | |
} | |
} | |
$query = "INSERT INTO tiles (tile_name, tile_price, quantity, old_lot_qty, nom_height, nom_width, nom_thick, exact_height, exact_width, box_tile_count, local_supply, rating, tile_notes) VALUES ( | |
'$sTile_name', | |
'$iTile_price', | |
'$iQuantity', | |
'$iOld_lot_qty', | |
'$iNom_height', | |
'$iNom_width', | |
'$iNom_thick', | |
'$iExact_height', | |
'$iExact_width', | |
'$iBox_tile_count', | |
'$iLocal_supply', | |
'$iRating', | |
'$sTile_notes' | |
);"; | |
$result_tiles = @mysql_query($query); | |
if ($result_tiles) { | |
$query = "SELECT tile_id FROM tiles WHERE tile_name = '$sTile_name'"; | |
$iTile_id = mysql_fetch_array(mysql_query($query)); | |
$iTile_id = $iTile_id['tile_id']; | |
$query = "INSERT INTO material_associations (tile_id, material_id) VALUES ('$iTile_id', '$iMaterials');"; | |
$result_material = mysql_query($query); | |
$query = "INSERT INTO country_associations (tile_id, country_id) VALUES ('$iTile_id', '$iCountry');"; | |
$result_country = mysql_query($query); | |
$query = "INSERT INTO manufacturer_associations (tile_id, manufacturer_id) VALUES ('$iTile_id', '$iManufacturers');"; | |
$result_manufacturer = mysql_query($query); | |
if ($iSealants != NULL) { | |
$query = "INSERT INTO sealant_associations (tile_id, sealant_id) VALUES ('$iTile_id', '$iSealants');"; | |
$result_manufacturer = mysql_query($query); | |
} | |
$query = "INSERT INTO colour_associations (tile_id, colour_id, b_primary_colour) VALUES ('$iTile_id', '$iPrimary_colour', 1);"; | |
$result_primary_colour = mysql_query($query); | |
// insert series associations here. | |
if ($iSeries != NULL) { | |
$query = "INSERT INTO seies_associations (tile_id, series_id) VALUES ('$iTile_id', '$iSeries')"; | |
} | |
if (isset($aSecondary_colours[0])) { | |
for ($k = 0; $k < count($aSecondary_colours); $k++) { | |
$iScolour = current($aSecondary_colours); | |
$query = "INSERT INTO colour_associations (tile_id, colour_id, b_primary_colour) VALUES ('$iTile_id', '$iScolour', 0);"; | |
$result_country = mysql_query($query); | |
next($aSecondary_colours); | |
} | |
} | |
} else { | |
// delete the entry, print an error message and quit. But I don't think I really have to do this for the generator. | |
} | |
} | |
} | |
} else { | |
echo '<p class="error">Please input a valid tile quantity less than 1000</p>'; | |
} | |
} | |
if (isset($_POST['clear_it'])) { | |
mysql_query("TRUNCATE TABLE tiles;"); | |
mysql_query("TRUNCATE TABLE colour_associations;"); | |
mysql_query("TRUNCATE TABLE country_associations;"); | |
mysql_query("TRUNCATE TABLE manufacturer_associations;"); | |
mysql_query("TRUNCATE TABLE material_associations;"); | |
mysql_query("TRUNCATE TABLE sealant_associations;"); | |
mysql_query("TRUNCATE TABLE series_associations;"); | |
} | |
$count_query = "SELECT tile_id FROM tiles"; | |
$count_result = mysql_num_rows(mysql_query($count_query)); | |
mysql_close(); // Done everything! | |
?> | |
<p>Current total tile records in database: <?php echo $count_result; ?></p> | |
<p>This fills up the tile database with a bunch of randomly generated tiles.</p> | |
<form action="generator.php" method="POST"> | |
<p>Number of Tiles: <input type="text" name="quantity" size="3" maxlength="3" /></p> | |
<p><input type="submit" name="submitted" value="Generate!" /></p> | |
</form> | |
<form action="generator.php" method="POST"> | |
<p><input type="submit" name="clear_it" value="*WARNING* Clear the database! *NO CONFIRMATION*" /></p> | |
</form> | |
<?php | |
include ('./includes/footer.html'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment