How do I return rows from $smcFunc query to my Custom Template

Started by mattizzy, July 07, 2018, 07:18:05 AM

Previous topic - Next topic

mattizzy

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.
I am only an SMF Addict. I think I took overdose.

mattizzy

I'm running my custom area I.e action=moderate;area=featured
I am only an SMF Addict. I think I took overdose.

Arantor

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.

mattizzy

It is global, can you look at The code I posted above. Sorry for the immediate stuff
I am only an SMF Addict. I think I took overdose.

mattizzy

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.
I am only an SMF Addict. I think I took overdose.

d3vcho

You'll get help as soon as we can, but please avoid double posting when not necessary. Thank you :)
"Greeting Death as an old friend, they departed this life as equals"

Aleksi "Lex" Kilpinen

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.
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Arantor

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.

mattizzy

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
I am only an SMF Addict. I think I took overdose.

mattizzy

I am only an SMF Addict. I think I took overdose.

Advertisement: