Advertisement:

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

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 29
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.

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 29
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

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 68,793
    • Arantor 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.
To assume is to hope that those who came before had the presence of mind and capacity to implement the dreams of those who would come after.

Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 29
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

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 29
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.

Online d3vcho(void)

  • Lead Localizer
  • SMF Hero
  • *
  • Posts: 2,654
  • Gender: Male
  • Be prepared for hell
    • d3vcho 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 :)

Offline Aleksi "Lex" Kilpinen

  • Lead Support Specialist
  • SMF Super Hero
  • *
  • Posts: 17,077
  • 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.

"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: 68,793
    • Arantor 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.
To assume is to hope that those who came before had the presence of mind and capacity to implement the dreams of those who would come after.

Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 29
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

Offline mattizzy

  • Semi-Newbie
  • *
  • Posts: 29
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.