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

if(isset(somevariable))

Thank you, how ever im having trouble implementing it:


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.

Notwithstanding Starlion’s reply…


// 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_diff( array_keys($_GET), $expected );
if($bad_key)  echo 'Unexpected key found ' . print_r($bad_key, 1),'<br />' ;

// c) check for keys which should be there but are not set (missing from white-list)
$missing_key = array_diff( $expected, array_keys($_GET)  );
if($missing_key) echo 'Missing key detected:',print_r($missing_key, 1), '<br />';

Outputs:

c is empty 
d is empty 
Unexpected key found Array ( [2] =&gt; c [3] =&gt; d ) 
Missing key detected :Array ( [2] =&gt; 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?

Nope.


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

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

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.

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.

A very big difference. Same as when you query a database and it returns 0 results vs returning an error.

That is correct, i need it to check if its NOT been set.

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!

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))

I’ve done it, but done it a different way.


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

seems to work fine :slight_smile:

Thanks for your help guys!

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. :slight_smile: