Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: rcane on April 01, 2023, 01:38:48 PM

Title: I need some help echoing some $variables in the index.template.php
Post by: rcane on April 01, 2023, 01:38:48 PM
2.0.19

I've got a php page that runs a few queries and has a couple of variables as a result.

I was trying to echo the variables in the index-template.php in the area kind of set aside for that stuff, below the main above area.

If I try to include/require the stuff.php file, I get 'call to member function query() on NULL.

I know the variables work, as I've tested that.  So, am I including improperly?


include('/home/example.com/files/stuff.php');

Title: Re: I need some help echoing some $variables in the index.template.php
Post by: rcane on April 01, 2023, 02:00:39 PM
query issues, disregard.
Title: Re: I need some help echoing some $variables in the index.template.php
Post by: Kindred on April 01, 2023, 02:08:46 PM
Do note that database queries do not belong in the display templates
Title: Re: I need some help echoing some $variables in the index.template.php
Post by: rcane on April 01, 2023, 02:12:07 PM
Quote from: Kindred on April 01, 2023, 02:08:46 PMDo note that database queries do not belong in the display templates

the query wasn't in the template, if that's what you meant. 

the stuff.php had the connection/query/and stored variables in it. 

if I echo inside stuff.php and run that it shows the queries work (just ->num_rows calls).

If I include stuff.php that's when it gives the error.

Title: Re: I need some help echoing some $variables in the index.template.php
Post by: rcane on April 01, 2023, 02:30:25 PM
I've checked the queries and they do not return null.

for example:
$GETdateun = $connltd->query("

SELECT *  FROM ltd_themes
WHERE `variable` = 'cust_dateun'
AND `value` REGEXP '^[0-9/-]+$'
AND LENGTH(`value`) > 0 ");

   
$final_dateun = ($GETdateun !== NULL) ? $GETdateun : 0;

Title: Re: I need some help echoing some $variables in the index.template.php
Post by: Kindred on April 01, 2023, 02:47:04 PM
This is why you'd load things to the $user_info or $context variable arrays...
Title: Re: I need some help echoing some $variables in the index.template.php
Post by: rcane on April 01, 2023, 02:50:48 PM
Quote from: Kindred on April 01, 2023, 02:47:04 PMThis is why you'd load things to the $user_info or $context variable arrays...

I've not had a need like this before.  Can you point me in the direction to add a those few variables so they're displayable?
Title: Re: I need some help echoing some $variables in the index.template.php
Post by: Arantor on April 01, 2023, 03:55:33 PM
To display them in index.template.php you need to load them on every page, which means editing the core files so that your code runs every page. Hard to know where the correct place for that is.

As for putting the values into $context, it's really no more complex than making sure your code calls 'global $context' to pull it into correct scope, then just $context['final_daterun'] = ($GETdateun !== NULL) ? $GETdateun : 0; or similar.
Title: Re: I need some help echoing some $variables in the index.template.php
Post by: rcane on April 01, 2023, 03:57:29 PM
I'll go experiment.  Something new to learn.

thanks.
Title: Re: I need some help echoing some $variables in the index.template.php
Post by: Doug Heffernan on April 01, 2023, 04:10:15 PM
Why not put in some extra work and do it properly by making a mod out of it? It will be worth the while imo.

You can use the integrate_pre_include hook to load/call/include your custom php file, which should be uploaded inside the /Sources directory. You can also create another template file to hold the display stuff.  This file should be uploaded inside /Themes/default folder.

Then you can use the integrate_load_theme hook for the query and to register the variable for use in the custom template. You can control the display place with a conditional inside the function that calls the integrate_load_theme hook.