SimplePortal

Support => English Support => Topic started by: SniffTheGlove on July 17, 2013, 01:04:08 PM

Title: Running a SMF query from a SimplePortal Page
Post by: SniffTheGlove on July 17, 2013, 01:04:08 PM
I have been trying today to run a query in a Simple Portal page that I have created. I am going around and around and nowhere.

Can some help please....

I am trying to use the SMF built in functions and same coding format but I am getting stuck on the simple foreach statements.

I am pulling data out of a projects table (id,timestamp and project). The SMF query works... I have put the data into an array called projects array. but the last few lines (ie the foreach) I can not seem to actually print out the data and I get meanlings single characters.

Ideally I should get something like....
id: 1
TS: 1374080590
Project: BatCream
id: 2
TS: 1374080590
Project: Stonehaven
id: 3
TS: 1374080590
Project: WickerMan

Code: [Select]
global $db_prefix, $smcFunc;
$request = $smcFunc['db_query']('', '
SELECT *
FROM projects
ORDER BY timestamp',
array(
)
        );
$projects_array = array();
while ($row = $smcFunc['db_fetch_assoc']($request)) {
$projects_array = array(
'id' => $row['id'],
'timestamp' => $row['timestamp'],
'project' => $row['project']
);
}
$smcFunc['db_free_result']($request);


foreach ($projects_array as $project => $array) {
echo 'id: '.$array['id'].'<br>';
echo 'TS: '.$array['timestamp'].'<br>';
echo 'Project: '.$array['project'].'<br>';
}
Title: Re: Running a SMF query from a SimplePortal Page
Post by: SniffTheGlove on July 23, 2013, 08:33:53 AM
Understand what was need, I got help from a user on another website and I am posting the code here as it might help others.
Code: [Select]
global $db_prefix, $context, $smcFunc;

$request = $smcFunc['db_query']('', '
SELECT
*
FROM
projects
ORDER BY timestamp',
array(
)
);

$context['projects_array'] = array();
while ($row = $smcFunc['db_fetch_assoc']($request)) {
$context['projects_array'][] = array(
'id' => $row['id'],
'project' => $row['project'],
'timestamp' => $row['timestamp']
);
}
$smcFunc['db_free_result']($request);

foreach ($context['projects_array'] as $project) {
echo 'id: '.$project['id'].'<br>';
echo 'TS: '.$project['timestamp'].'<br>';
echo 'Project: '.$project['project'].'<br>';
}
Title: Re: Running a SMF query from a SimplePortal Page
Post by: Divecall on July 23, 2013, 05:28:12 PM
Usefull....Thank you!
Title: Re: Running a SMF query from a SimplePortal Page
Post by: [SiNaN] on July 30, 2013, 05:35:36 AM
The problem with your initial code was that you were resetting the $projects_array in each loop when you are gathering the data from db. This change to your initial code would have fixed it as well:

Code: (Find) [Select]
$projects_array = array(
Code: (Replace) [Select]
$projects_array[] = array(
Title: Re: Running a SMF query from a SimplePortal Page
Post by: SniffTheGlove on August 02, 2013, 02:53:17 PM
Thanks, I have made those changes.
Title: Re: Running a SMF query from a SimplePortal Page
Post by: [SiNaN] on August 14, 2013, 11:39:33 AM
Cool. I'll be marking this topic as solved then. :)
SimplePortal 2.3.8 © 2008-2024, SimplePortal