Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]struktura if else
marian84
post
Post #1





Grupa: Zarejestrowani
Postów: 185
Pomógł: 10
Dołączył: 29.01.2008

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


Mam prostą strukturę pliku index.php

Kod
(...)

if($_GET['str']=='foto')
{
    include('foto.php);
}
elseif($_GET['str']=='pliki')
{
    include('costam.php');
}
itd.

(...)


kod niestety wyswietla błąd:

Notice: Undefined index: str in (...) index.php on line 5

Czemu?
Go to the top of the page
+Quote Post
slawny
post
Post #2





Grupa: Zarejestrowani
Postów: 214
Pomógł: 26
Dołączył: 7.11.2007
Skąd: Warszawa

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


A wklej coś poza tymi if-ami
Go to the top of the page
+Quote Post
marian84
post
Post #3





Grupa: Zarejestrowani
Postów: 185
Pomógł: 10
Dołączył: 29.01.2008

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


Kod
<?php
    include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Administracja</title>
        <meta name="Author" content="marian84">
        <meta name="copyright" content="....">
        <script type="text/javascript" src="js/menu.js"></script>
        <link rel="stylesheet" href="css/style.css" type="text/css" />
    </head>
    <body>
<?php
        if($_GET['str']=='foto')
        {
            include('foto.php);
        }
        elseif($_GET['str']=='pliki')
        {
            include('costam.php');
        }
?>
    </body>
</html>
Go to the top of the page
+Quote Post
slawny
post
Post #4





Grupa: Zarejestrowani
Postów: 214
Pomógł: 26
Dołączył: 7.11.2007
Skąd: Warszawa

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


Pojęcia nie mam. Krzyczy coś w 5 linii. Daj taki html

  1. <?php
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="pl" xml:lang="pl">
  3. ?>


Ten post edytował slawny 21.04.2009, 08:29:56
Go to the top of the page
+Quote Post
porady-it.pl
post
Post #5





Grupa: Zarejestrowani
Postów: 89
Pomógł: 20
Dołączył: 13.05.2007
Skąd: Warszawa

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


popraw najpierw literówke:
Kod
include('foto.php);

poprawnie:
Kod
include('foto.php');
Go to the top of the page
+Quote Post
batman
post
Post #6





Grupa: Moderatorzy
Postów: 2 921
Pomógł: 269
Dołączył: 11.08.2005
Skąd: 127.0.0.1




Cytat(marian84 @ 21.04.2009, 09:01:12 ) *
kod niestety wyswietla błąd:

Notice: Undefined index: str in (...) index.php on line 5

Czemu?

Ponieważ w zmiennej $_GET nie ma klucza 'str'. Masz dwie możliwości:
1. Dodać sprawdzanie czy $_GET['str'] istnieje (isset)
2. Użyć funkcji error_reporting.

Opis obu rozwiązań znajdziesz w manualu.

~slawny
Skoro nie masz pojęcia, to się nie udzielaj i nie wprowadzaj w błąd.
Go to the top of the page
+Quote Post
marian84
post
Post #7





Grupa: Zarejestrowani
Postów: 185
Pomógł: 10
Dołączył: 29.01.2008

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


Cytat(porady-it.pl @ 21.04.2009, 09:33:50 ) *
popraw najpierw literówke:
Kod
include('foto.php);

poprawnie:
Kod
include('foto.php');


literówka tutaj akurat nie ma znaczenia. Błąd krzyczy w każdej lini gdzie jest zawarte $_GET['str']
Go to the top of the page
+Quote Post
porady-it.pl
post
Post #8





Grupa: Zarejestrowani
Postów: 89
Pomógł: 20
Dołączył: 13.05.2007
Skąd: Warszawa

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


To dorzuć weryfikacje istnienia zmiennej:

  1. <?php
  2. if(isset($_GET['str'])) {
  3.  
  4.        if($_GET['str']=='foto')
  5.        {
  6.            include('foto.php');
  7.        }
  8.        elseif($_GET['str']=='pliki')
  9.        {
  10.            include('costam.php');
  11.        }
  12.  
  13. }
  14. ?>
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 05:22