OcalaDesigns
OcalaDesigns

Reputation: 39

Simple IfThen Statement from WordPress Field

Ok i just don't have enough knowledge for this simple script apparently. Basically, in the end I will be using an ifthen statement on the address field, if it's empty, the map will not appear, if it's got an address, the map will appear.

This is not the ifthen statement, i'm just trying to figure out how to write one from the array, or whatever, I'm getting. This is what I can get to output from the address field.

<?php

global $userdata, $app_abbr, $gmap_active;

$custom_fields = get_post_custom();
$varaddress = $custom_fields[$app_abbr.'_street'];

echo strlen($varaddress); //
var_dump($varaddress); //
echo string($varaddress);
?>

This is what I'm getting from that:

When the address field is empty:

5array(1) { [0]=> string(0) "" } 

When the address field is full:

5array(1) { [0]=> string(24) "14 South Magnolia Avenue" } 

I'm trying to figure out how to write a simple ifthen statement against this but it keeps coming back as true no matter how I try ... so i broke it down to see exactly what was being returned and that was the output. How?

Ok since I now know how to "pop" something out of the array ... lol here's the ifthen statement that seems to work. Let me know if it can be streamlined or not:

<?php

global $userdata, $app_abbr, $gmap_active;

$custom_fields = get_post_custom();
$varaddress = $custom_fields[$app_abbr.'_street'];
$varmap = array_pop($varaddress);

if (strlen($varmap) >= 1 && !is_bool($varmap)) {
include_once ( TEMPLATEPATH . '/includes/sidebar-gmap.php' ); 
} else {
echo "NO ADDRESS GIVEN FOR MAP"; 
}

?>

Upvotes: 1

Views: 103

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270727

What you have is a one-element array. You can either access the address by its array index [0] or pop it off:

echo $varaddress[0];
// Or    
echo array_pop($varaddress);

The reason it gives you 5 for strlen() is that when you cast an array as a string as when passing it to a function like strlen(), it returns the string Array, which has a length of 5. The result is misleading and has nothing do do with the array's actual contents.

Upvotes: 2

Related Questions