Search code examples
phphtmljoomlajoomla3.1

PHP Table Not Created From Syntax


I am attempting to query my SQL Server and generate a table of the results. I have the below syntax, but the table is not being generated. Am I setting this up incorrectly? What should I change so that the table properly generates?

<!DOCTYPE html>
<?php
        $option = array();
        $option['driver'] = 'mssql';
        $option['host'] = '555.24.24.18';
        $option['user'] = 'user';
        $option['password'] = 'password';
        $option['database'] = 'database1';
        $option['prefix'] = '';
        $db = JDatabase::getInstance($option);
        $query = $db->getQuery(true);
?>
<html>
<html>
    <div>
        <table frame="hsides" style="color:#ffffff;" border="1">
            <thead>
                <tr>
                    <th>Header 1 </th>
                    <th>Header 2 </th>
                    <th>Header 3 </th>
                    <th>Header 4 </th>
                    <th>Header 5 </th>
                    <th>Header 6 </th>
                    <th>Header 7 </th>
                    <th>Header 8 </th>
                    <th>Header 9 </th>
                    <th>Header 10 </th>
                    <th>Header 11 </th>
                    <th>Header 12 </th>
                    <th>Header 13 </th>
                    <th>Header 14 </th>
                    <th>Header 15 </th>
                </tr>
            </thead>
        </table>
    </div>
    <div>
        <div>
            <table border="1">
            <?php
                $query1 = //Select statement goes here
                $db->setQuery($query1);
                $query1 = $db->loadObjectList();
                if ($query1) 
                {
                    if ($query1->num_rows > 0) 
                    {
                        echo "<table>";
                        foreach ($query1 as $res) 
                        {
                            print "<tr>";
                            print "<td>" . $res->field1 . "</td>";
                            print "<td>" . $res->field2 . "</td>";
                            print "<td>" . $res->field3 . "</td>";
                            print "<td>" . $res->field4 . "</td>";
                            print "<td>" . $res->field5 . "</td>";
                            print "<td>" . "$" . round($res->field6) . "</td>";
                            print "<td>" . "$" . round($res->field7) . "</td>";
                            print "<td>" . "$" . round($res->field8) . "</td>";
                            print "<td>" . "$" . round($res->field9) . "</td>";
                            print "<td>" . "$" . round($res->field10) . "</td>";
                            print "<td>" . "$" . round($res->field11) . "</td>";
                            print "<td>" . "$" . round($res->field12) . "</td>";
                            print "<td>" . "$" . round($res->field13) . "</td>";
                            print "<td>" . round($res->field14) . "%" . "</td>";
                            print "<td>" . round($res->field15) . "%" . "</td>";
                            print "</tr>";
                        }
                        echo "</table>";
                    } 
                    else          
                    {
                        echo "Empty Table.";
                    }
                }
            ?>
        </table>
    </div>
</div>
</html>

ERROR - when removing the <table> statementsenter image description here

EDIT EDIT EDIT....
Just for clarity's sake this is the current code that I am working with, but NetBeans still presents the error in my above image. -- removed all <div> tags as well as only have a begin and end <html> tag. Also, removed all but 1 <table>

<!DOCTYPE html>
<html>
<?php
    $option = array();
    $option['driver'] = 'mssql';
    $option['host'] = '555.24.24.18';
    $option['user'] = 'user';
    $option['password'] = 'password';
    $option['database'] = 'database1';
    $option['prefix'] = '';
    $db = JDatabase::getInstance($option);
    $query = $db->getQuery(true);
?>
<table frame="hsides" style="color:#ffffff;" border="1">
    <thead>
        <tr>
            <th>Header 1 </th>
            <th>Header 2 </th>
            <th>Header 3 </th>
            <th>Header 4 </th>
            <th>Header 5 </th>
            <th>Header 6 </th>
            <th>Header 7 </th>
            <th>Header 8 </th>
            <th>Header 9 </th>
            <th>Header 10 </th>
            <th>Header 11 </th>
            <th>Header 12 </th>
            <th>Header 13 </th>
            <th>Header 14 </th>
            <th>Header 15 </th>
        </tr>
    </thead>
    <?php
        $query1 = //Select statement goes here
        $db->setQuery($query1);
        $query1 = $db->loadObjectList();
        if ($query1) 
        {
            if ($query1->num_rows > 0) 
            {
                foreach ($query1 as $res) 
                {
                    print "<tr>";
                    print "<td>" . $res->field1 . "</td>";
                    print "<td>" . $res->field2 . "</td>";
                    print "<td>" . $res->field3 . "</td>";
                    print "<td>" . $res->field4 . "</td>";
                    print "<td>" . $res->field5 . "</td>";
                    print "<td>" . "$" . round($res->field6) . "</td>";
                    print "<td>" . "$" . round($res->field7) . "</td>";
                    print "<td>" . "$" . round($res->field8) . "</td>";
                    print "<td>" . "$" . round($res->field9) . "</td>";
                    print "<td>" . "$" . round($res->field10) . "</td>";
                    print "<td>" . "$" . round($res->field11) . "</td>";
                    print "<td>" . "$" . round($res->field12) . "</td>";
                    print "<td>" . "$" . round($res->field13) . "</td>";
                    print "<td>" . round($res->field14) . "%" . "</td>";
                    print "<td>" . round($res->field15) . "%" . "</td>";
                    print "</tr>";
                }
            } 
            else          
            {
                echo "Empty Table.";
            }
        }
    ?>
</table>
</html>

Solution

  • In the above code you have jumped straight to $db = JDatabase::getInstance($option); without including any classes that define JDatabase. HTML page cannot connect to Joomla API directly. You need to call the base classes before executing any Joomla code. You need to change your code like this

    define( '_JEXEC', 1 );
    define( 'DS', DIRECTORY_SEPARATOR );
    define( 'JPATH_BASE', 'C:\xampp\htdocs\joomla' );//YOur Joomla installation Path
    require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
    require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
    require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
    $app = JFactory::getApplication('site');
    $app->initialise();
    

    Rest of your code

    You can call the database using

    $database = JFactory::getDBO();