News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Cant add/get value from $context

Started by Klajdi, January 25, 2017, 08:44:13 AM

Previous topic - Next topic

Klajdi

So, I got a value from database using the code below (Display.template.php):
  $query = $smcFunc['db_query']('', '
    SELECT subid
    FROM {db_prefix}membergroups
    WHERE group_name = {string:current_groupname}',
array(
'member' => (int) $context['user']['id'],
'current_groupname' => $message['member']['group'],
)
);

$row = $smcFunc['db_fetch_assoc']($query);

  $smcFunc['db_free_result']($query);
 
  $context['member']['subid'] = $row['subid'];

And when I use it on the same file arund line 270-286:
<li class="membergroup" data-memberID="', $message['member']['id'], '">', $context['member']['subid'], " SPACE ", $message['member']['group'], '</li>';

but whenever I use $context['member']['subid'] in Subs-SimpleColorizer.php it doesnt work.
It gives me HTTP ERROR 500
using 2.0.12

vbgamer45

If that query is ran in the template file. Probably won't work.
Templates are executed after the sources/ folder files.
Community Suite for SMF - Grow your forum with SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com - Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Kindred

also, remember, the content of the $context array is fluid and what is loaded for one page may not be there for another page.

And yes... as vbgamer says, but more specifically database queries should never be run in template files.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Klajdi

I deleted it from Display.template.php but when I add it to Subs-SimpleColorizer.php page still wont load, or gives HTTP ERROR 500

vbgamer45

Probably because  $message['member']['group'] is not defined in that context...
Community Suite for SMF - Grow your forum with SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com - Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Klajdi

Quote from: vbgamer45 on January 25, 2017, 01:49:09 PM
Probably because  $message['member']['group'] is not defined in that context...
Hmm, im not sure where $message is defined.

Kindred

again...  it really depends on where it's being display and used

You don't seem to understand that the "same" variable name can be used with different content, depending on where it is defined and how it is used.

As I said, $context is regularly reloaded and overwritten by new data, depending on the page that it's being used on...

maybe Display.php...   maybe Subs-MessageIndex.php

but simplecolorizer edits mod than the display of messages.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Klajdi

Ok, so, im a bit confused, where is $message['member']['group'] defined?
And how do I define it, so I can use it on simplecolorizer?

Klajdi

Any way to replace $message['member']['group'] with sth else?

Any way I can get 'subid' for each member?

Klajdi

So, i managed to create "$memberContext[$message['id_member']]['subid_show']" and seems to work good. Is there any way I can use this in Sub-Simplecolorizer.php?

nend

It is actually defined in the while statement in the display template. This data doesn't have a long TTL as it is done this way for memory reasons.

What happens is the query is created in the display sources and when the template is called it loads the defined function and the query is executed.

I believe if memory serves me right, this is the only area in SMF the query is actually made while in a template. May be the reason your having trouble with it.

Klajdi

Quote from: nend on January 28, 2017, 12:05:32 AM
It is actually defined in the while statement in the display template. This data doesn't have a long TTL as it is done this way for memory reasons.

What happens is the query is created in the display sources and when the template is called it loads the defined function and the query is executed.

I believe if memory serves me right, this is the only area in SMF the query is actually made while in a template. May be the reason your having trouble with it.
I managed to add it in the "$memberContext[$message['id_member']]['subid_show']", I want to know how to use it Sub-Simplecolorizer.php

Klajdi

Ok, as I mentioned I managed to get the subid value from database, making some modifications in Load.php file.
I managed to sucessfully echo (and use) the value in Display.php
$memberContext[$message['id_member']]['subid']
or in Display.template.php
$message['member']['subid']

Managed to fix most errors in error log (except one which ill be adding below)
I sucessfully managed to get the value:
$memberContext[$message['id_member']]['subid']
but a bit differently:

loadMemberData($user_id);
loadMemberContext($user_id);
$memberContext[$user_id]['subid_show']


Now I'm getting a fairly odd error:

And the odd thing is that there are only 191 lines in that file.

Kindred

see the "eval" at the end...   that means that the error is not actually occuring in that file - it's just triggered in that file.

Turn off template eval in your admin > server settings
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Advertisement: