Jump to content

Accessing Custom Fields by ID (not sortorder)


jarod

Recommended Posts

I'm trying to add custom field values to the client summary client information in cliensummary.tpl but the only way I can get this to work using what I've found is by assigning the sortorder which doesn't seem very intuitive.

 

<tr><td>Secondary Phone</td><td>{$clientsdetails.customfields1}</td></tr>

 

How can I get the custom field value based on ID rather than sortorder?

Link to comment
Share on other sites

I'm trying to add custom field values to the client summary client information in cliensummary.tpl but the only way I can get this to work using what I've found is by assigning the sortorder which doesn't seem very intuitive.

surely it's intuitive in the sense that you can control the sort order! :)

 

How can I get the custom field value based on ID rather than sortorder?

any number of ways - depending on your coding experience...

 

1. you could re-sort the array in PHP... via a hook I suppose... might only be a couple of lines...

2. you could re-sort the array using Smarty in the template via the sortby plugin i've previously posted... basically using a foreach loop and specifying the order within the foreach.

3. you could use a hook to query the db or class method to create multiple new variables... e.g $secondaryphone and link it to a specific cf value etc

4. you could use a hook to query the database and get the cf values in a new array sorted however you want... with labels!

 

they're just the four that spring to mind off-hand... 2 is easy, but possibly not practical for this as you don't have labels in the array... 3 would be a safe solution and future proofed...

 

but frankly if it were me, i'd just use customfields1 etc and make a mental note that if you ever add a new ccf into the middle of the sorted order, it's going to mess up the variables you're using.

Link to comment
Share on other sites

Thank you for the info. Is there really no easier way to output the customfields data? I would imagine there be some way to simply output the value of a custom field based on the id... The sort order isn't even really in question except that it is the only way I have found to retrieve the custom field.

 

But yes, is there any way to get the custom field by it's inherit id and display that in the clientsuummary.tpl?

Link to comment
Share on other sites

Thank you for the info. Is there really no easier way to output the customfields data?

the above four options are easy - though perhaps some more than others! :idea:

 

I would imagine there be some way to simply output the value of a custom field based on the id... The sort order isn't even really in question except that it is the only way I have found to retrieve the custom field.

imagination is good... reality is another matter. :)

 

by default, you can only work with what's available to you... so in clientsummary.tpl, you'll only have two immediate options... direct access to specific customfield values ($clientsdetails.customfields1}, or an array that includes an ID value and so can be re-sorted by it.

 

XFJYsxg.png

 

as I said last night, outputting via a foreach in the template is easy - but borderline useless in this case because you can't display the name of the customfield (they're not available to the template), so there's no simple way to distinguish them...

 

                    {foreach from=$clientsdetails.customfields item=customfield}
                       <tr><td>{$customfield.id}</td><td>{$customfield.value}</td></tr>
                   {/foreach}

 

JNjm2YJ.png

you could use the sortby plugin to sort the customfields array by ID, but you'd still have no labels..

 

                    {foreach from=$clientsdetails.customfields|@sortby:id item=customfield}
                       <tr><td>{$customfield.id}</td><td>{$customfield.value}</td></tr>
                   {/foreach}

pVPOTvO.png

 

But yes, is there any way to get the custom field by it's inherit id and display that in the clientsummary.tpl?

if you don't want the additional code of querying the database or using the class method in a hook, the absolute simplest way I can think to do this in the template is by assigning the customfield names/labels to a new Smarty array and outputting them in the loop...

 

                    {$cflabels = ['5'=>'Dashboard','6'=>'Where did you hear about us?','7'=>'Other','9'=>'Are you sure?']}
                   {foreach from=$clientsdetails.customfields|@sortby:id item=customfield}
                       <tr><td>{$cflabels.{$customfield.id}}</td><td>{$customfield.value}</td></tr>
                   {/foreach}

j9Fcg3x.png

 

the above is written for v7.2.2 using Smarty3, but it should work in earlier WHMCS versions (v6 & before) using Smarty2 - though you'd possibly have to use the longer method of assigning a Smarty array.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated