By Mist on Monday, 27 July 2015
Posted in General Issues
Replies 5
Likes 0
Views 890
Votes 0
Hi guys, i am trying to output some user profile fields value in joomla article.
What i need is to build an "if" statement that will output the code only if the field have a value.

What i did so far

1. I am getting the author ID of the article like this

$authorid = $this->item->created_by ? $this->item->created_by : $this->item->author;


2. I am setting-up variables for the fields i need

//Author Extended Profile Info
$author_city = Foundry::user($authorid)->getFieldValue('ADDRESS')->value->city;
$author_state = Foundry::user($authorid)->getFieldValue('ADDRESS')->value->state;
$author_about = Foundry::user($authorid)->getFieldValue('ABOUT');
$user_website = Foundry::user($authorid)->getFieldValue('URL');


3. Now, finally, i am trying to "output" those fields ONLY if the field have value ... but it doesn't work

 <?php if( $author_about ){ ?>
<?php echo $author_about;?>
<?php } ?>


<?php if( $user_website ){ ?>
<a href="http://<?php echo $user_website; ?>/" rel="nofollow" target="_blank"><?php echo $user_website; ?></a>
<?php } ?>


... and so on

How to build that "if" statement in order to output the field only if it contains value ?

Thanks !
Thanks for the suggestion Nik. Your example was not working (throwing some errors) so i tried the one below which works perfect.
Hope is the right way to do it.

 <?php if ($author_about != '') { ?>
<?php echo $author_about;?>
<?php } ?>
·
Tuesday, 28 July 2015 16:24
·
0 Likes
·
0 Votes
·
0 Comments
·
You should try to debug and see what $author_abou and $user_website contains
·
Tuesday, 28 July 2015 01:58
·
0 Likes
·
0 Votes
·
0 Comments
·
I did a var_dump on $author_about , here is the result

1. Empty "about" field, var_dump will look like this

object(SocialFieldValue)#1747 (8) { ["unique_key"]=> string(5) "about" ["element"]=> string(8) "textarea" ["field_id"]=> string(3) "128" ["uid"]=> string(3) "692" ["type"]=> string(4) "user" ["value"]=> string(0) "" ["raw"]=> string(0) "" ["data"]=> string(0) "" }


2. "About" field not empty, var_dump look like this


object(SocialFieldValue)#1659 (8) { ["unique_key"]=> string(5) "about" ["element"]=> string(8) "textarea" ["field_id"]=> string(3) "128" ["uid"]=> string(4) "8611" ["type"]=> string(4) "user" ["value"]=> string(89) "This is the about text" ["raw"]=> string(89) "This is the about text" ["data"]=> string(89) "This is the about text" }


So, what i need is an "if" statement to echo $author_about only if the field have some value (not empty)
·
Tuesday, 28 July 2015 02:52
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello Mist,

You can try this:

if (!empty($author_about['value'])) {
echo $author_about['value'];
}


·
Tuesday, 28 July 2015 12:52
·
0 Likes
·
0 Votes
·
0 Comments
·
Hey Mist,

There are lots of method to check whether the variable is empty or not and one of it is like you posted above. By the way thanks for sharing.
·
Tuesday, 28 July 2015 16:31
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post