Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: mattizzy on July 07, 2018, 07:18:05 AM

Title: How do I return rows from $smcFunc query to my Custom Template
Post by: mattizzy on July 07, 2018, 07:18:05 AM
Hello, I'm creating a mod that features posts to homepage with thumbnail and excerpt just like WordPress.

Now, I ran a query e.g
function Featured(){
$request = $smcFunc['db_query']('','
SELECT * FROM {db_prefix}messages' LIMIT 10);
while($row = $smcFunc['db_fetch_assoc']($request))
$context['ft] = $row;
}
Featured();


The template I created is actually for modificationcenter, it is supposed to display all post there but it does not show.
Then I ran to my template file name Featured.template.php

template_featured_main(){
echo $context['ft'];
}

Nothing shows. I need help immediately. Thank you.
Title: Re: How do I return rows from $smcFunc query to my Custom Template
Post by: mattizzy on July 07, 2018, 07:19:13 AM
I'm running my custom area I.e action=moderate;area=featured
Title: Re: How do I return rows from $smcFunc query to my Custom Template
Post by: Arantor on July 07, 2018, 07:23:16 AM
In both cases you need to make $context global.

Also please note, everyone here is a volunteer, there is no such thing as 'immediate' help, it just happened that I checked and responded. Again I would recommend looking through existing code which will demonstrate all of the things SMF does.
Title: Re: How do I return rows from $smcFunc query to my Custom Template
Post by: mattizzy on July 07, 2018, 07:25:50 AM
It is global, can you look at The code I posted above. Sorry for the immediate stuff
Title: Re: How do I return rows from $smcFunc query to my Custom Template
Post by: mattizzy on July 07, 2018, 07:26:49 AM
Quote from: Arantor on July 07, 2018, 07:23:16 AM
In both cases you need to make $context global.

Also please note, everyone here is a volunteer, there is no such thing as 'immediate' help, it just happened that I checked and responded. Again I would recommend looking through existing code which will demonstrate all of the things SMF does.

I couldn't sleep for 5hrs trying to fix it. So it became frustrating.
Title: Re: How do I return rows from $smcFunc query to my Custom Template
Post by: d3vcho on July 07, 2018, 07:28:14 AM
You'll get help as soon as we can, but please avoid double posting when not necessary. Thank you :)
Title: Re: How do I return rows from $smcFunc query to my Custom Template
Post by: Aleksi "Lex" Kilpinen on July 07, 2018, 07:29:20 AM
Quote from: mattizzy on July 07, 2018, 07:26:49 AM
Quote from: Arantor on July 07, 2018, 07:23:16 AM
In both cases you need to make $context global.

Also please note, everyone here is a volunteer, there is no such thing as 'immediate' help, it just happened that I checked and responded. Again I would recommend looking through existing code which will demonstrate all of the things SMF does.

I couldn't sleep for 5hrs trying to fix it. So it became frustrating.
Yet you are repeating behavior Arantor was referring to. ;) Arantor meant you should allow for time to pass, before expecting replies. That is the nature of completely volunteer run support.
Title: Re: How do I return rows from $smcFunc query to my Custom Template
Post by: Arantor on July 07, 2018, 07:32:04 AM
Quote from: mattizzy on July 07, 2018, 07:25:50 AM
It is global, can you look at The code I posted above. Sorry for the immediate stuff

No, it isn't. If that's your exact code, it will guaranteed fail since neither $context nor $smcFunc are pulled into scope. Not to mention the multiple syntax errors in that code.

You might have more luck with:


function Featured()
{
global $smcFunc, $context;

$request = $smcFunc['db_query']('','
SELECT *
FROM {db_prefix}messages
ORDER BY id_msg DESC
LIMIT 1');
while($row = $smcFunc['db_fetch_assoc']($request))
{
$context['ft'] = $row;
}
$smcFunc['db_free_result']($request);
}
Featured();


function template_featured_main() {
echo $context['ft'];
}


Not that any of that will work the way you expect, because the code is trying to print an array which it won't be able to do (for debugging, try var_dump or print_r, and then print the bits of the array you care about)

Or the fact you were trying to get 10 rows out of the database but only ever keeping one of them (and you were getting the oldest 10 messages in the database, which seems unlikely to be what you're trying to get)

Posting because you're getting frustrated only guarantees you getting more frustrated. Go have some rest, come back when you're feeling more positive.
Title: Re: How do I return rows from $smcFunc query to my Custom Template
Post by: mattizzy on July 07, 2018, 07:53:24 AM
Quote from: Arantor on July 07, 2018, 07:32:04 AM
Quote from: mattizzy on July 07, 2018, 07:25:50 AM
It is global, can you look at The code I posted above. Sorry for the immediate stuff

No, it isn't. If that's your exact code, it will guaranteed fail since neither $context nor $smcFunc are pulled into scope. Not to mention the multiple syntax errors in that code.

You might have more luck with:


function Featured()
{
global $smcFunc, $context;

$request = $smcFunc['db_query']('','
SELECT *
FROM {db_prefix}messages
ORDER BY id_msg DESC
LIMIT 1');
while($row = $smcFunc['db_fetch_assoc']($request))
{
$context['ft'] = $row;
}
$smcFunc['db_free_result']($request);
}
Featured();


function template_featured_main() {
echo $context['ft'];
}


Not that any of that will work the way you expect, because the code is trying to print an array which it won't be able to do (for debugging, try var_dump or print_r, and then print the bits of the array you care about)

Or the fact you were trying to get 10 rows out of the database but only ever keeping one of them (and you were getting the oldest 10 messages in the database, which seems unlikely to be what you're trying to get)

Posting because you're getting frustrated only guarantees you getting more frustrated. Go have some rest, come back when you're feeling more positive.

Thank you, I'll just get some rest
Title: Re: How do I return rows from $smcFunc query to my Custom Template
Post by: mattizzy on July 11, 2018, 01:39:20 PM
I figured it out. Thanks.