Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Gdy pole jest puste
Forum PHP.pl > Forum > Przedszkole
szymon202
  1. if(($api->status == 'OK')) {
  2. if($produkt["price"] == $api->cash) {
  3. echo '<div class="alert alert-dismissible alert-success"><button type="button" class="close" data-dismiss="alert">×</button>Pomyślnie zakupiono przedmiot!</div>';
  4. }
  5. } else {
  6. echo '<div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Błędny kod!</div>';
  7. } elseif(empty($code)) {
  8. echo '<div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Błędny kod!</div>';
  9. }


Gdy kod jest poprawny, wyświetla dobrze.
Gdy kod jest błędny, wyświetla dobrze.
Gdy zawartość $code ($_POST["code"]) jest pusta, nic nie wyświetla. I właśnie dlaczego nic nie wyświetla.
Kshyhoo
IF
szymon202
Cytat(Kshyhoo @ 25.03.2015, 21:12:05 ) *

  1. } elseif(empty($code)) {
  2. echo '<div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Błędny kod!</div>';
  3. }
Kshyhoo
Poprawnie tak:
  1. if(wyrazenie) {
  2. // blok kodu
  3. } elseif(wyrazenie) {
  4. // blok kodu
  5. } else {
  6. // blok kodu
  7. }
szymon202
Cytat(Kshyhoo @ 25.03.2015, 21:19:07 ) *
Poprawnie tak:
  1. if(wyrazenie) {
  2. // blok kodu
  3. } elseif(wyrazenie) {
  4. // blok kodu
  5. } else {
  6. // blok kodu
  7. }


Wciąż nie działa.
  1. if(($api->status == 'OK')) {
  2. if($produkt["price"] == $api->cash) {
  3. echo '<div class="alert alert-dismissible alert-success"><button type="button" class="close" data-dismiss="alert">×</button>Pomyślnie zakupiono przedmiot!</div>';
  4. }
  5. } elseif(empty($code)) {
  6. echo '<div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Błędny kod!</div>';
  7. } else {
  8. echo '<div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Błędny kod!</div>';
  9. }
Aqu
może $api->status == 'OK' ale $produkt["price"] != $api->cash ?

  1. if($api->status == 'OK' && $produkt["price"] == $api->cash) {
  2. echo '<div class="alert alert-dismissible alert-success"><button type="button" class="close" data-dismiss="alert">×</button>Pomyślnie zakupiono przedmiot!</div>';
  3. } elseif(empty($code)) {
  4. echo '<div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Błędny kod!</div>';
  5. } else {
  6. echo '<div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Błędny kod!</div>';
  7. }


Jaki sens w ogóle ma ten elseif? Nawet jeśli ten warunek nie zostanie spełniony, to wyświetlasz ten sam tekst w else.
szymon202
Cytat(Aqu @ 25.03.2015, 21:23:39 ) *
może $api->status == 'OK' ale $produkt["price"] != $api->cash ?

  1. if($api->status == 'OK' && $produkt["price"] == $api->cash) {
  2. echo '<div class="alert alert-dismissible alert-success"><button type="button" class="close" data-dismiss="alert">×</button>Pomyślnie zakupiono przedmiot!</div>';
  3. } elseif(empty($code)) {
  4. echo '<div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Błędny kod!</div>';
  5. } else {
  6. echo '<div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Błędny kod!</div>';
  7. }


Jaki sens w ogóle ma ten elseif? Nawet jeśli ten warunek nie zostanie spełniony, to wyświetlasz ten sam tekst w else.


Cena produktu ma być równa cenie z API, więc ==.
Aqu
Chyba nie rozumiesz o co mi chodzi. Nic się nie wyświetla w sytuacji kiedy pierwszy warunek jest spełniony, a ten wewnątrz niego nie jest.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.