News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Call info from one specific custom field

Started by samborabora, July 10, 2014, 05:50:45 AM

Previous topic - Next topic

samborabora

I have custom profile called 'About Me' and it's code in the profile editor is:
<textarea name="customfield[cust_aboutm0]" rows="5" cols="50"></textarea>

I'd like, instead of calling for all of the custom fields together, to call just one custom field, in this case, I assume it is called cust_aboutm0. I tried:
   foreach ($context['custom_fields'] as $field)
      {
      if ($field['name'] == 'cust_aboutm0')
         echo '
            <dt>', $field['name'], ':</dt>
            <dd>', $field['output_html'], '</dd>';
      }


But this displays nothing. Where did I go wrong?

ZerK

whenever i wanna get something from an array and i  have no documentation about it i usually use print_r:

print_r($context['custom_fields']);

then you can get the key/id of the field you want and call it directly without having to iterate throught all its items.

Arantor

Where are you trying to use that custom field exactly?
Holder of controversial views, all of which my own.


samborabora

Quote from: ‽ on July 10, 2014, 01:41:29 PM
Where are you trying to use that custom field exactly?

On the profile, but I don't want to use the standard output of all the custom fields, I want to specify an individual form.

Arantor

On what profile? The main profile? The mini profile on post view?
Holder of controversial views, all of which my own.


samborabora

Quote from: ‽ on July 10, 2014, 04:32:49 PM
On what profile? The main profile? The mini profile on post view?

profile.template.php, the profile of whichever user you click on, every users profile.

samborabora

Quote from: ZerK on July 10, 2014, 05:53:26 AM
whenever i wanna get something from an array and i  have no documentation about it i usually use print_r:

print_r($context['custom_fields']);

then you can get the key/id of the field you want and call it directly without having to iterate throught all its items.
Thanks, I tried this and all I got was
Array ( [0] => Array ( [name] => About Me [desc] => [type] => textarea [input_html] =>  [output_html] => [placement] => 0 [colname] => cust_aboutm0 [value] => ) [1] => Array ( [name] => Facebook Profile [desc] => Facebook Profile [type] => text [input_html] =>  [output_html] => [placement] => 0 [colname] => face_pro [value] => ) )

Which once again just confirms that name was cust_aboutm0 but I still cant figure how to just show this field?

ZerK

there are multiple ways to do this:

foreach ($context['custom_fields'] as $field)
if($field[colname] == "cust_aboutm0")
echo $field['value'];


the second one is just printing it:
name:
echo $context['custom_fields'][0]['name'];
value:
echo $context['custom_fields'][0]['value'];

samborabora

Perfect, that first one works!  Thanks so much!! :D

samborabora

#9
One last question, what's the best way of checking if the value is empty, ie: if the field in question has no text in it, so I can put an "they've said nothing" or whatever message?

EDIT: Pardon my ignorance, but I've tried this, and it doesn't work, but I'm not the best php coder yet!
if (!empty($field[colname] == "cust_aboutm0"))

ZerK

foreach ($context['custom_fields'] as $field)
if($field['colname'] == "cust_aboutm0")
echo (empty($field['value']) ? 'they ve said nothing' : $field['value']);


didnt tested it but it should work.

samborabora

Quote from: ZerK on July 13, 2014, 09:27:46 PM
foreach ($context['custom_fields'] as $field)
if($field['colname'] == "cust_aboutm0")
echo (empty($field['value']) ? 'they ve said nothing' : $field['value']);


didnt tested it but it should work.

Perfect, thankyou!! :D :D

hicks

#12
This was helpful but ¿what about showing the custom field in Display.template.php ?

I tried
echo $context['custom_fields']['cust_tipode']['value'];

cust_tipode is the name of the variable of the value I want to get (I'm not sure if works like that).

So I have the table smf_themes with
id_member - id_theme - variable - value
------------------------------------------
5828        -   1      - cust_tipode - valueA
5748        -   1      - cust_tipode - valueB
...
...

(I thought no need to open new thread for the similarity of what I need)

Thanks

Edit*

Finally found it

http://www.simplemachines.org/community/index.php?topic=379579.0

(Yo have to add $smcFunc to function template_main in Display.template.php  (line 15))

Advertisement: