MyBB Community Forums

Full Version: Show database results on template page?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm still new to PHP and MySQL coding, especially when it comes to using it with MyBB.

What I've done is created a new php page for my website and have it pull the coding from one of the global templates to use for the page. What I want to do is then have it pull information from a database table that I've created and display it on the page.

This is the coding I have so far:

<?php

define('IN_MYBB', 1); // (1a)
require "./global.php"; // (1b)

$plugins->add_hook("index_start", "add_dominions", 1000000);
$plugins->run_hooks("index_start");

add_breadcrumb("Test", "test.php"); // (2)

// Build dominions table
function add_dominions()
{

    global $db, $mybb, $dominions;

    $domQuery = $db->query("
            SELECT *
            FROM ".TABLE_PREFIX."dominions
            WHERE discovered = '1'
            ORDER BY dominion ASC
        ");

    while($row = mysqli_fetch_array($domQuery)){

        $domid         = $row['domid'];
        $domname     = $row['dominion'];
        $description     = $row['description'];
        $power         = $row['power'];
        $control     = $row['control'];

        $dominions = $domname;

    }

}

eval("\$test = \"".$templates->get("test")."\";"); // (3)
output_page($test); // (4)
?>

When I put {$dominions} onto the template, it will only show the last result in the table, it doesn't keep the while loop going. How can I code this so it'll show all the results, rather than just the one?
All times the loop change the content of the variable $domions, you have to append the name:
$dominions .= $domname;
Before the while add this code:
$dominions = '';
Should also use the database class for fetching the data. Instead of using mysqli_fetch_array($query) simply use $db->fetch_array($query).