【问题标题】:PHP To Show 2 Tables On One PagePHP在一页上显示2个表
【发布时间】:2017-11-29 08:00:48
【问题描述】:

我正在使用这种语法在我的页面上显示一个 PHP 表。我现在需要在该表正上方添加第二个表,但我尝试的所有语法都会引发 500 错误。如何通过 1 个与 MSSQL 的连接运行 2 个 Select 语句并填充 2 个单独的 html 表?

    $option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'UserName';
$option['password'] = 'Password';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT name, hiredate, bday, payrate, hourlypay from HRData ORDER BY name ASC";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query) 
{
    ?>
    <table border="1">
    <thead>
    <tr>
    <th>Name </th>
    <th>Hire Date </th>
    <th>Birthday </th>
    <th>Pay Rate </th>
    <th>hourlypay </th>
    </tr>
    </thead>
    <?php
    foreach ($query as $res) 
    {
        print "<tr>";
        print "<td>" . $res->name . "</td>";
        print "<td>" . $res->hiredate . "</td>";
        print "<td>" . $res->bday . "</td>";
        print "<td>" . $res->payrate . "</td>";
        print "<td>" . $res->hourlypay . "</td>";
        print "</tr>";
    }
}

编辑
这是我正在尝试调整的语法,但我不断收到 500 错误

    $option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'UserName';
$option['password'] = 'Password';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query) 
{
    ?>
    <table border="1">
    <thead>
    <tr>
    <th>Name </th>
    <th>YTD Pay </th>
    </tr>
    </thead>
    <?php
    foreach ($query as $res) 
    {
        print "<tr>";
        print "<td>" . $res->name . "</td>";
        print "<td>" . "$" . round($res->PayYTD) . "</td>";
        print "</tr>";
    }
}
<br><br><br>
//Query
$query = $db->getQuery(true);
$query = "SELECT name, hiredate, bday, payrate, hourlypay from HRData ORDER BY name ASC";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query) 
{
    ?>
    <table border="1">
    <thead>
    <tr>
    <th>Name </th>
    <th>Hire Date </th>
    <th>Birthday </th>
    <th>Pay Rate </th>
    <th>hourlypay </th>
    </tr>
    </thead>
    <?php
    foreach ($query as $res) 
    {
        print "<tr>";
        print "<td>" . $res->name . "</td>";
        print "<td>" . $res->hiredate . "</td>";
        print "<td>" . $res->bday . "</td>";
        print "<td>" . $res->payrate . "</td>";
        print "<td>" . $res->hourlypay . "</td>";
        print "</tr>";
    }
}

【问题讨论】:

  • 在此之后像往常一样运行下一个查询而不关闭连接。它应该可以工作。
  • 我是否使用相同的变量 $query?就像这样 $db = JDatabase::getInstance($option); $query = $db->getQuery(true);
  • 是的,您可以使用相同的变量。更改查询,和往常一样,您需要编写这些语句$db-&gt;setQuery($query);$query = $db-&gt;loadObjectList();
  • 您似乎对 setQuery 和 getQuery 直接运行 SQL 语句的作用感到困惑。你应该遵循这里的语法:docs.joomla.org/Selecting_data_using_JDatabase
  • @BellHopByDayAmetuerCoderByNigh 您不能只在 php 文件中正确处理纯 html。您要么必须回显它,要么像echo "&lt;br&gt;&lt;br&gt;&lt;br&gt;"; 一样打印它。您要么没有正确粘贴源代码,要么粘贴后正在编辑源代码。

标签: php html joomla html-table joomla3.0


【解决方案1】:

您遇到的问题是您错误地使用了调用。

$query = $db->getQuery(true);
$query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
$db->setQuery($query);

第一行将创建一个对象,不管是哪个。该对象将在 $query 中。

第二行会立即销毁对象并给$query赋值一个字符串(这是不正确的)。

第三行期望一个对象作为setQuery的参数,但不幸的是它是一个字符串!错误。

如果你想让它正常工作,那么你需要正确使用 $query 中的对象。

我不是 Joomla 专家,因此我将您链接到如何正确执行此操作的页面:https://docs.joomla.org/Selecting_data_using_JDatabase

【讨论】:

    【解决方案2】:

    将输出放入变量中。

    $StringOut = '';
    $StringOut .=     '<table border="1">
        <thead>
        <tr>
        <th>Name </th>
        <th>Hire Date </th>
        <th>Birthday </th>
        <th>Pay Rate </th>
        <th>hourlypay </th>
        </tr>
        </thead>';
    
        foreach ($query as $res) 
        {
            $StringOut .=  "<tr>";
            $StringOut .=  "<td>" . $res->name . "</td>";
            $StringOut .=  "<td>" . $res->hiredate . "</td>";
            $StringOut .=  "<td>" . $res->bday . "</td>";
            $StringOut .=  "<td>" . $res->payrate . "</td>";
            $StringOut .=  "<td>" . $res->hourlypay . "</td>";
            $StringOut .=  "</tr>";
            }
    $StringOut .=     '</table>';
    #Do other logic to retrieve first table. 
    #You could put it in a different variable if you like. And print when and whereever you wish.
    
    echo $StringOut;
    #Optionally close connection, done.
    

    【讨论】:

    • 什么?为什么要投反对票?这是OP问的。一种控制输出并在当前表之前打印另一个表的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2016-03-26
    • 2021-02-18
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多