SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member Fenrir's Avatar
    Join Date
    Jul 2002
    Location
    . .. ... ... .. .
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a little error in my script... how do i fix it?

    hi, i'm sorry for bothering you folks... i just want to know how to fix this little error

    -----------------------------
    Notice: Undefined index: id in c:\Apache\Apache2\htdocs\home\test\main.php on line 3

    Notice: Undefined index: nav in c:\Apache\Apache2\htdocs\home\test\main.php on line 4
    -----------------------------

    here's my code

    PHP Code:
    <?php

    $id 
    $_GET['id'];
    $nav $_GET['nav'];

    ?>
    <html>
    <head><title>....</title>
    <style type="text/css">
    body,table {   font-size: 8pt; font-family: verdana   }

    a {   text-decoration: none; color: black; font-weight: bold   }

    a:hover {   color: orange   }
    </style>
    </head>
    <body bgcolor="#FDF6DB">
    <table cellpadding="0" cellspacing="0" align="center" width="760" height="100" bgcolor="white" border="2" bordercolor="black">
    <tr><td align="center"><b>Put Banner here!</b> <font color="red"><sup>testing sup</sup></font></td></tr>
    </table>
    <br>
    <table cellpadding="7" cellspacing="0" align="left" width="150" bgcolor="white" border="2" bordercolor="black">
    <tr><td>:: <a href=main.php>Home</a></td></tr>
    <tr><td>:: <a href=main.php?id=evangelion&nav=evanav>Evangelion</a></td></tr>
    <tr><td>:: <a href=main.php?id=links>Links</a></td></tr>
    <tr><td>-------------------</td></tr>
    <?php

    if ($nav == '') {
              include 
    'submenu.html';
    } else { 
              include 
    $nav '.html'
    }

    ?>
    </table>
    <table cellpadding="5" cellspacing="0" align="right" width="600" bgcolor="white" border="2" bordercolor="black">
    <?php

    if ($id == '') {
              include 
    'main.html';
    } else { 
              include 
    $id '.html'
    }

    ?>
    </table>
    </body>
    </html>

    any help is appreciated... thanks in advance

  2. #2
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To fix, do something like this:
    PHP Code:
    $id = (isset($_GET['id'])) ? $_GET['id'] : ''
    There's probably more ( and ) in there than you need, but you get the idea.
    Oh no! the coots are eating my nodes!

  3. #3
    SitePoint Member Fenrir's Avatar
    Join Date
    Jul 2002
    Location
    . .. ... ... .. .
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi timnz,

    thanks a lot your my saviour. it fixed the little error.



    just one thing, can you explain what that code just did? (or an explanation on what it does)

  4. #4
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure,

    It uses the ternary operator to make a judgement on what value $id should have.
    You're getting that error because when there is no id or nav specified in the URL, there is subsequently of course, no id or nav in the $_GET array.
    So when you try to assign the value in the $_GET array of the index (sometimes known as key) of id or nav, it can't find a value, and because error_reporting is probably by the looks of things on E_ALL it is displaying this fact, which PHP refers to as being a notice.
    What the isset() function does, if you are not aware, is check to see if a variable is set or not. Thus it solves our problem.

    So if I take what I wrote and the above garble:
    PHP Code:
    $id = (isset($_GET['id'])) ? $_GET['id'] : ''
    What this is saying is:
    if $_GET['id'] has been set (regardless of whether it has any decernable value), use it (ie the bit between ? and :, and if it hasn't been set, use the value between the : and ;

    There is more information about this operator here which says it better and more concisely than what I've just said here!

    Oh no! the coots are eating my nodes!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •