Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> odmiana liczebników
Cysiaczek
post
Post #1





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




Witam.

Szukam skryptu odmiany liczebników. Jeśli jakaś dobra duszyczka dysponuje, to będe wdzięczny, jak nie, to trudno - napiszę sam ;|

Przykład:
1 produkt
12 produktów
34 produkty

Pozdrawiam.


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
tomeksobczak
post
Post #2





Grupa: Zarejestrowani
Postów: 139
Pomógł: 10
Dołączył: 6.07.2007
Skąd: opole

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


nie wiem czy to mozna nazwac az skryptem

  1. <?php
  2. $koniec = substr( (string) $ilosc, -);
  3. if( $ilosc == 1 )
  4. { PRODUKT }
  5. elseif( $ilosc > 20 && in_array( $koniec, array( '2', '3', '4' ) )
  6. { PRODUKTY }
  7. else
  8. { PRODUKTÓW }
  9. ?>


--------------------
Go to the top of the page
+Quote Post
Cysiaczek
post
Post #3





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




Dzięki Ci.
Mimo, że podałeś błędną w implementację, to 90% założeń było dobre.
Poprawione wygląda i działa tak.
  1. <?php
  2. if($ilosc == 1 )
  3. {
  4. $nPart=' produkt';
  5. }
  6. elseif($ilosc <= 20 && $ilosc>4)
  7. {
  8. $nPart=' produktów';
  9. }
  10. elseif(in_array($koniec, array( 2, 3, 4)))
  11. {
  12. $nPart=' produkty';
  13. }
  14. else
  15. {
  16. $nPart=' produktów';
  17. }
  18. ?>


Pozdrawiam.


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
tomeksobczak
post
Post #4





Grupa: Zarejestrowani
Postów: 139
Pomógł: 10
Dołączył: 6.07.2007
Skąd: opole

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


&pisałem z pamięci ;]

ale Twoja poprawka tez nie jest poprawna w 100%
[ 112 -> 112 produkty ] - problem pojawia sie po kazdej setce tak więc

  1. <?php
  2. $koniec  = substr( (string) $ilosc, -);
  3. $koniec2 = (;$ilosc > 9 ) ? substr( (string) $ilosc, -) : 1;
  4. if( $ilosc == 1 )
  5. { PRODUKT }
  6. elseif( ( (int) $koniec2 > 20 ) && in_array( $koniec, array( '2', '3', '4' ) )
  7. { PRODUKTY }
  8. else
  9. { PRODUKTÓW }
  10. ?>


teraz juz chyba powinno dzialac bez problemów


P.S.

po chwili widze ze moje znow nie dziala przy 102, 103, 104 ;]

Ten post edytował tomeksobczak 21.09.2007, 07:27:15


--------------------
Go to the top of the page
+Quote Post
Cosi*
post
Post #5





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 9.07.2007
Skąd: Dąbrowa Górnicza

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


Panowie,
Najpierw modulo 100, a potem reguła:
Kod
R = 1 -> produkt
(R mod 10 = 2,3,4) & (R != 12,13,14) -> produkty
else -> produktów

Tylko zaimplementować i powinno działać biggrin.gif
Pozdrawiam

PS. Poprawka smile.gif Warunek R = 1 powinien się znaleźć przed modulo 100.

Ten post edytował Cosi* 21.09.2007, 11:28:57


--------------------
http://cosi.com.pl
<? $kate or die ?>
Go to the top of the page
+Quote Post
eteropczyk
post
Post #6





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 27.06.2007

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


dla potomnych smile.gif
  1.  
  2. function sklep_koszyk_odmiana($ile)
  3. {
  4. $r1=$ile % 100;
  5. if ($r1 == 1 && $ile < 100)
  6. {
  7. $p='produkt'; $z='znajduje';
  8. }
  9. else
  10. {
  11. $r2=$r1 % 10;
  12. if (($r2 > 1 && $r2 < 5) && ($r1 < 12 || $r1 > 14))
  13. {
  14. $p='produkty'; $z='znajdują';
  15. }
  16. else
  17. {
  18. $p='produktów'; $z='znajduje';
  19. }
  20. }
  21. return array($p,$z);
  22. }
  23.  



test:

  1.  
  2. for ($i=0;$i<300;$i++)
  3. {
  4. list($p,$z)=sklep_koszyk_odmiana($i);
  5. echo 'W koszyku '.$z.' się '.$i.' '.$p.'<br />'."\n";
  6. }
  7.  
Powód edycji: [thek]: Nie uprawiaj archeologii! ostatni post z 2007r. Pogięło Cię by to odkopać?
Go to the top of the page
+Quote Post
Crozin
post
Post #7





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


Od takich rzeczy to jest http://pl.php.net/manual/en/class.messageformatter.php
Go to the top of the page
+Quote Post
Fifi209
post
Post #8





Grupa: Zarejestrowani
Postów: 4 655
Pomógł: 556
Dołączył: 17.03.2009
Skąd: Katowice

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


Crozin... Ten temat jest z 2007 roku. ;]

Cytat
This extension is bundled with PHP as of PHP version 5.3.0.


A od kiedy mamy php 5.3 ?


--------------------
Zainteresowania: C#, PHP, JS, SQL, AJAX, XML, C dla AVR
Chętnie pomogę, lecz zanim napiszesz: Wujek Google , Manual PHP
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 Aktualny czas: 19.08.2025 - 11:35