Support Forum

Advanced Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
plugins-topic
Custom Profile Field
Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
Feb 2, 2012 - 6:05 am

This is all good but - a couple of points.

There should be no need whatsoever to use the output buffer. You should just be able to perform the $zip_code variable assignment without that.

Rather than using isset() I would recommend using empty(). More reliable as you have created the $zip_code variable so it does exist.

andy-signature.png
YELLOW
SWORDFISH
Avatar
SPQC
Member
Free Members
sp_UserOfflineSmall Offline
Feb 2, 2012 - 6:35 am

Hey VikingBrent, thanks for the help!  Nice it serve others too.

 

Following you suggestion I came to this :

 

$ZipCode=sp_CustomProfileFieldsDisplay('Code ami ', $spThisPost->user_id);
                            if (!empty($ZipCode['Zip Code'])) {
                                echo '<p style="font-size: 80%;" class="spCenter">Zip Code : $ZipCode</p>';
                                }

 

But again it looks the the echo does not work BUT I see the ZipCode when not empty.  So it seems the if is executed but the echo is not except that the variable $ZipCode is echoed? 

Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
Feb 2, 2012 - 7:13 am

That's because that is incorrect use of the variable.

If you want to embed a variable in a string of text like that then the whole string has to be in double quotes not single.

Use:

Zip Code : ' . $ZipCode . '

instead is easiest = or - swap all the quotes around.

andy-signature.png
YELLOW
SWORDFISH
Avatar
VikingBrent
Rookie
Free Members
sp_UserOfflineSmall Offline
Feb 2, 2012 - 12:08 pm
Thank you Yellow Swordfish for the clarification and suggestion.
However, I'm going to have to retract my previous post, as the code I shared is most certainly not working.

 It appears to function as I saw the correct value stored in my custom field, displayed in the appropriate location in the spUserSection div. However, upon further scrutiny, I discovered that the custom field value was only being echoed in the user's first post in the topic. If the user has more than one reply, the custom field value is missing from all but the first entry.

 I discovered that the variable is apparently not being defined at all. For troubleshooting purposes, I've stripped the code down to its barest form. Here's what I'm working with (these are the particulars from my own work, which is why there's no reference to the $ZipCode variable from prior posts):

 $SteamID=sp_CustomProfileFieldsDisplay('Steam ID', $spThisPost->user_id);

echo 'Steam: ';
echo $SteamID;

 What appears to be happening is the $SteamID from line 1 is being ignored entirely and the output that I'm seeing (in my first post in the topic) is coming from the sp_CustomProfileFieldsDisplay('Steam ID', $spThisPost->user_id); function.

 The first echo which outputs the string "Steam:" is working as expected and showing up on every reply. But the second echo outputs nothing. So here is the result:

 1ST POST

custom-field-valueSteam:

 2ND POPST

Steam:

 I double checked the code in a clean install of the css-only theme to verify that this wasn't the result of something I botched in my custom theme and the results are the same. As I stated before, I am a PHP novice so perhaps this is a simple syntax mistake.

 To check myself, I copied SPQC's code from reply #22, changing the improperly embedded variable, and substituted my own custom field name. Again, I stripped the 'if' statement for the sake of simplicity and got the same result: The output of sp_CustomProfileFieldsDisplay('Steam ID', $spThisPost->user_id); is displayed on the first post only.

 This code is being placed into the spTopicView.php file around line 112, before the start of the user-identities section.

 Any insight is most welcome.

 

Avatar
SPQC
Member
Free Members
sp_UserOfflineSmall Offline
Feb 2, 2012 - 4:56 pm

I see VikingBrent have replied but still waiting moderation.  You should subscribe!  Maybe you have the answer?  wink

 

Ok the echo don't work at all and the simple fact of having this line :

 

$ZipCode = sp_CustomProfileFieldsDisplay('Zip Code', $spThisPost->user_id);

 

output the zip code...  I tried a few things but I'm out of ideas.

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
Feb 2, 2012 - 7:54 pm

where are you guys putting this in your spTopicView template file?

also the template tag echos the output itself, so you cannot assign it to a variable...  nothing is returned...

sp_CustomProfileFieldsDisplay('Zip Code', $spThisPost->user_id);

will just output the zip code stored for the user...

if you precede that with

echo 'zip: ';

you will end up with something like

zip: 54545

I put them right after:

sp_PostIndexUserName('tagClass=spPostUserName spCenter');

in my topic view to verify it works fine...

Avatar
SPQC
Member
Free Members
sp_UserOfflineSmall Offline
Feb 2, 2012 - 8:05 pm

Mr Papa said

also the template tag echos the output itself, so you cannot assign it to a variable...  nothing is returned...

We were trying to see how to achieve that if user filled Zip Code, then display Zip Code: xxxx, if not then show nothing at all.

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
Feb 2, 2012 - 8:18 pm

okay, a little secret... eh, not really...  but there is an object that contains most of the data in this case being inside the post class with the $spThisPost user...

so in this case, you dont really have to use the template tag... its already in the user class...  but we try to generically help...

so for this exact case, and not globally, just do:

if (!empty($spThisPostUser->zipcode)) echo 'zip: '.$spThisPostUser->zipcode;

you obviously could have replace the $spThisPostUser->zipcode; part with a call to the template tag... but since in our post loop, its already been prefilled...

Avatar
SPQC
Member
Free Members
sp_UserOfflineSmall Offline
Feb 2, 2012 - 9:49 pm

Mr Papa said

so for this exact case, and not globally, just do:
if (!empty($spThisPostUser->zipcode)) echo 'zip: '.$spThisPostUser->zipcode;

hmmm..  I don't understand.  Dunno if my english is the problem.  Where is the call to properly target the desired CustumProfileField we want to display?

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
Feb 2, 2012 - 10:53 pm

lol. not your English, but my knowledge of SP...

i just happen to know that whenever the user class object is loaded (in this case $spThisPostUser), we also load all user meta info including any custom profile fields into the user object...  so

if (!empty($spThisPostUser->zipcode)) echo 'zip: '.$spThisPostUser->zipcode;

is the same as:

if (!empty($spThisPostUser->zipcode)) {
    echo 'zip: ';
    sp_CustomProfileFieldsDisplay('Zip Code', $spThisPost->user_id);
}

why? because as I said, we load the user object with all the info about that user... and the when you generate a slug from your custom field 'Zip Code', it becomse zipcode...  so I can just grab it from the user object instead of using the template tag...

if you were ever to look at the object $spThisPostUser, you would see lots of data about that user...

Forum Timezone: Europe/Stockholm
Most Users Ever Online: 1170
Currently Online:
Guest(s) 1
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Mr Papa: 19448
Ike: 2086
Brandon: 864
kvr28: 804
jim: 650
FidoSysop: 577
Conrad_Farlow: 531
fiddlerman: 358
Stefano Prete: 325
Member Stats:
Guest Posters: 620
Members: 17366
Moderators: 0
Admins: 4
Forum Stats:
Groups: 7
Forums: 17
Topics: 10128
Posts: 79626