SimplePortal
Support => English Support => Topic started 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
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>';
}
-
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.
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>';
}
-
Usefull....Thank you!
-
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:
$projects_array = array(
$projects_array[] = array(
-
Thanks, I have made those changes.
-
Cool. I'll be marking this topic as solved then. :)