Arrays & Regexp - Help Requested

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

<?php
// Loop:
echo '<table width="100%" cellspacing="1" cellpadding="2" border="0">' .
"\n";
    foreach ($fields as $field)
    {
     // ROW BEGIN
     echo "\t" . '<tr>' . "\n";
     // NAME OF FIELD
     echo "\t\t" . '<td>`' . $field['field'] . '`: </td>' . "\n";
     // FIELD DATA TYPE
     echo "\t\t" . '<td>' . $field['type'] . '</td>' . "\n";
     // VALUE INPUT
     echo "\t\t" . '<td>';
     $input_type = printByType($field['type'], 'INPUT_TYPE');
     echo '<input type="' . $input_type . '" ';
     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 '</td>' . "\n";
     echo "\t" . '</tr>' . "\n";
    }
    echo '</table>';

?>

The function:

<?php

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!

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux