Arrays & Regexp - Help Requested
am 02.01.2010 00:38:36 von Allen McCabe--00504502b1c1d66c69047c22dee6
Content-Type: text/plain; charset=ISO-8859-1
Happy New Year, here's my first question of the year (and it's only 15 hours
into the year!).
I am creating a small database management tool for my a website (my work IP
blocks my access to PhpMyAdmin) and I don't want to install any additional
software.
I am working on adding rows and need to format the input boxes properly (ie.
VARCHAR needs an text input, TEXT needs a textarea input). Using a mysql
query I can determine the data types for each field. I have a function that
uses this information to return datatype specific variables. For example
$field['field'] may equal int(6) or varchar(256).
The code is a bit extensive, but it's necessary to explain what's going on.
// Loop:
echo '
`' . $field['field'] . '`: | ' . "\n";' . $field['type'] . ' | ' . "\n";'; $input_type = printByType($field['type'], 'INPUT_TYPE'); echo ' if ($input_type == 'text') { echo 'size="'; $length = printByType($field['type'], 'INPUT_LENGTH'); echo $length; echo '" '; echo 'value="'; if ($field['null'] == 'YES') // CAN BE NULL? { echo 'NULL'; } echo '" '; } elseif ($input_type == 'textarea') { echo 'rows="7" cols="30" '; echo 'value="'; if ($field['null'] == 'YES') // CAN BE NULL? { echo 'NULL'; } echo '" '; } echo 'name="value[]" id="value[]" onfocus="if(this.value==\'NULL\')this.value=\'\';" />'; echo ' | ' . "\n";
?>
The function:
function printByType($string, $mode)
{
(string) $string;
$lengths = array(
'VARCHAR' => 10
, 'TINYINT' => 1
, 'TEXT' => 10
, 'DATE' => 7
, 'SMALLINT' => 1
, 'MEDIUMINT' => 2
, 'INT' => 2
, 'BIGINT' => 3
, 'FLOAT' => 4
, 'DOUBLE' => 4
, 'DECIMAL' => 4
, 'DATETIME' => 10
, 'TIMESTAMP' => 10
, 'TIME' => 7
, 'YEAR' => 4
, 'CHAR' => 7
, 'TINYBLOB' => 10
, 'TINYTEXT' => 10
, 'BLOB' => 10
, 'MEDIUMBLOB' => 10
, 'MEDIUMTEXT' => 10
, 'LONGBLOB' => 10
, 'LONGTEXT' => 10
, 'ENUM' => 5
, 'SET' => 5
, 'BIT' => 2
, 'BOOL' => 1
, 'BINARY' => 10
, 'VARBINARY' => 10);
$types = array(
'VARCHAR' => 'text'
, 'TINYINT' => 'text'
, 'TEXT' => 'textarea'
, 'DATE' => 'text'
, 'SMALLINT' => 'text'
, 'MEDIUMINT' => 'text'
, 'INT' => 'text'
, 'BIGINT' => 'text'
, 'FLOAT' => 'text'
, 'DOUBLE' => 'text'
, 'DECIMAL' => 'text'
, 'DATETIME' => 'text'
, 'TIMESTAMP' => 'text'
, 'TIME' => 'text'
, 'YEAR' => 'text'
, 'CHAR' => 'text'
, 'TINYBLOB' => 'textarea'
, 'TINYTEXT' => 'textarea'
, 'BLOB' => 'textarea'
, 'MEDIUMBLOB' => 'textarea'
, 'MEDIUMTEXT' => 'textarea'
, 'LONGBLOB' => 'textarea'
, 'LONGTEXT' => 'textarea'
, 'ENUM' => 'text'
, 'SET' => 'text'
, 'BIT' => 'text'
, 'BOOL' => 'text'
, 'BINARY' => 'text'
, 'VARBINARY' => 'text');
switch ($mode)
{
case 'INPUT_LENGTH':
foreach ($lengths as $key => $val)
{
(string) $key;
(int) $val;
// DETERMINE LENGTH VALUE eg. int(6) GETS 6
preg_match('#\((.*?)\)#', $string, $match);
(int) $length_value = $match[1];
// SEARCH
$regex = "/" . strtolower($key) . "/i";
$found = preg_match($regex, $string);
if ($found !== false)
{
// DETERMINE ADD INTEGER eg. If the length_value is long enough,
determine number to increase html input length
switch ($length_value)
{
case ($length_value <= 7):
return $length_value;
break;
case ($length_value > 7 && $length_value < 15):
return $val += ($length_value/2);
break;
case ($length_value > 14 && $length_value < 101):
$result = ($length_value / 5);
$divide = ceil($result);
return $val += $divide;
break;
case ($length_value > 100):
return 40;
break;
default:
return 7;
break;
}
return $val;
}
else
{
return 7; // default value
}
}
break;
case 'INPUT_TYPE':
foreach ($types as $key => $val)
{
(string) $val;
(string) $key;
// SEARCH
$regex = "/" . strtolower($key) . "/i";
$found = preg_match($regex, $string);
if ($found === false)
{
return 'text'; // default value
}
else
{
return $val;
}
}
break;
}
}
?>
The first part of the function (the first switch case) works, and the text
fields are variable in length, but even the fields with a TEXT datatype is
printing out a text box instead of a textarea. Can anyone see why this is
happening?
Thanks!
--00504502b1c1d66c69047c22dee6--