【问题标题】:PHP Table Not Created未创建 PHP 表
【发布时间】:2017-02-26 00:00:03
【问题描述】:

我正在使用 php 查询一个 sql server 数据库并将结果写在屏幕上。我以为我所有的带有 table & tr/td 的 echo 语句都在创建一个表来显示结果,但是当数据显示在屏幕上时,它看起来像下面这样一起运行:

Employee EmployeeIDPrice1Price2Price3Price4Price5

等没有表结构。我应该在此语法中进行哪些更改,以便创建一个实际的表来显示结果?

if ($result = mssql_execute($proc)) 
{
    $number_of_rows = mssql_num_rows($result);
    if($number_of_rows > 0) {
        echo '<table border="1">';
        echo '<tr>';
        echo '  <th bgcolor="#FFFFFF" >Employee </th>';
        echo '  <th bgcolor="#FFFFFF">EmployeeID </th>';
        echo '  <th bgcolor="#FFFFFF">Price1 </th>';
        echo '  <th bgcolor="#FFFFFF">Price2 </th>';
        echo '  <th bgcolor="#FFFFFF">Price3 </th>';
        echo '  <th bgcolor="#FFFFFF">Price4 </th>';
        echo '  <th bgcolor="#FFFFFF">Price5 </th>';
        echo '  <th bgcolor="#FFFFFF">Price6 </th>';
        echo '  <th bgcolor="#FFFFFF">Price7</th>';
        echo '  <th bgcolor="#FFFFFF">Price8</th>';
        echo '  <th bgcolor="#FFFFFF">Price9</th>';
        echo '  <th bgcolor="#FFFFFF">Price10</th>';
        echo '  <th bgcolor="#FFFFFF">Price11</th>';
        echo '</tr>';  

    while ($Row = mssql_fetch_assoc($result)) 
    {
        echo '<tr><td>'.$Row['Employee'] . 
        '</td><td>'.$Row['EmployeeID'] . 
        '</td><td>'."$".round($Row['Price1']) . 
        '</td><td>'."$".round($Row['Price2']) . 
        '</td><td>'."$".round($Row['Price3']) . 
        '</td><td>'."$".round($Row['Price4']) . 
        '</td><td>'."$".round($Row['Price5']) . 
        '</td><td>'."$".round($Row['Price6']) . 
        '</td><td>'."$".round($Row['Price7']) . 
        '</td><td>'."$".round($Row['Price8']) . 
        '</td><td>'."$".round($Row['Price9']) .
        '</td><td>'."$".round($Row['Price10']) . 
        '</td><td>'."$".round($Row['Price11']) . 
        '</td></tr>';
    }
        echo '</table>';
    }

编辑
这就是我的输出的样子,即使在添加了 cellpadding="5" ... 之后,也不是像我在使用 html 单词 tabletd tr 时所期望的那样明确定义的表格 - 意味着所有数据只是一起流血,而不是像我想象的那样设置表格。

【问题讨论】:

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


    【解决方案1】:

    这不是正确的结构
    一定是这样的:

     <table style="width:100%">
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Age</th>
      </tr>
      <tr>
        <td>Jill</td>
        <td>Smith</td>
        <td>50</td>
      </tr>
      <tr>
        <td>Eve</td>
        <td>Jackson</td>
        <td>94</td>
      </tr>
    </table> 
    

    https://www.w3schools.com/html/html_tables.asp

    【讨论】:

    • 即使我从 sql server 返回结果我也不强制使用 echo 语句?
    • 如果我不使用 echo 语句,当我尝试访问该页面时会收到 500 错误。
    【解决方案2】:

    echo '&lt;table border="1" cellpadding="1" style="width: 100%;"&gt;';

    根据需要增加单元格填充。

    但我建议您阅读 css 作为未来的改进。

    另外,这一行有语法错误:'&lt;/td&gt;&lt;td&gt;."$".round($Row['Price6']) .

    应该是:'&lt;/td&gt;&lt;td&gt;'."$".round($Row['Price6']) . - 自从我发帖后你就解决了这个问题!

    或者更好:'&lt;/td&gt;&lt;td&gt;$'.round($Row['Price6']) .

    【讨论】:

    • 澄清一下,cellpadding="1" 会给你一个像素的单元格周围的空间。 cellpadding="5" 会给你 5 个像素等。这个答案有帮助还是你仍然没有看到它显示你想要的样子?
    • 仍然不是我想要的输出。这一切都只是一起运行,就像书中的文字一样。我想要一张干净有序的桌子,就像下面提供的 w3schools 链接一样。
    • 即使使用 cellpadding="5" ?你能给出你得到的实际 html 输出吗?
    • 是的 - 即使添加 cellpadding="5" 它也会在值之间添加空格,类似于书中单词之间的空格。但不设置表结构。
    • 另外,您可以尝试结合使用这两个答案。 echo '';
    猜你喜欢
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2020-01-28
    • 1970-01-01
    • 2011-09-20
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多