Want to know Optimized way for adding new Custom User Field which is changing

gginni

Active member
Hi,

I just read the below tutorial:-

http://xenforo.com/community/threads/custom-user-fields-in-xenforo-1-1.20673/

And below is my requirement:-
I want to add new custom user field which will show threads posted by user in particular forum. This Custom User field will show up in message_user_info below user profile pic.

I know one way to achieve the same:-

1. I will add Custom User field using the tutorial and will define PHP Callback and method.
2. Within PHP callback method. I will write below query:-

Code:
select count(*) as total from xf_thread where node_id = 10 and user_id = 5

But I guess this will hamper the performance because let's say I have 30 posts to show on one page and 30 different users posted in that thread then 30 extra queries will be executed to show this thing...

Can anyone please give me some optimized way to achieve this. I just need to show one custom user field below user profile pic and that custom user field will show threads posted in particular forum.

Please help me.
 
The PHP Callback should be used for verifying the value inserted, not for setting it. You might be better off adding in a new column to xf_user and populating it via cron job.
 
The PHP Callback should be used for verifying the value inserted, not for setting it. You might be better off adding in a new column to xf_user and populating it via cron job.

Oh we cannot use PHP call back for getting the value :S.. then that's really bad... And yeah I thought of doing the same..adding column in xf_user but then it will be load of work for me because then I need to write triggers or queries to manage below things:-

1. +1 count when thread is posted in that forum
2. +1 count when thread is moved in that forum
3. -1 count when thread is deleted from that forum
4. -1 count when thread is moved in some other forum from that forum
5. -1 count or +1 count when thread is merged

Managing these many things is not possible for me atleast :P :P..

Isn't there any simple way to have custom user field that will show number of threads posted in particular forum?? If not then tell me how to add triggers/ or where to add things to manage +1/-1 count :(
 
Oh we cannot use PHP call back for getting the value :S.. then that's really bad... And yeah I thought of doing the same..adding column in xf_user but then it will be load of work for me because then I need to write triggers or queries to manage below things:-

1. +1 count when thread is posted in that forum
2. +1 count when thread is moved in that forum
3. -1 count when thread is deleted from that forum
4. -1 count when thread is moved in some other forum from that forum
5. -1 count or +1 count when thread is merged

Managing these many things is not possible for me atleast :p :p..

Isn't there any simple way to have custom user field that will show number of threads posted in particular forum?? If not then tell me how to add triggers/ or where to add things to manage +1/-1 count :(
Custom Fields are meant for user set values, not generated values. You could set a cron job to run your count query for users that were active in the last >x time frame (since last run) and update the field via a cron job. It won't be 100% up to date, but it'll be easier to code & manage.
 
Custom Fields are meant for user set values, not generated values. You could set a cron job to run your count query for users that were active in the last >x time frame (since last run) and update the field via a cron job. It won't be 100% up to date, but it'll be easier to code & manage.

Oh wowww great solution bro :) ... I guess cron job will do it for me :)... I can even set it to run after 24 hrs coz I actually dnt need too much up-to-date information :) ... Or even if in future I wanted upto date information then I can run cron job after 6 or 12 hrs :)... Excellent solution :).. Since I'm newbie in PHP, I never had this solution in my mind :) .. But as I'm web developer in .Net, I know this is the best solution which anyone can propose :)

Thanks once again :)
 
Create a cron entry in:
ACP -> Tools -> Cron Entries -> + Create New Cron Entry

Set your PHP Callback and settings in there.
 
Create a cron entry in:
ACP -> Tools -> Cron Entries -> + Create New Cron Entry

Set your PHP Callback and settings in there.

I have written the code and tested as well.. I have added new column in xf_user table and update the column value..Also I have written cron job which is working fine

Now can u plz tell me how to access the value.. Let's say new column in xf_users is special_forum_count... Now I need to show this value under every display pic... Currently I'm showing 2 things.. Messages, Likes Received. Now I want to show 3rd thing..Special Forum Points.. How to get as you said that PHP callback are for validation only and cannot be used for setting the values... I was under impression that I can use PHP callback of custom user field.. now what should I do?
 
Ohh too easy it was .. I just used $user.special_forum_count in my template and it worked :)

Xenforo is too easy for developers :)

Thanks a lot bro for your help :)

This question can be marked as closed :)
 
Create a cron entry in:
ACP -> Tools -> Cron Entries -> + Create New Cron Entry

Set your PHP Callback and settings in there.

Bro can you help me in one last thing... I have shown the count of that special forum but now I want to make that as link like count in front of messages is link:-

below is messages search code:-

Code:
<dd><a href="{xen:link search/member, '', 'user_id={$user.user_id}'}" class="concealed">{xen:number $user.message_count}</a></dd>

I want to show search using three things:-
1. user_id
2. node_id
3. Search threads started by this member only

Below is screenshot which shows that search is possible using search window:-

pHiZJ01.png


Please tell me what are the variables that get set in this post request... I tried using tamper data but then I don't know how to set those variables in template like the way for messages count is done using below code:-

Code:
<dd><a href="{xen:link search/member, '', 'user_id={$user.user_id}'}" class="concealed">{xen:number $user.message_count}</a></dd>
 
So, you want your new special count to be a hyperlink to search, where it will search for what?

Sorry, I'm just not understanding the specifics of what you're asking :p

Yes bro I want new special count to be hyperlink to search

Actually I'm able to show threads by user in all forums:-

Code:
<dd><a href="{xen:link search/search, '', 'type=post', 'user_content=thread', 'nodes=10', 'users={$user.username}'}" class="concealed">{xen:number $user.adeals_count}</a></dd>

I have written nodes=10 but it is returning threads of that particular user in all forums :(.. I want threads in particular forum whose node_id is 10?? I tried using nodes as post parameter but it is not working..actual parameter is nodes[] but how to escape that??
 
Try setting it as nodes[]=10. I'm on a phone so shooting in the dark here.

Tried everything bro :(..
1. nodes[]
2. nodes
3. node_id
4. nodes%5B%5D

But it is not working.. On clicking count I see all the threads posted by that user instead of showing threads of particular forum...

For 1st and 4th it is not even allowing me to save the template... I get error:-

"The following templates contained errors and were not saved: message_user_info: 1) Line 56: Named parameter not specified correctly."

It is very small thing.. I just want to set the post parameter to set node_id... and the name of nodes parameter is sadly nodes[] and now template is not allowing me to save :(

Please help me :)
 
I'll have to look into it later tonight when I'm on a computer.

Did you get a chance to see it??

Just add this line in template message_user_info:-

Code:
<dd><a href="{xen:link search/search, '', 'type=post', 'user_content=thread', 'nodes=10', 'users={$user.username}'}" class="concealed">{xen:number $user.message_count}</a></dd>

And after clicking on message count it will show all the threads posted by that user in all the forums... nodes=10 should be considered... I just want to show threads in particular forum... plz help me

nodes[] is not working in template :(
 
Its probably going to be easier to hard code your link (since you can custom code it to your site):
HTML:
<dd><a href="index.php?search/search&type=post&user_content=thread&nodes[]=10&users={$user.username}," class="concealed">{xen:number $user.message_count}</a></dd>
If you have full friendly urls (I don't in my test environment), use:
HTML:
<dd><a href="/search/search?type=post&user_content=thread&nodes[]=2&users={$user.username}," class="concealed">{xen:number $user.message_count}</a></dd>
 
Its probably going to be easier to hard code your link (since you can custom code it to your site):
HTML:
<dd><a href="index.php?search/search&type=post&user_content=thread&nodes[]=10&users={$user.username}," class="concealed">{xen:number $user.message_count}</a></dd>
If you have full friendly urls (I don't in my test environment), use:
HTML:
<dd><a href="/search/search?type=post&user_content=thread&nodes[]=2&users={$user.username}," class="concealed">{xen:number $user.message_count}</a></dd>

I'm so stupid :(... this PHP is now killing me... it was too easy :(... I'm really sorry bro for bothering u for such a small thing... I could have thought of using full URL :(... I didn't use full URL coz I thought it is post request and if I will hard-code it in URL then it will be get request and search page will not be able to read the query string variables :(... My mind was fully towards making a post request :(... and that too coz of stupid type=post parameter... Now got to know that this is another variable and not a post request.... I never thought that even a simple get request can do a search :(

Thanks a lot bro :).. I have hard-coded the URL and now I'm getting the results which I wanted :)

Thanks a lot for your help bro :)... I owe you a party now ;)... Do let me know whenever u come to India ;) :P
 
I'm so stupid :(... this PHP is now killing me... it was too easy :(... I'm really sorry bro for bothering u for such a small thing... I could have thought of using full URL :(... I didn't use full URL coz I thought it is post request and if I will hard-code it in URL then it will be get request and search page will not be able to read the query string variables :(... My mind was fully towards making a post request :(... and that too coz of stupid type=post parameter... Now got to know that this is another variable and not a post request.... I never thought that even a simple get request can do a search :(

Thanks a lot bro :).. I have hard-coded the URL and now I'm getting the results which I wanted :)

Thanks a lot for your help bro :)... I owe you a party now ;)... Do let me know whenever u come to India ;) :p
No biggie. Using {xen:link} is good practice and the 'proper' way to do it. (y) It's just sometimes easier to take shortcuts when they won't make your life difficult in the future (template edits).
 
Back
Top Bottom