Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php] Różnica między IF, a operatorem trójargumentowym w łączeniu stringów
23kulpamens
post 25.02.2008, 14:41:56
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. ?>


--------------------
Ta sygnaturka to lekkie przegięcie. To poważne forum. Pomijam już fakt naruszenia regulaminu. Usuwam /~nospor/ szkoda :(
Go to the top of the page
+Quote Post
kszychu
post 25.02.2008, 14:58:01
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.


--------------------
"Coś się kończy, coś się zaczyna." Andrzej Sapkowski
Go to the top of the page
+Quote Post
nevt
post 25.02.2008, 15:02:33
Post #3





Grupa: Przyjaciele php.pl
Postów: 1 595
Pomógł: 282
Dołączył: 24.09.2007
Skąd: Reda, Pomorskie.

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


ź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ć...


--------------------

-
Oh no, my young coder. You will find that it is you who are mistaken, about a great many things... -
Go to the top of the page
+Quote Post
23kulpamens
post 25.02.2008, 15:03:18
Post #4





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

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


dzięki smile.gif nawiasy załatwiły sprawę. Nie mogłem się doszukać, a taki prosty błąd. Wstyd winksmiley.jpg


--------------------
Ta sygnaturka to lekkie przegięcie. To poważne forum. Pomijam już fakt naruszenia regulaminu. Usuwam /~nospor/ szkoda :(
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 8.06.2024 - 19:44