Chciałem zrobić sobie funkcję któraby automatycznie przepisywała strukturę(nie zwartość) tabeli MySQL do tabeli w PHP. Napisałem sobie następujący kod:
<?php
function arrayOfAllFields( $mask, $tableName){
$loop = 0;
$result = describe( $tableName);
$string = '$this->table = Array( '."\n";
$string = $string.( $string[strlen($string)-2] != ' ') ?
( ",\n") : ( null). $loop.' => '.
' Array ( '.( strcasecmp( $mask, 'Field')) ?
(''Field' => ''.$table['Field'].''') : ( null). ( $string[strlen($string)-1] != ' ') ?
( ', ') : ( null).( strcasecmp( $mask, 'Comment')) ?
(''Comment' => ''.$table['Comment'].'' ') : ( null). ')';
$loop++;
}
$string = $string."\n".
' );';
return $string;
}
?>
No i wydawało mi się że będzie chodzić, ale produkt tej funkcji był taki:
<?php
'Comment' => 'hidden'
);
?>
Szukałem błędu, nie znalazłem, parser Eclipsa teżżadnego nie znajdował. Pomyślałem że to przez wstawienie "null" w operatorach trójargumentowych, ale zamiana ich na "' '", albo na "'a'" nic nie dała, nie wiedziałem o co chodzi, więc poświęciłem czytelność kodu i zrobiłem to samo tyle że na IF'ach:
<?php
function arrayOfAllFields( $mask, $tableName){
$loop = 0;
$result = describe( $tableName);
$string = '$this->table = Array( '."\n";
if( $string[strlen($string)-2] != ' ') $string = $string.','."\n"; $string = $string.$loop.' => '.' Array ( ';
if( strcasecmp( $mask, 'Field')) $string = $string.''Field' => ''.$table['Field'].'''; if( $string[strlen($string)-1] != ' ') $string = $string.', '; if( strcasecmp( $mask, 'Comment')) $string = $string.''Comment' => ''.$table['Comment'].'''; $string = $string.')';
$loop++;
}
$string = $string."\n".
' );';
return $string;
}
?>
No i chodzi jak złoto:
<?php
0
=> Array ( 'Field' => 'idPlayer', 'Comment' => ''),1
=> Array ( 'Field' => 'login', 'Comment' => ''),2
=> Array ( 'Field' => 'password', 'Comment' => ''),3
=> Array ( 'Field' => 'lastLogged', 'Comment' => 'hidden') );
?>
tylko nie rozumiem dlaczego tak jest, czy ktoś może mi to wytłumaczyć, albo powiedzieć jak zrobić zeby to samo działało na operatorach trójargumentowych?
Nie wiem czy to potrzebne, ale zamieszczę kod funkcji describe
<?php
function describe( $tableName){
$query = 'SHOW FULL FIELDS FROM `'.$tableName.'` FROM `sphere`;';
return $result;
}
?>