Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP][HTML]Funkcje ...
ArturEales
post
Post #1





Grupa: Zarejestrowani
Postów: 106
Pomógł: 0
Dołączył: 21.09.2008

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


Witam posiadam taki kod:
Kod
function logowanie()
{
    
    if($_SESSION['logowanie'] == 'poprawne')
{
    $string  = '<form action="'.getenv(REQUEST_URI).'" method="post">';
       $string .= '    <input type="submit" name="wylogowanie" value="Wyloguj" />';
       $string .= '</form>';
}
     else
{
        $string = '<form action="'.getenv(REQUEST_URI).'" method="post">';
       $string .= '    <ul style="list-style-type: none;  margin: 0; padding: 0;">';
      
       if(isset($_SESSION['logowanie'])) $string .= '<li>'.$_SESSION['logowanie'].'</li>';
      
       $string .= '        <li>Login: <input type="text" name="login" /></li>';
       $string .= '        <li>Haslo: <input type="password" name="haslo" /></li>';
       $string .= '        <li><input type="submit" name="logowanie" value="Logowanie" /></li>';
       $string .= '    </ul>';
       $string .= '</form>';
      
}    
    return $string;    
}

Problem polega na tym ,że nie wiem jak to rozdzielić żebym nie pisał
echo logowanie ;
tylko wyloguj i loguj w różnych miejscach.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
phpion
post
Post #2





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




To rozbij to na 2 osobne funkcje.
Go to the top of the page
+Quote Post
ArturEales
post
Post #3





Grupa: Zarejestrowani
Postów: 106
Pomógł: 0
Dołączył: 21.09.2008

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


Ale jak kombinowałem i teraz się zaplątałem w tym jak próbowałem to mi if nie działo.
Go to the top of the page
+Quote Post
phpion
post
Post #4





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Pokaż jak próbowałeś - gotowca nie dostaniesz (przynajmniej ode mnie).
Go to the top of the page
+Quote Post
ArturEales
post
Post #5





Grupa: Zarejestrowani
Postów: 106
Pomógł: 0
Dołączył: 21.09.2008

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


np.

Kod
function wyloguj()
{

if($_SESSION['logowanie'] == 'poprawne')
{
    
     $string  = '<form action="'.getenv(REQUEST_URI).'" method="post">';
        $string .= '    <input type="submit" name="wylogowanie" value="Wyloguj" />';
        $string .= '</form>';
}
     return $string;    
}

function logowanie()
(


else
{
         $string = '<form action="'.getenv(REQUEST_URI).'" method="post">';
        $string .= '    <ul style="list-style-type: none;  margin: 0; padding: 0;">';
        
        if(isset($_SESSION['logowanie'])) $string .= '<li>'.$_SESSION['logowanie'].'</li>';
        
        $string .= '        <li>Login: <input type="text" name="login" /></li>';
        $string .= '        <li>Haslo: <input type="password" name="haslo" /></li>';
        $string .= '        <li><input type="submit" name="logowanie" value="Logowanie" /></li>';
        $string .= '    </ul>';
        $string .= '</form>';
        
}    
     return $string;    
)


Ten post edytował ArturEales 23.10.2008, 08:41:23
Go to the top of the page
+Quote Post
phpion
post
Post #6





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Pierwsza wygląda na ok. Druga jest kompletnie źle napisana.

if (warunek) <- warunek spełniony
else <- warunek nie spełniony

W takim razie drugą funkcję można napisać tak:
  1. <?php
  2. function logowanie()
  3. (
  4.  
  5.  
  6. // else  <- to jest zle
  7. if($_SESSION['logowanie'] != 'poprawne') // tak powinno byc
  8. {
  9.         $string = '<form action="'.getenv(REQUEST_URI).'" method="post">';
  10.        $string .= '    <ul style="list-style-type: none;  margin: 0; padding: 0;">';
  11.        
  12.        if(isset($_SESSION['logowanie'])) $string .= '<li>'.$_SESSION['logowanie'].'</li>';
  13.        
  14.        $string .= '        <li>Login: <input type="text" name="login" /></li>';
  15.        $string .= '        <li>Haslo: <input type="password" name="haslo" /></li>';
  16.        $string .= '        <li><input type="submit" name="logowanie" value="Logowanie" /></li>';
  17.        $string .= '    </ul>';
  18.        $string .= '</form>';
  19.        
  20. }    
  21.     return $string;    
  22. )
  23. ?>


// Edit:
Jeszcze przydałoby się w obu funkcjach przypisanie pustego ciągu do $string przez warunkiem aby w razie jego nie spełnienia zwrócić pusty ciąg.

Ten post edytował phpion 23.10.2008, 08:53:19
Go to the top of the page
+Quote Post
ArturEales
post
Post #7





Grupa: Zarejestrowani
Postów: 106
Pomógł: 0
Dołączył: 21.09.2008

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


ok po drobnej walce z nawiasami działa...Wielkie dzięki.
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: 21.08.2025 - 15:39