Skip to content

Instantly share code, notes, and snippets.

@Moketronics
Created March 21, 2012 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Moketronics/2151907 to your computer and use it in GitHub Desktop.
Save Moketronics/2151907 to your computer and use it in GitHub Desktop.
Functions for tile product management system
<?php
function get_secondary_colours($iTile_id) {
$query_scolour = "SELECT col.colour_name FROM colour AS col, colour_associations AS col_ass WHERE $iTile_id=col_ass.tile_id AND col.colour_id=col_ass.colour_id AND col_ass.b_primary_colour='0'";
$result_scolour = mysql_query($query_scolour);
$aScolours = array();
if ($result_scolour) {
while ($row = mysql_fetch_array($result_scolour)) {
array_push($aScolours, $row['colour_name']);
}
return implode(', ',$aScolours);
}
}
function get_series($iTile_id) {
$query_series = "SELECT ser.series_name FROM series AS ser, series_associations AS ser_ass WHERE $iTile_id=ser_ass.tile_id AND ser.series_id=ser_ass.series_id";
$result_series = mysql_fetch_row(mysql_query($query_series));
return $result_series[0];
}
function get_sealants($iTile_id) {
$query_sealant = "SELECT sea.sealant_name FROM sealant AS sea, sealant_associations AS sea_ass WHERE $iTile_id=sea_ass.tile_id AND sea.sealant_id=sea_ass.sealant_id";
$result_sealant = mysql_query($query_sealant);
$aSealants = array();
if ($result_sealant) {
while ($row = mysql_fetch_array($result_sealant)) {
array_push($aSealants, $row['sealant_name']);
}
return implode(', ',$aSealants);
}
}
function inch_fraction_conv($iDec_inches) {
// Maybe I should check that it's not somehow a negative or otherwise non-int value being passed in here.
$aInch_conversions = array('47' => '1/32',
'78' => '1/16',
'109' => '3/32',
'141' => '1/8',
'172' => '5/32',
'203' => '3/16',
'234' => '7/32',
'266' => '1/4',
'297' => '9/32',
'328' => '5/16',
'334' => '1/3',
'359' => '11/32',
'391' => '3/8',
'422' => '13/32',
'453' => '7/16',
'484' => '15/32',
'516' => '1/2',
'547' => '17/32',
'578' => '9/16',
'609' => '19/32',
'641' => '5/8',
'668' => '2/3',
'672' => '21/32',
'703' => '11/16',
'734' => '23/32',
'766' => '3/4',
'797' => '25/32',
'828' => '13/16',
'859' => '27/32',
'891' => '7/8',
'922' => '29/32',
'953' => '15/16',
'984' => '31/32',
'other' => NULL);
foreach ($aInch_conversions as $threshold=>$value) {
if (($iDec_inches < $threshold) || ($threshold == 'other')) {
return $value;
}
}
}
function output_inches($iInches) {
if (substr($iInches, -4, 1) == '.') {
$sNominal = floor($iInches);
if (substr($iInches, -3) != '000') {
$sNominal .= ' ' . inch_fraction_conv(substr($iInches, -3));
}
if (substr($sNominal, 0, 2) == '0 ') {
$sNominal = str_replace('0 ', '', $sNominal);
}
return $sNominal;
} else {
return 'Invalid input';
}
}
/*
* This function validates and, if needed, converts inputed inch measurements, returning a decimal string.
* Later on I'll probably want to take this and some other functions and shove them into an includes.php
* file. I'll certainly want to use this elsewhere.
*/
function inch_input_vnc($iNom) {
$iNom = stripslashes(htmlspecialchars_decode(trim($iNom)));
if (substr($iNom, -1) == '"') {
$iNom = substr($iNom, 0, -1);
}
if (preg_match("/^((([1-9]|[1-9][0-9])\s)?)([1-9]|[1-9][0-9])\/([1-9]|[1-9][0-9])\$/", $iNom)) {
if (strpos($iNom, ' ')) {
$aNom = explode(' ', $iNom);
$aNom_fr = explode('/', $aNom[1]);
return number_format(($aNom[0] + ($aNom_fr[0] / $aNom_fr[1])), 3);
} else {
$aNom_fr = explode('/', $iNom);
return number_format(($aNom_fr[0] / $aNom_fr[1]), 3);
}
} elseif ((is_numeric($iNom)) && ($iNom != 0)) {
return number_format($iNom, 3);
} else {
return NULL;
}
}
function order_switch($order = 0, $page) {
// Make column header link variables globally available to output later.
global $sTile_name_h, $sTile_price_h, $sTile_quant_h;
// Check if passed URI already has order value and clean up.
$page = basic_parameter_clean('order', $page);
if ((substr($page, -1) != '?') && (substr($page, -1) != '&')) {
$page .= strstr($page, '?') ? '&' : '?';
}
switch ($order) {
case 1:
$sTile_name_h = '<a href="' . $page . 'order=0">Tile name:</a>';
$sTile_price_h = '<a href="' . $page . 'order=2">Price:</a>';
$sTile_quant_h = '<a href="' . $page . 'order=4">Quantity:</a>';
return 'ti.tile_name DESC ';
break;
case 2:
$sTile_name_h = '<a href="' . $page . 'order=0">Tile name:</a>';
$sTile_price_h = '<a href="' . $page . 'order=3">Price:</a>';
$sTile_quant_h = '<a href="' . $page . 'order=4">Quantity:</a>';
return 'ti.tile_price DESC ';
break;
case 3:
$sTile_name_h = '<a href="' . $page . 'order=0">Tile name:</a>';
$sTile_price_h = '<a href="' . $page . 'order=2">Price:</a>';
$sTile_quant_h = '<a href="' . $page . 'order=4">Quantity:</a>';
return 'ti.tile_price ASC ';
break;
case 4:
$sTile_name_h = '<a href="' . $page . 'order=0">Tile name:</a>';
$sTile_price_h = '<a href="' . $page . 'order=2">Price:</a>';
$sTile_quant_h = '<a href="' . $page . 'order=5">Quantity:</a>';
return 'ti.quantity DESC ';
break;
case 5:
$sTile_name_h = '<a href="' . $page . 'order=0">Tile name:</a>';
$sTile_price_h = '<a href="' . $page . 'order=2">Price:</a>';
$sTile_quant_h = '<a href="' . $page . 'order=4">Quantity:</a>';
return 'ti.quantity ASC ';
break;
default:
$sTile_name_h = '<a href="' . $page . 'order=1">Tile name:</a>';
$sTile_price_h = '<a href="' . $page . 'order=2">Price:</a>';
$sTile_quant_h = '<a href="' . $page . 'order=4">Quantity:</a>';
return 'ti.tile_name ASC ';
}
}
function result_picker($aOptions, $results = 10, $page, $start = NULL, $order = NULL) {
$t = explode('?' ,$page);
$page = $t[0];
$sResults_picker = '<form action="' . $page . '" method="GET" name="resultsnum">' . "\n\t<select name=\"results\">";
foreach ($aOptions as $row) {
$sResults_picker .= "\n\t\t<option value=\"" . $row . "\"";
if ($row == $results) {
$sResults_picker .= ' selected="selected"';
}
$sResults_picker .= ">" . $row . "</option>";
}
if (($order == '') || ($order == NULL)) {
$order = 0;
}
$sResults_picker .= '<input type="hidden" name="order" value="' . $order . '" />';
if (is_numeric($start)) {
$sResults_picker .= '<input type="hidden" name="start" value="' . $start . '" />';
}
$sResults_picker .= "<input type=\"submit\" value=\"Submit\" />\n</form>";
return $sResults_picker;
}
function paginate($results = 10, $start = NULL, $order = 0, $page) {
global $sPrev_link, $sNext_link;
$t = explode('?' ,$page);
$page = $t[0];
if (is_numeric($results) && is_numeric($start)) {
// Get starting point for next set of results, check if something is there, if so form link
$iNext_start = ($start + $results);
$result = mysql_query("SELECT tile_id FROM tiles LIMIT $iNext_start, 1");
if (mysql_num_rows($result) == 1) {
$sNext_link = '<a href="' . $page . '?results=' . $results . '&start=' . $iNext_start . '&order=' . $order . '">Next Page &raquo;</a>';
}
// Is the previous page the first? Generate appropriate link for each situation.
$iPrev_start = ($start - $results);
if ($iPrev_start <= 0) {
$sPrev_link = '<a href="' . $page . '?results=' . $results . '&order=' . $order . '">&laquo; Previous Page</a>';
} else {
$sPrev_link = '<a href="' . $page . '?results=' . $results . '&start=' . $iPrev_start . '&order=' . $order . '">&laquo; Previous Page</a>';
}
return 'LIMIT ' . $start . ', ' . $results . ';';
} else if (($start == NULL) && is_numeric($results)) {
$iNext_start = $results;
$result = mysql_query("SELECT tile_id FROM tiles LIMIT $iNext_start, 1");
if (mysql_num_rows($result) == 1) {
$sNext_link = '<a href="' . $page . '?results=' . $results . '&start=' . $iNext_start . '&order=' . $order . '">Next Page &raquo;</a>';
}
return 'LIMIT ' . $results . ';';
} else {
// Somehow something innapropriate got passed into the function, so just use default settings.
$result = mysql_query("SELECT tile_id FROM tiles LIMIT 10, 1");
if (mysql_num_rows($result) == 1) {
$sNext_link = '<a href="' . $page . '?results=10&start=10&order=' . $order . '">Next Page &raquo;</a>';
}
return 'LIMIT 10;';
}
}
function basic_parameter_clean($par, $page) {
$reg = '/(' . $par . '=)([a-z0-9]*)/';
if (preg_match($reg, $page)) {
$page = preg_replace($reg, '', $page);
if (strstr($page, '?&')) {
if (substr($page, -2) == '?&') {
$page = str_ireplace('?&', '', $page);
} else {
$page = str_ireplace('?&', '?', $page);
}
} else if (strstr($page, '&&')) {
if (substr($page, -2) == '&&') {
$page = str_ireplace('&&', '', $page);
} else {
$page = str_ireplace('&&', '&', $page);
}
}
}
return $page;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment