PHP表不是从语法创建的

时间:2017-05-15 22:37:55

标签: php html joomla joomla3.1

我正在尝试查询我的SQL Server并生成结果表。我有以下语法,但表没有生成。我错误地设置了吗?我应该更改什么以便正确生成表格?

<!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>

错误 - 删除<table>语句enter image description here

编辑编辑编辑.... 为了清楚起见,这是我正在使用的当前代码,但NetBeans仍然在我上面的图像中显示错误。 - 删除了所有<div>代码,并且只有一个开始和结束<html>代码。此外,删除了除{1}}

之外的所有内容
<table>

1 个答案:

答案 0 :(得分:0)

在上面的代码中,你直接跳到$ db = JDatabase :: getInstance($ option);不包括任何定义JDatabase的类。 HTML页面无法直接连接到Joomla API。您需要在执行任何Joomla代码之前调用基类。您需要像这样更改代码

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();

您的其余代码

您可以使用

调用数据库
$database = JFactory::getDBO();