Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php][mysql]Problem z explode
Sebcioo
post 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.
Go to the top of the page
+Quote Post
vtuner
post 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:

  1. <?php
  2. $BuildQueue = $b_hangar_id;
  3. $BuildArray = explode (";", $BuildQueue);
  4. for ($QElement = 0; $QElement < count($BuildArray); $QElement++) 
  5. {
  6. $ElmentArray[$QElement] = explode (",", $BuildArray[$QElement] );
  7. $Missiles[$ElmentArray[$QElement][0]] += $ElmentArray[$QElement][1];
  8. }
  9. ?>


Ten post edytował vtuner 4.04.2008, 18:44:16
Go to the top of the page
+Quote Post
-Potasiak-
post 4.04.2008, 18:54:48
Post #3





Goście







Nie wiem jak to wytłumaczyć słowami, ale dam przykład:
  1. <?php
  2. $zmienna1 = "502,7;503,2";
  3. $tablica1 = explode(";", $zmienna1);
  4. for ($element = 0; $element < count($tablica1); $element++) {
  5.  $tablica_element = explode(",", $tablica1[$element]);
  6.  if ($tablica_element[1] != 0) {
  7. $missles[$tablica_element[0]] += $tablica_element[1];
  8.  }
  9. ?>

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.
Go to the top of the page
+Quote Post
Sebcioo
post 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.
Go to the top of the page
+Quote Post
nevt
post 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%)
-----


  1. <?php
  2. $BuildArray = explode (';', $b_hangar_id);
  3. foreach($BuildArray as $ElmentArray)
  4. {
  5. $Element = explode (',', $ElmentArray );
  6. $Missiles[(int)$Element[0]] += (int)$Element[1];
  7. }
  8. ?>


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... -
Go to the top of the page
+Quote Post
Sebcioo
post 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ł smile.gif 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
Go to the top of the page
+Quote Post
vtuner
post 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%)
-----


Cytat(Sebcioo @ 4.04.2008, 20:12:59 ) *
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.
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: 23.09.2024 - 04:02