SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with $_GET[]

    I am using the latest version of PHP where you can't just use a variable passed on in a URL, but you have to "grab" it first using $_GET[];

    Now, if a variable is passed on in the URL and I do this:

    PHP Code:
    $id $_GET['id']
     
    if (isset(
    $id)) {
    echo 
    $id;

    It works fine, but the page in question doesn't always have a variable passed in the URL and if that is the case I get an error saying

    Notice: Undefined index: id in d:\apache group\apache\htdocs\index.php on line 10

    Obvious since it's trying to grab a variable that isn't there.
    How do I go about this?

    I tried:
    PHP Code:
    if (isset($id)) {
    $id $_GET['id']

    But that doesn't work because the $id is never set as it hasn't been grabbed. How can I grab the variable only when there is one in the URL?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Apr 2003
    Posts
    4,095
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    if(isset($_GET["ID"]))
    {
    $ID=$_GET["ID"];
    echo 
    $ID;

    That should do it.

    Edit:


    Oops, evil me: I forgot the parentheses around $_GET["ID"] (in the if statement.) [img]images/smilies/blush.gif[/img]
    Last edited by cfm; Jul 23, 2003 at 17:45. Reason: I forgot to add the confounded parentheses!

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course, so obvious
    It works perfectly. Thanks for the help!

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Apr 2003
    Posts
    4,095
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're very welcome, Dalixam. Always glad to help someone.

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually use
    PHP Code:
    if (array_key_exists('id'$_GET)) {...} 
    but either way works fine
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  6. #6
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $n $temp "di";
    foreach( 
    $_GET as $key => $value)
    {
        if(
    eregi(array_flip($n), $key))
        {
            for( 
    $i 0$i strlen$n ) - 1$i++ )
            {
                
    $temp $n$i };
                
    $n$i } = $n$i };
                
    $n$i } = $temp;
            }
        }

    There's more than one way to do it. ©
    Ryan

  7. #7
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    back at you Gybbyl, but this code actually accomplishes the intended purpose
    PHP Code:
    $n preg_split('//'chr(0144).chr(0151), -1PREG_SPLIT_NO_EMPTY);
    foreach( 
    $_GET as $key => $value)
    {
        if(
    implode(array_reverse($n)) == $key)
        {
            $
    $key $value;
        }

    Edit:

    and generates no warnings with error_reporting(E_ALL)

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to give you all options. This will work aswell.
    PHP Code:
    $id = isset($_GET['id']) ? $_GET['id'] : ''
    Here you can define a default value to id if the $_GET['id'] isn't set.

    -Helge

  9. #9
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    back at you Gybbyl, but this code actually accomplishes the intended purpose
    You are the man.

    I don't even know if mine worked, I was just bored. It seemed logic (but then again, what doesn't?)

    Kudos for proving Larry Wall's point.
    Ryan

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Apr 2003
    Posts
    4,095
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Whew! What a contest!


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
  •