Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: mattizzy on July 08, 2018, 03:29:34 AM

Title: How do I echo Out Data Fetched from database in custom template
Post by: mattizzy on July 08, 2018, 03:29:34 AM
I guess I may be disturbing but I still have a problem.

I want to use the output of this in my custom template but the array is automatically giving each messages a digit.
I used print_r($context['ft']) to check this.

array([0] => array([subject] => test [body] => how to yest) [1] => array([subject] => honey [body] => how to honey))
Let's imagine that's all the posts I've got (2).

I want to get them such that if I use

echo '<div>'.$context['subject'] . $context['body'].'</div>';
It'll style each individual posts.

I can't call all the posts with

$context['ft'][0]['subject']
$context['ft'][1][body']

$context['ft'][1]['subject']
$context['ft'][1]['body']


Here is the main code I used
<?
global $context;
$context['ft'] = array();
$query = $smcFunc['db_query']('', "SELECT * FROM {db_prefix}messages");
while ($row = $smcFunc['db_fetch_assoc']($query))
{
   $context['ft'][]  = array(
      'id' => $row['id_msg'],
                'subject' => $row['subject'],
      'body' => $row['body'],
   );
}

Title: Re: How do I echo Out Data Fetched from database in custom template
Post by: SychO on July 08, 2018, 04:26:10 AM
Use a foreach Loop ?


foreach($context['ft'] as $id=>$data) {
     echo '<div>'.$data['subject'] . $data['body'].'</div>';
}

Title: Re: How do I echo Out Data Fetched from database in custom template
Post by: mattizzy on July 08, 2018, 05:20:38 AM
Thanks, I'll test it