SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If Theres No Data From $_GET

    Hi Guys,

    How would i do an if statement where it checks to see if the following variables have data in them?

    $title=$_GET['title'];
    $first=$_GET['firstname'];
    $last=$_GET['lastname'];
    $email=$_GET['email'];
    $dob=$_GET['dob'];
    $ip=@$REMOTE_ADDR;
    $subid=$_GET['subid'];
    $proid=$_GET['p'];
    $affid=$_GET['affid'];

    If not then it will say which one has no data.

    Any help would be great please.

    Thanks

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    if(isset(somevariable))

  3. #3
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, how ever im having trouble implementing it:

    PHP Code:
    if(isset($affid) || ($p) || ($title) || ($first) || ($last) || ($email) || ($dob))
    echo 
    "DATA MISSING!";
    else
    echo 
    "All THERE!"
    It just says DATA MISSING even though there isnt. Any help would be great.

    Thank you.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Notwithstanding Starlion's reply...

    PHP Code:
    // your incoming GET vars
    $_GET = array(
          
    'a'=>1
        
    'b'=>2
        
    'c' => ''
        
    'd'=>0);

    // vs what you are expecting, as keys (a so called 'white-list')
    $expected = array('a','b','e');

    // a) check rather dumbly which are "empty" - even if not on white-list
    foreach( $_GET as $k=>$v){
    if(empty(
    $v)) echo "$k is empty <br />";
    }

    // b) check for keys which should not be there (not on 'white-list')
    $bad_key array_diffarray_keys($_GET), $expected );
    if(
    $bad_key)  echo 'Unexpected key found ' print_r($bad_key1),'<br />' ;

    // c) check for keys which should be there but are not set (missing from white-list)
    $missing_key array_diff$expectedarray_keys($_GET)  );
    if(
    $missing_key) echo 'Missing key detected:',print_r($missing_key1), '<br />'
    Outputs:
    ======
    c is empty
    d is empty
    Unexpected key found Array ( [2] => c [3] => d )
    Missing key detected :Array ( [2] => e )

    I'm not saying you need all/any of this by any means, as it not exactly clear what you are after - its just that I ended up writing something like b) yesterday, and I wondered maybe there is a better way of doing it?

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by AdWarm View Post
    Thank you, how ever im having trouble implementing it:

    PHP Code:
    if(isset($affid) && ($p) || ($title) || ($first) || ($last) || ($email) || ($dob))
    echo 
    "DATA MISSING!";
    else
    echo 
    "All THERE!"
    It just says DATA MISSING even though there isnt. Any help would be great.

    Thank you.
    Nope.
    PHP Code:
    if( isset($title) && isset($first) && isset($first) ... etc ){
      echo 
    'all were set, but might be empty**';
    }else{
    echo 
    'well at least one was not set, but which one?';

    ** depending on how you define empty

    Edit:

    http://www.deformedweb.co.uk/php_variable_tests.php <- have a good read of this when trying to get a handle on true/false empty/notempty set/not set
    Last edited by Cups; Aug 16, 2011 at 07:10. Reason: oops, swopped || for &&

  6. #6
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a little confused with the code from your response Cups, can you take a look at my reply above yours so you can see what im trying to do please.

    Thanks

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AdWarm View Post
    Thank you, how ever im having trouble implementing it:

    PHP Code:
    if(isset($affid) || ($p) || ($title) || ($first) || ($last) || ($email) || ($dob))
    echo 
    "DATA MISSING!";
    else
    echo 
    "All THERE!"
    It just says DATA MISSING even though there isnt. Any help would be great.

    Thank you.
    isset($affid);

    if affid is set, this is TRUE. Which.. isnt what you want. Because you're looking for something that is missing. You're looking for if affid is NOT set.. (Hint: NOT.)

    PS: You need to isset every clause.

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If not then it will say which one has no data.
    Look at the truth table link I gave you.

    Now, do you want to know:

    Which get var is set but has no data?

    or

    Which get var is missing completely? (and therefore has no data, because it is not even set).

    The two are not the same, you see.

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Look at the truth table link I gave you.

    Now, do you want to know:

    Which get var is set but has no data?

    or

    Which get var is missing completely? (and therefore has no data, because it is not even set).

    The two are not the same, you see.
    A very big difference. Same as when you query a database and it returns 0 results vs returning an error.

  10. #10
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    isset($affid);

    if affid is set, this is TRUE. Which.. isnt what you want. Because you're looking for something that is missing. You're looking for if affid is NOT set.. (Hint: NOT.)

    PS: You need to isset every clause.
    That is correct, i need it to check if its NOT been set.

    PHP Code:
    if( isset($title) && isset($first) && isset($first) && isset($last) && isset($email) && isset($dob)  ){
      echo 
    'DATA MISSING';
    }else{
    echo 
    'DATA ALL THERE';

    just keeps saying DATA MISSING.

    Any help would be great.

    Thank you again guys!

  11. #11
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Well, lets put your code into sentence form and see if it makes sense why you're getting the error.

    if( isset($title) && isset($first) && isset($first) && isset($last) && isset($email) && isset($dob) ){
    echo 'DATA MISSING';


    If title is set, and first is set, and first is set (again), and last is set, and email is set, and dob is set, then data is missing.

    Something seem off there?

    (For those of you playing along at home, we've stepped across logic transformation 1, almost... A && B = !(!A || !B))

  12. #12
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've done it, but done it a different way.

    PHP Code:
    if (empty($title) || empty($first) || empty($last) || empty($email) || empty($dob)) {
        echo 
    'NO DATA!';
    }else{
        echo 
    'ALL DATA';

    seems to work fine

    Thanks for your help guys!

  13. #13
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Ah, yes, my reply reversed the check - confusing enough without me doing that I suppose...

    if( true && true ){
    // they were set
    }

    Still, if AdWarm is happy, then so am I.


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
  •