【问题标题】:Populating Web Table with MySQL data使用 MySQL 数据填充 Web 表
【发布时间】:2018-05-31 05:13:16
【问题描述】:

我在这个网站上得到了很多答案,但是我根据他们的建议对我的代码所做的更改仍然没有给我带来解决方案。

尝试使用 PHP 调用 MySQL 数据库,以便在此表中填充办公室内部网页的信息。这是我现在拥有的代码:

<?php
    require_once 'login.php';
    $db_server = mysql_connect($db_hostname, $db_username, $db_password);

    if (!$db_server) die("Unable to connect to MySQL: " mysql_error());

    mysql_select_db($db_database, $db_server)
        or die("Unable to select database: " . mysql_error());

    $query = "Select * FROM tablename";
    $result = mysql_query($query);
    ?>
                        <table class="mainTable TableTools table-hover table-condensed table-small dataTable sortable" id="DataTables_Table_0" aria-describedby="DataTables_Table_0_info">
                        <tr>
                            <td class="ui-state-default" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Blade Name:activate to sort column ascending" style="width: 82px;">
                                <div class="DataTables_sort_wrapper">
                                    Blade Name
                                    <span class="DataTables_sort_icon css_right ui-icon ui-icon-carat-2-n-s"></span>
                                </div>
                            </td>
                            <td class="ui-state-default" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Alias:activate to sort column ascending" style="width: 82px;">
                                <div class="DataTables_sort_wrapper">
                                    Alias
                                    <span class="DataTables_sort_icon css_right ui-icon ui-icon-carat-2-n-s"></span>
                                </div>
                            </td>
                            <td class="ui-state-default" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Chassis Name:activate to sort column ascending" style="width: 82px;">
                                <div class="DataTables_sort_wrapper">
                                    Chassis Name
                                    <span class="DataTables_sort_icon css_right ui-icon ui-icon-carat-2-n-s"></span>
                                </div>
                            </td>
                            <td class="ui-state-default" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="PDU:activate to sort column ascending" style="width: 82px;">
                                <div class="DataTables_sort_wrapper">
                                    PDU
                                    <span class="DataTables_sort_icon css_right ui-icon ui-icon-carat-2-n-s"></span>
                                </div>
                            </td>
                            <td class="ui-state-default" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Circuit Info:activate to sort column ascending" style="width: 82px;">
                                <div class="DataTables_sort_wrapper">
                                    Circuit Info
                                    <span class="DataTables_sort_icon css_right ui-icon ui-icon-carat-2-n-s"></span>
                                </div>
                            </td>
                            <td class="ui-state-default" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Description:activate to sort column ascending" style="width: 82px;">
                                <div class="DataTables_sort_wrapper">
                                    Description
                                    <span class="DataTables_sort_icon css_right ui-icon ui-icon-carat-2-n-s"></span>
                                </div>
                            </td>
                            <td class="ui-state-default" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Degraded:activate to sort column ascending" style="width: 82px;">
                                <div class="DataTables_sort_wrapper">
                                    Degraded
                                    <span class="DataTables_sort_icon css_right ui-icon ui-icon-carat-2-n-s"></span>
                                </div>
                            </td>
                            <td class="ui-state-default" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Blade Type:activate to sort column ascending" style="width: 82px;">
                                <div class="DataTables_sort_wrapper">
                                    Blade Type
                                    <span class="DataTables_sort_icon css_right ui-icon ui-icon-carat-2-n-s"></span>
                                </div>
                            </td>
                            <td class="ui-state-default" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Instance:activate to sort column ascending" style="width: 82px;">
                                <div class="DataTables_sort_wrapper">
                                    Instance
                                    <span class="DataTables_sort_icon css_right ui-icon ui-icon-carat-2-n-s"></span>
                                </div>
                            </td>
                        </tr>
       <?php
        while ($row = mysql_fetch_array($result)) {
            echo "<tr>";
            echo "<td>" . $row[ServerName] . "</td>";
            echo "<td>" . $row[EncolsureName] . "</td>";
            echo "<td>" . $row[Rackname] . "</td>";
            echo "<td>" . $row[Health_State] . "</td>";
            echo "</tr>";
        }

       ?>

表格的东西有点乱,因为我是从一个已经存在的页面改编而来的,所以我试图模拟它的外观。你们中的任何人都可以指出我要去哪里错了吗?我知道 HTML,但我对 PHP 非常缺乏经验,所以我觉得我错过了一些让我眼前一亮的东西。

编辑:抱歉,第一次在 SE 上发帖(如果你不知道的话),我已经为此苦苦挣扎了一段时间,所以我空白了。问题是页面显示表头,但没有其他内容。

【问题讨论】:

  • 你还没有说你遇到了什么问题。
  • 你有什么问题??任何错误!是的,只是一个快速的建议,使用 $row['ServerName'] 而不是 $row[ServerName]
  • 为什么你的原始代码中有所有这些类?如果您使用的是 DataTables,那么它会在页面加载时自动为您添加。
  • 您将您的行视为 constants $row[ServerName] 等 - 使用引号 $row['ServerName'] 等 - 启用 error reporting 会触发该错误。

标签: php mysql html-table


【解决方案1】:

在我看来,您正在复制一个使用 dataTables jQuery 插件的表。您不需要在所有这些类中硬编码,插件会为您完成。

如果您希望自己的桌子和他们的一样,请查看jQuery,尤其是dataTables,并阅读文档。

应该是这样的(链接必备文件后):

<table id="mytable">
    <thead>
        <tr>
            <th>header1</th>
            <th>header2</th>
            <!-- etc -->
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>content1</td>
            <td>content2</td>
            <!-- etc -->
        </tr>
        <tr>
            <td>content1</td>
            <td>content2</td>
            <!-- etc -->
        </tr>
    </tbody>
</table>

还有 javascript:

<script>
    $(document).ready(function(){
        $('#mytable').dataTable();
    });
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 2016-06-19
    • 2013-04-04
    • 2014-08-30
    • 1970-01-01
    相关资源
    最近更新 更多