2014-03-27, 04:49 PM
Excuses if this thread is posted in the wrong forum, however, the developments forums tend to not get any replies lately.
I'm running into an issue when fetching all rows from MyBB templates table and processing them. My code is as follows:
$data only prints the title of the last row of the table as an array instead of all 800+ rows.
Above code returns:
instead of:
When I do this:
I'm running into an issue when fetching all rows from MyBB templates table and processing them. My code is as follows:
DEFINE("IN_MYBB", 1);
require_once("global.php");
global $mybb, $db, $settings, $templates, $lang;
$query = $db->query("SELECT * FROM ".TABLE_PREFIX."templates");
while($template = $db->fetch_array($query))
{
$data["title"] = $template["title"];
}
print_r($data);
$data only prints the title of the last row of the table as an array instead of all 800+ rows.
Above code returns:
Array
(
[tid] => 1216
)
instead of:
Array
(
[tid] => 1216
)
instead of:
Array
(
[tid] => 1216
),
Array
(
[tid] => 1215
),
Array
(
[tid] => 1214
),
etc
I understand that this is because $data is replaced each time the while loops, however, I don't know how to work around this issue.When I do this:
while($template = $db->fetch_array($query))
{
print_r($template);
}
It prints all the +800 arrays, but all as separate arrays. The comma ',' is missing after each output creating:Array
(
[tid] => 1216
)
Array
(
[tid] => 1215
)
Array
(
[tid] => 1214
)
etc
My final goal is encoding all the arrays into JSON format. When I try:while($template = $db->fetch_array($query))
{
echo json_encode($template);
}
it outputs all the arrays into JSON format, however, the "," is missing between each "{}" creating an invalid JSON string. I understand this is a very basic concept/problem, but not having worked with MyBB in years it's killing me. What am I doing wrong?