Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] Różnica między IF, a operatorem trójargumentowym w łączeniu stringów
Forum PHP.pl > Forum > Przedszkole
23kulpamens
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:
  1. <?php
  2. function arrayOfAllFields( $mask, $tableName){
  3. $loop = 0;
  4. $result = describe( $tableName);
  5. $string = '$this->table = Array( '."\n";
  6. while( $table = mysql_fetch_array( $result )){
  7. $string = $string.( $string[strlen($string)-2] != ' ') ? ( ",\n") : ( null).
  8. $loop.' => '.
  9. ' Array ( '.( strcasecmp( $mask, 'Field')) ? (''Field' => ''.$table['Field'].''') : ( null).
  10. ( $string[strlen($string)-1] != ' ') ? ( ', ') : ( null).( strcasecmp( $mask, 'Comment')) ? (''Comment' => ''.$table['Comment'].'' ') : ( null).
  11. ')';
  12. $loop++;
  13. }
  14. print $string;
  15. $string = $string."\n".
  16. '  );';
  17. return $string;
  18. }
  19. ?>

No i wydawało mi się że będzie chodzić, ale produkt tej funkcji był taki:
  1. <?php
  2. 'Comment' => 'hidden' 
  3.  );
  4. ?>

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:
  1. <?php
  2. function arrayOfAllFields( $mask, $tableName){
  3. $loop = 0;
  4. $result = describe( $tableName);
  5. $string = '$this->table = Array( '."\n";
  6. while( $table = mysql_fetch_array( $result )){
  7. if( $string[strlen($string)-2] != ' ') $string = $string.','."\n";
  8. $string = $string.$loop.' => '.' Array ( ';
  9. if( strcasecmp( $mask, 'Field')) $string = $string.''Field' => ''.$table['Field'].''';
  10. if( $string[strlen($string)-1] != ' ') $string = $string.', ';
  11. if( strcasecmp( $mask, 'Comment')) $string = $string.''Comment' => ''.$table['Comment'].''';
  12. $string = $string.')';
  13. $loop++;
  14. }
  15. print $string;
  16. $string = $string."\n".
  17. '  );';
  18. return $string;
  19. }
  20. ?>

No i chodzi jak złoto:
  1. <?php
  2. $this->table = Array( 
  3. => Array ( 'Field' => 'idPlayer', 'Comment' => ''),
  4. => Array ( 'Field' => 'login', 'Comment' => ''),
  5. => Array ( 'Field' => 'password', 'Comment' => ''),
  6. => Array ( 'Field' => 'lastLogged', 'Comment' => 'hidden')
  7.  );
  8. ?>

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
  1. <?php
  2. function describe( $tableName){
  3. $query = 'SHOW FULL FIELDS FROM `'.$tableName.'` FROM `sphere`;';
  4. $result = mysql_query ($query);
  5. return $result;
  6. }
  7. ?>
kszychu
Tak na pierwszy rzut oka masz niepoprawną konstrukcję operatora. Tzn. poprawną składniowo, ale niepoprawną logicznie.
O co chodzi.
(a ? b : c).(d ? e : f) u Ciebie wygląda tak: a ? b : c.d ? e : f
Rozumiesz?
Chodzi o to, że do instrukcji w wyniku niespełnienia warunku doklejany jest następny warunek, czyli interpreter traktuje to tak: (a ? b : c.d) (? e : f)
W drugim nawiasie warunek warunek jest false, więc wykona się f.
nevt
źle poustawiałeś nawiasy... i nie używasz operatora .= ...
  1. <?php
  2. $string .= ($string[strlen($string)-2] == ' ' ? '' : ",\n").
  3. "$loop => Array ( ".
  4. (strcasecmp( $mask, 'Field') ? "'Field' => '".$table['Field']."'" : '').
  5. ($string[strlen($string)-1] == ' ' ? '' : ', ').
  6. (strcasecmp($mask, 'Comment') ? "'Comment' => '".$table['Comment']."'" : '').
  7. ')';
  8. ?>

pisałem bez możliwości sprawdzenia więc wybacz jeśli wkradła się jakaś literówka, ale z grubsza powinno działać...
23kulpamens
dzięki smile.gif nawiasy załatwiły sprawę. Nie mogłem się doszukać, a taki prosty błąd. Wstyd winksmiley.jpg
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.