Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] Różnica między IF, a operatorem trójargumentowym w łączeniu stringów
23kulpamens
post
Post #1





Grupa: Zarejestrowani
Postów: 57
Pomógł: 1
Dołączył: 11.10.2007

Ostrzeżenie: (0%)
-----


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. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
kszychu
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 2 712
Pomógł: 23
Dołączył: 27.10.2003
Skąd: z kontowni

Ostrzeżenie: (0%)
-----


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.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 27.09.2025 - 12:38