Advertisement:

Author Topic: How do I return rows from $smcFunc query to my Custom Template  (Read 431 times)

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 75
  • Gender: Male
    • mattizzy.okeke on Facebook
    • @mattizzyo on Twitter
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.

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 75
  • Gender: Male
    • mattizzy.okeke on Facebook
    • @mattizzyo on Twitter
Re: How do I return rows from $smcFunc query to my Custom Template
« Reply #1 on: July 07, 2018, 07:19:13 AM »
I'm running my custom area I.e action=moderate;area=featured
I am only an SMF Addict. I think I took overdose.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 69,615
    • StoryBB/StoryBB on GitHub
Re: How do I return rows from $smcFunc query to my Custom Template
« Reply #2 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.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 75
  • Gender: Male
    • mattizzy.okeke on Facebook
    • @mattizzyo on Twitter
Re: How do I return rows from $smcFunc query to my Custom Template
« Reply #3 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
I am only an SMF Addict. I think I took overdose.

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 75
  • Gender: Male
    • mattizzy.okeke on Facebook
    • @mattizzyo on Twitter
Re: How do I return rows from $smcFunc query to my Custom Template
« Reply #4 on: July 07, 2018, 07:26:49 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.

Offline d3vcho(void)

  • Sempiterno
  • Lead Localizer
  • SMF Hero
  • *
  • Posts: 3,027
  • Gender: Male
    • frandominguez03 on GitHub
Re: How do I return rows from $smcFunc query to my Custom Template
« Reply #5 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 :)
"Too weird to live but much too rare to die"

d3vcho(void)

Offline Aleksi "Lex" Kilpinen

  • A Peculiar Finn
  • Lead Support Specialist
  • SMF Super Hero
  • *
  • Posts: 17,574
  • Gender: Male
  • Don't worry, I'm n00b friendly
    • Aleksi.Kilpinen on Facebook
    • aleksi-kilpinen on LinkedIn
Re: How do I return rows from $smcFunc query to my Custom Template
« Reply #6 on: July 07, 2018, 07:29:20 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.
A Finnish Support Specialist
 Happily running multiple SMF 2.0 installations.

How you can help SMF

"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

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 69,615
    • StoryBB/StoryBB on GitHub
Re: How do I return rows from $smcFunc query to my Custom Template
« Reply #7 on: July 07, 2018, 07:32:04 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:

Code: [Select]
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.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 75
  • Gender: Male
    • mattizzy.okeke on Facebook
    • @mattizzyo on Twitter
Re: How do I return rows from $smcFunc query to my Custom Template
« Reply #8 on: July 07, 2018, 07:53:24 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:

Code: [Select]
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.

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 75
  • Gender: Male
    • mattizzy.okeke on Facebook
    • @mattizzyo on Twitter
Re: How do I return rows from $smcFunc query to my Custom Template
« Reply #9 on: July 11, 2018, 01:39:20 PM »
I figured it out. Thanks.
I am only an SMF Addict. I think I took overdose.