Use custom database query values as part of $user

Discussion forum for MOD Writers regarding MOD Development.
Locked
amochohan
Registered User
Posts: 92
Joined: Tue Dec 11, 2007 2:59 pm

Use custom database query values as part of $user

Post by amochohan »

Ok, here's an example of what i'm trying to do.

A user on my site can log in and make comments on various things, I want an additional value as part of the $user[] array, for example $user['total_comments'] which stores the total number of comments made by that user.

I cannot simply add another field to the user database now as the site is established and there's over 50,000 comments made so that'd be next to impossible to do given time factors.

What I want to do, is part of the $user setup, it does the query to calculate the number of comments, and assigns it as part of the user array.

Can this be done? I assume then, this value could get cached to prevent unnescessary re-querying

User avatar
david63
Registered User
Posts: 17068
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Use custom database query values as part of $user

Post by david63 »

amochohan wrote:Can this be done?
Allthings can be done but this does not sound like a very good idea to me. it is going to mean an extra sql query, which from the sound of it could be quite a heavy query.

My suggestion would be add another column to the user table to hold the count, create a mod that updates the count whenever a comment is added and have a one off script to update the database with the current counts.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

amochohan
Registered User
Posts: 92
Joined: Tue Dec 11, 2007 2:59 pm

Re: Use custom database query values as part of $user

Post by amochohan »

david63 wrote:My suggestion would be add another column to the user table to hold the count, create a mod that updates the count whenever a comment is added and have a one off script to update the database with the current counts.
I've mentioned in my original post that this sort of workaround couldn't be done due to the large volume of original existing content.

Assuming, I wanted to use the 'inefficient' method - how could it be done? As a learning excercise it'd be interesting to know

amochohan
Registered User
Posts: 92
Joined: Tue Dec 11, 2007 2:59 pm

Re: Use custom database query values as part of $user

Post by amochohan »

In fact, If i did just write a mod that could calculate the number of comments and populate the fields automatically, running it as a one off script then modifying the post function of the comment system to automatically increase the comment counter - how can I access that new variable from the database as say $user['total_comments'] (assuming i add the database field as total_comments)

User avatar
david63
Registered User
Posts: 17068
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Use custom database query values as part of $user

Post by david63 »

Without knowing what database structure you have for this data it is impossible to say how to go about it.
amochohan wrote:I've mentioned in my original post that this sort of workaround couldn't be done due to the large volume of original existing content.
I still do not understand why you cannot do this "sort of workaround". If it is such a problem to do it as a "one off" then how are you going to cope doing it on every page?
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
david63
Registered User
Posts: 17068
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Use custom database query values as part of $user

Post by david63 »

amochohan wrote:In fact, If i did just write a mod that could calculate the number of comments and populate the fields automatically, running it as a one off script then modifying the post function of the comment system to automatically increase the comment counter - how can I access that new variable from the database as say $user['total_comments'] (assuming i add the database field as total_comments)
If you put it in the user table then it will be $user->data['total_comments'] and you will have nothing else to do other than maintain the count.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

amochohan
Registered User
Posts: 92
Joined: Tue Dec 11, 2007 2:59 pm

Re: Use custom database query values as part of $user

Post by amochohan »

Sorry, i'm not being clear - i menat to view that variable via a template say in memberlist_view.html...how would i assign it as a variable to use in the template

User avatar
david63
Registered User
Posts: 17068
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Use custom database query values as part of $user

Post by david63 »

amochohan wrote:how would i assign it as a variable to use in the template
In the same way as any other variable.You would need to look in memberlist.php for where it is picking up the other data and, because it is in the user table , it would be a fairly simple procedure to add it into the $template->assign_block_vars for where you want it to appear. You would then have to add a column into the template meberlist_body.html
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

Locked

Return to “[3.0.x] MOD Writers Discussion”