如何在同一个PHP页面上显示两个不同的MySQL表

问题描述 投票:0回答:1

我有两个我正在阅读的sql表,我想将每个表显示为它自己的独立表。首先是内部,然后是外部。而且我希望每个人都有一个简单的标题。例如:

内部

Table here

外部

Table here

然而,正在发生的事情是INTERNAL和EXTERNAL标题一直出现在同一行,或者两者都出现在表格之前。像这样:

内部

外部

Internal Table here
External Table here

我已经尝试将标题包含在php标签中。这些是目前注释掉的行。而且我也试过在php标签之外添加html,如下所示:

<br><br>
<b>INTERNAL</b>
<br><br>

<?php Internal Table here ?>

然后:

<br><br>
<b>EXTERNAL</b>
<br><br>

<?php External Table here ?>

但这仍然导致两个标题首先出现在表格之前。是否在PHP之前处理html?我必须过于线性地解释这个,就像一个脚本,因为它肯定没有按照它在我的页面上编码的顺序进行处理。

这是我的代码,其标题已注释掉。这些表格夹在一起,告诉我它们不会被解释为页面上的两个不同元素。我是否需要将每个标签放入自己的标签中?

<?php
    $internal = getInternalNetworkTable();
    if ($internal->num_rows > 0){


            //echo " <b>INTERNAL</b>";
            echo "<table cellspacing=\"0\">";
            echo "<tr>";
            echo " <th><b>Device</b></th>"; 
            echo " <th><b>Label</b></th>";
            echo " <th><b>Address</b></th>";
            echo " <th><b>Type</b></th>";
            echo " <th><b>DisplayNumber</b></th>";
            echo " <th><b>Wiki</b></th>";
            echo "</tr>";
            while ($row = $internal->fetch_assoc()){
                    echo "<tr>";
                    echo ("<td>" . $row["Device"] ."</td>");
                    echo ("<td>" . $row["Label"]."</td>");
                    echo ("<td>" . $row["Address"]."</td>");
                    echo ("<td>" . $row["Type"]."</td>");
                    echo ("<td>" . $row["DisplayNumber"]."</td>");
                    echo ("<td>" . $row["Wiki"]."</td>");
                    echo "</tr>";
            }           

    } else {
        echo "No results founds";
    }

?>

<?php
    $external = getExternalNetworkTable();
    if ($external->num_rows > 0){


            //echo " <b>EXTERNAL</b>";      
            echo "<table cellspacing=\"0\">";
            echo "<tr>";
            echo " <th><b>Device</b></th>"; 
            echo " <th><b>Label</b></th>";
            echo " <th><b>Address</b></th>";
            echo " <th><b>Type</b></th>";
            echo " <th><b>DisplayNumber</b></th>";
            echo " <th><b>Wiki</b></th>";
            echo "</tr>";
            while ($row = $external->fetch_assoc()){
                    echo "<tr>";
                    echo ("<td>" . $row["Device"] ."</td>");
                    echo ("<td>" . $row["Label"]."</td>");
                    echo ("<td>" . $row["Address"]."</td>");
                    echo ("<td>" . $row["Type"]."</td>");
                    echo ("<td>" . $row["DisplayNumber"]."</td>");
                    echo ("<td>" . $row["Wiki"]."</td>");
                    echo "</tr>";

            }               

    } else {
        echo "No results founds";
    }
$conn->close();
?>  

编辑:需要添加:

echo "</table>";

新代码:

<?php
    $internal = getNovatoInternalNetworkTable();
    if ($internal->num_rows > 0){


            echo " <b>INTERNAL</b>";
            echo "<table cellspacing=\"0\">";
            echo "<tr>";
            echo " <th><b>Device</b></th>"; 
            echo " <th><b>Label</b></th>";
            echo " <th><b>Address</b></th>";
            echo " <th><b>Type</b></th>";
            echo " <th><b>DisplayNumber</b></th>";
            echo " <th><b>Wiki</b></th>";
            echo "</tr>";
            while ($row = $internal->fetch_assoc()){
                    echo "<tr>";
                    echo ("<td>" . $row["Device"] ."</td>");
                    echo ("<td>" . $row["Label"]."</td>");
                    echo ("<td>" . $row["Address"]."</td>");
                    echo ("<td>" . $row["Type"]."</td>");
                    echo ("<td>" . $row["DisplayNumber"]."</td>");
                    echo ("<td>" . $row["Wiki"]."</td>");
                    echo "</tr>";

            }
                echo "</table>";

    } else {
        echo "No results founds";
    }

?>

<?php
    $external = getNovatoExternalNetworkTable();
    if ($external->num_rows > 0){


            echo " <b>EXTERNAL</b>";        
            echo "<table cellspacing=\"0\">";
            echo "<tr>";
            echo " <th><b>Device</b></th>"; 
            echo " <th><b>Label</b></th>";
            echo " <th><b>Address</b></th>";
            echo " <th><b>Type</b></th>";
            echo " <th><b>DisplayNumber</b></th>";
            echo " <th><b>Wiki</b></th>";
            echo "</tr>";
            while ($row = $external->fetch_assoc()){
                    echo "<tr>";
                    echo ("<td>" . $row["Device"] ."</td>");
                    echo ("<td>" . $row["Label"]."</td>");
                    echo ("<td>" . $row["Address"]."</td>");
                    echo ("<td>" . $row["Type"]."</td>");
                    echo ("<td>" . $row["DisplayNumber"]."</td>");
                    echo ("<td>" . $row["Wiki"]."</td>");
                    echo "</tr>";

            }
                echo "</table>";

    } else {
        echo "No results founds";
    }
$conn->close();
?>  
php html mysql
1个回答
1
投票

你在两个表格中都缺少</table>

因此,您的表将相互合并。

    echo '</table>';

在你的两个} else {之前应该做的伎俩

© www.soinside.com 2019 - 2024. All rights reserved.