[php][mysql]Problem z explode |
[php][mysql]Problem z explode |
4.04.2008, 18:28:42
Post
#1
|
|
Grupa: Zarejestrowani Postów: 10 Pomógł: 0 Dołączył: 14.10.2007 Skąd: Olsztyn Ostrzeżenie: (0%) |
Witam, mam problem z funkcją explode. Dany mam poniższy kod:
Kod $BuildQueue = $b_hangar_id; $BuildArray = explode (";", $BuildQueue); for ($QElement = 0; $QElement < count($BuildArray); $QElement++) { $ElmentArray = explode (",", $BuildArray[$QElement] ); if ($ElmentArray[502] != 0) { $Missiles[502] += $ElmentArray[502]; } elseif ($ElmentArray[503] != 0) { $Missiles[503] += $ElmentArray[503]; } } Gdzie "$b_hangar_id" = "502,6;502,5" Missiles[502] i Missiles[503] = 3 Chodzi głównie o to, żeby dane z $b_hangar_id były dodawane to Missiles. Czyli np. jeśli w b_hangar_id znajdują się 2 wartości "502,5;502,7" to liczba po przecinku to ilość jaką trzeba dodać. Czyli łącznie 12 (7+5). Niestety $ElementArray wyrzuca mi pusty wynik i nie wiem co jest źle. |
|
|
4.04.2008, 18:44:08
Post
#2
|
|
Grupa: Zarejestrowani Postów: 220 Pomógł: 10 Dołączył: 23.08.2005 Skąd: Łódź Ostrzeżenie: (0%) |
Spróbuj tak:
Ten post edytował vtuner 4.04.2008, 18:44:16 |
|
|
-Potasiak- |
4.04.2008, 18:54:48
Post
#3
|
Goście |
Nie wiem jak to wytłumaczyć słowami, ale dam przykład:
Ten kod robi takie coś: 1. Definiuje zmienną "502,7;503,2" 2. Tworzy z tego tablicę oddzielając części znakami ; 3. rozpoczyna pętlę 4. części utworzonej tablicy są jeszcze raz rozdzielane (przecinkami) 5. powstaje takie coś: array(0=>array(0=>'502',1=>'7'),1=>array(0=>'503',1=>'2')); 6. Teraz jeśli liczba po przecinku nie jest równa 0... 7. ...do tablicy missles, gdzie numer elementu tablicy wynosi liczbę sprzed przecinka dodawana jest wartość zza przecinka. Wiem że troszkę zawile, ale myślę że połapiesz się w przykładzie. |
|
|
4.04.2008, 18:55:36
Post
#4
|
|
Grupa: Zarejestrowani Postów: 10 Pomógł: 0 Dołączył: 14.10.2007 Skąd: Olsztyn Ostrzeżenie: (0%) |
Hmm.. tylko, że w $b_hangar_id może być parę różnych typów (bo 502 to typ danego elementu mojej strony).
np. $b_hangar_id = "502,6;502,5;507,5;121,3" I chodzi mi o to, żeby wybrać i dodać tylko te wyniki gdzie jest 502, czyli w tym wypadku 6+5. Pozostawiając 5 i 3 nienaruszone. |
|
|
4.04.2008, 19:06:19
Post
#5
|
|
Grupa: Przyjaciele php.pl Postów: 1 595 Pomógł: 282 Dołączył: 24.09.2007 Skąd: Reda, Pomorskie. Ostrzeżenie: (0%) |
Ten post edytował nevt 4.04.2008, 19:06:54 -------------------- - Oh no, my young coder. You will find that it is you who are mistaken, about a great many things... - |
|
|
4.04.2008, 19:12:59
Post
#6
|
|
Grupa: Zarejestrowani Postów: 10 Pomógł: 0 Dołączył: 14.10.2007 Skąd: Olsztyn Ostrzeżenie: (0%) |
Już sobie poradziłem, ale to dzięki vtunerowi, on mnie naprowadził Zmieniłem:
Kod if ($ElmentArray[502] != 0) { $Missiles[502] += $ElmentArray[502]; } elseif ($ElmentArray[503] != 0) { $Missiles[503] += $ElmentArray[503]; } na to: Kod if ($ElmentArray[0] == '502') {
$Missiles[502] += $ElmentArray[1]; } elseif ($ElmentArray[0] == '503') { $Missiles[503] += $ElmentArray[1]; } Ten post edytował Sebcioo 4.04.2008, 19:27:15 |
|
|
7.04.2008, 18:10:26
Post
#7
|
|
Grupa: Zarejestrowani Postów: 220 Pomógł: 10 Dołączył: 23.08.2005 Skąd: Łódź Ostrzeżenie: (0%) |
Kod if ($ElmentArray[0] == '502') { $Missiles[502] += $ElmentArray[1]; } elseif ($ElmentArray[0] == '503') { $Missiles[503] += $ElmentArray[1]; } Ale dzięki temu Twój kod nie jest odporny na zmiany, bo może na miesiąc będziesz chciał dodać kolejny typ np. 504 i już musisz zmieniać ten kod, natomiast mój sposób jest uniwersalny niezależnie od typu. |
|
|
Wersja Lo-Fi | Aktualny czas: 23.09.2024 - 04:02 |