MyBB Community Forums

Full Version: Fetch all arrays from DB table in MyBB
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

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?
In this block:

while($template = $db->fetch_array($query))
{
    $data["title"] = $template["title"];
}

you are assigning the title of the template to an array index in $data named 'title' . . .

If you did:

while($template = $db->fetch_array($query))
{
    $data[] = $template["title"];
}

That would add another index to the $data array with the next available key (numeric) and it would contain the template title only.

This:

while($template = $db->fetch_array($query))
{
    $data[$template['title']] = $template;
}

would store and indexed array of all the template data keyed to the template title in $data.
You are overwritting the array every time you loop.

You should be using this or similar dependingon what data you need
$data[$template["tid"]] = $template["title"];
Thanks guys. Feel utterly stupid for such a simple mistake. I should get more sleep :3 This is what I needed at the end:

		$query = $db->query("SELECT * FROM ".TABLE_PREFIX."templates");
		
		while($template = $db->fetch_array($query))
		{
			$data[] = $template;

		}
		
		foreach($data as $key => $value)
		{
			$template_structure[$key]["tid"] = $value["tid"];
			$template_structure[$key]["title"] = $value["title"];
			$template_structure[$key]["template"] = $value["template"];
			$template_structure[$key]["sid"] = $value["sid"];
			$template_structure[$key]["version"] = $value["version"];
			$template_structure[$key]["status"] = $value["status"];
			$template_structure[$key]["dateline"] = $value["dateline"];	
		}

		echo json_encode($template_structure);