Arrays & Regexp - Help Requested

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 '

' .
"\n";
foreach ($fields as $field)
{
// ROW BEGIN
echo "\t" . '' . "\n";
// NAME OF FIELD
echo "\t\t" . '' . "\n";
// FIELD DATA TYPE
echo "\t\t" . '' . "\n";
// VALUE INPUT
echo "\t\t" . '' . "\n";
echo "\t" . '' . "\n";
}
echo '
`' . $field['field'] . '`: ' . $field['type'] . '';
$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 '
';

?>

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--

Re: Arrays & Regexp - Help Requested

am 02.01.2010 00:47:00 von Mari Masuda

I think the problem is here:

echo '
[...snip...]

elseif ($input_type == 'textarea')
{
echo 'rows=3D"7" cols=3D"30" ';
echo 'value=3D"';
if ($field['null'] == 'YES') // CAN BE NULL?
{
echo 'NULL';
}
echo '" ';
}

because to create a textarea, the HTML is . It looks like your code is trying =
to make a textarea that starts with work. See http://www.w3.org/TR/html401/interact/forms.html#h-17.7 for =
how to use textarea.


On Jan 1, 2010, at 3:38 PM, Allen McCabe wrote:

> echo ' > if ($input_type == 'text')
> {
> echo 'size=3D"';
> $length =3D printByType($field['type'], 'INPUT_LENGTH');
> echo $length;
> echo '" ';
> echo 'value=3D"';
> if ($field['null'] == 'YES') // CAN BE NULL?
> {
> echo 'NULL';
> }
> echo '" ';
> }
> elseif ($input_type == 'textarea')
> {
> echo 'rows=3D"7" cols=3D"30" ';
> echo 'value=3D"';
> if ($field['null'] == 'YES') // CAN BE NULL?
> {
> echo 'NULL';
> }
> echo '" ';
> }


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php