如何使用php代码从数据库中获取数据并将类似的国家相关按钮移动到特定的表列?
我不熟悉PHP编码,我正在开发仪表板来显示所有linux服务器的状态。我们在5个国家拥有大量服务器 我用所有服务器及其各自的国家数据填充了一个数据库表 现在我的要求是,我需要在我的网页上显示5列表格,所有与同一国家相关的服务器都应该在同一列中打印。为此,我使用了下面的代码。但它在表外打印按钮(目前我只在下表中填充了新加坡服务器) 如果你需要更详细的信息,请告诉我如何使用php代码从数据库中获取数据并将类似的国家相关按钮移动到特定的表列?,php,Php,我不熟悉PHP编码,我正在开发仪表板来显示所有linux服务器的状态。我们在5个国家拥有大量服务器 我用所有服务器及其各自的国家数据填充了一个数据库表 现在我的要求是,我需要在我的网页上显示5列表格,所有与同一国家相关的服务器都应该在同一列中打印。为此,我使用了下面的代码。但它在表外打印按钮(目前我只在下表中填充了新加坡服务器) 如果你需要更详细的信息,请告诉我 <?php $conn = new mysqli('localhost', 'instadm','welcome','
<?php
$conn = new mysqli('localhost', 'instadm','welcome','dashboard');
if ($conn->connect_error){
die("Connection error: " . $conn-> connect_error);
}
$result = $conn-> query("select hostname,ip,status,country from servers");
if ($result->num_rows > 0) {
echo "<font color='Orange'><b>ENT Zone:</b></font><br>
<table class='style1' height='100' width='1300' border='1' cellspacing='1'>
<tr align='center' height='10' border='1'>
<td width=500><font color='black'>Singapore</font></td>
<td widht =200><font color='white'>China</font></td>
<td width=200><font color='white'>India</font></td>
<td width='200'><font color='white'>Indonesia</font></td>
<td width='200'><font color='white'>Taiwan</font></td>
</tr>
<tr align='center'>
<td width=500>";
function singapore_server($country,$status,$caption){
if($status == 'Y'){
echo "<abbr title='$caption'><button class='button' style='background-color:#43D216;padding: 7px 10px;border-radius: 50px;cursor: pointer;border: none;'></button></abbr> ";
}
else {
echo "<abbr title='$caption'><button class='button' style='background-color:red;padding: 7px 10px;border-radius: 50px;cursor: pointer;border: none;'></button></abbr> ";
}
};
echo "</td>
<td width =200>
China
</td>
<td width=200>
India
</td>
<td width=200>
Indonesia
</td>
<td width=200>
Taiwan
</td>
</tr>
</table>";
while ($row = $result->fetch_assoc()){
$country=$row['country'];
$status=$row['status'];
$hostname=$row['hostname'];
$ip=$row['ip'];
$caption="$hostname - $ip";
if ($country == 'Singapore'){
singapore_server($country,$status,$caption);
}
}
}
?>
您正在希望回显的位置编写函数,但在您调用此函数的位置,回显
完成。您应该在希望echo
按钮的位置调用函数(您可以将函数移到HTML之外):
function新加坡_服务器($country、$status、$caption){
如果($status='Y'){
回声“;
}
否则{
回声“;
}
};
回声“耳鼻咽喉区:
新加坡
中国
印度
印度尼西亚
台湾
";
而($row=$result->fetch_assoc()){
$country=$row['country'];
$status=$row['status'];
$hostname=$row['hostname'];
$ip=$row['ip'];
$caption=“$hostname-$ip”;
如果($country==‘Singapore’){
新加坡_服务器($country,$status,$caption);
}
}
回声“
中国
印度
印度尼西亚
台湾
";
函数中的代码不会在函数位置中回显。它会在你打电话的地方发出回声。实际上我的要求有点不同。让我再解释一遍。我在mariadb中有一个包含3列(主机名、ip、国家)的表。我在5个国家拥有多台服务器。现在我正尝试使用php代码在网页上填充一个表,表格式如下。需要将5个国家/地区名称显示为表格标题,与相应国家/地区相关的所有主机名应显示在与表格数据相同的国家/地区标题下(作为所有主机名的单个单元格)。我们可以动态地这样做吗?请帮帮我。我在这里被阻止了。请不要删除绿色勾号并提出新问题。如果你有新问题,请问一个新问题,我很乐意帮助你。这就是堆栈溢出的工作原理。非常感谢,当然可以。我将以新问题的形式发布,“如何在动态表(嵌套表)中显示网页上的mysql表数据,类似的数据应该在同一单元格中”。帮帮我,我会尽快看的。
function singapore_server($country,$status,$caption){
if($status == 'Y'){
echo "<abbr title='$caption'><button class='button' style='background-color:#43D216;padding: 7px 10px;border-radius: 50px;cursor: pointer;border: none;'></button></abbr> ";
}
else {
echo "<abbr title='$caption'><button class='button' style='background-color:red;padding: 7px 10px;border-radius: 50px;cursor: pointer;border: none;'></button></abbr> ";
}
};
echo "<font color='Orange'><b>ENT Zone:</b></font><br>
<table class='style1' height='100' width='1300' border='1' cellspacing='1'>
<tr align='center' height='10' border='1'>
<td width=500><font color='black'>Singapore</font></td>
<td widht =200><font color='white'>China</font></td>
<td width=200><font color='white'>India</font></td>
<td width='200'><font color='white'>Indonesia</font></td>
<td width='200'><font color='white'>Taiwan</font></td>
</tr>
<tr align='center'>
<td width=500>";
while ($row = $result->fetch_assoc()){
$country=$row['country'];
$status=$row['status'];
$hostname=$row['hostname'];
$ip=$row['ip'];
$caption="$hostname - $ip";
if ($country == 'Singapore'){
singapore_server($country,$status,$caption);
}
}
echo "</td>
<td width =200>
China
</td>
<td width=200>
India
</td>
<td width=200>
Indonesia
</td>
<td width=200>
Taiwan
</td>
</tr>
</table>";