簡體   English   中英

使用MySQL數據填充HTML表(多個查詢)

[英]Populate HTML Table with MySQL data (Multiple Queries)

經過廣泛的研究,我得出的結論是我的案子是獨一無二的,我需要問一下。 我對PHP的了解非常有限,我正試圖完全根據Google的結果來完成這項工作。

目標:創建一個顯示包含5列(端口,L1狀態,L2狀態,成幀錯誤,活動呼叫)的HTML表的網頁,每列的日期存儲在單個數據庫表中,這是竅門,大部分這些數據來自同一字段...這意味着我需要創建5個不同的查詢。 我嘗試創建一個查詢(如果可以的話,我相信它會起作用),但我無法這樣做。

到目前為止的結果:僅包含第5個查詢的結果的表,而該表的其余部分僅重復包含第1個查詢的表。

這是我的代碼:

<!-- Simple HTML to Create the table layout -->
<table border=1 style="background-color:#F0F8FF;" >
<caption><EM>HEADER</EM></caption>
<tr>
<th>Port</th>
<th>L1 Status</th>
<th>L2 Status</th>
<th>Framing Errors</th>
<th>Active calls</th>
</tr>
<!-- END Simple HTML to Create the table layout -->
<?php
$server = "localhost";
$dbname = "database";
$user = "user";
$password = "password";
$con = mysql_connect($server,$user,$password) or die (mysql_error());
mysql_select_db($dbname) or die (mysql_error());

$query1="select right(name, 10) as 'Port' from items where hostid = (select hostid from hosts where name = 'MIAGATE01') and key_ like '%activeChannels[%' and key_ not like '%SNMPINDEX%' order by name";
$query2="select lastvalue as 'Layer1' from items where hostid = (select hostid from hosts where name = 'MIAGATE01') and key_ like '%statusLayer1[%' and key_ not like '%SNMPINDEX%' order by name";
$query3="select lastvalue as 'Layer2' from items where hostid = (select hostid from hosts where name = 'MIAGATE01') and key_ like '%statusLayer1[%' and key_ not like '%SNMPINDEX%' order by name";
$query4="select lastvalue as 'Framing_Errors'from items where hostid = (select hostid from hosts where name = 'MIAGATE01') and key_ like '%frameErrors[%' and key_ not like '%SNMPINDEX%' order by name";
$query5="select lastvalue as 'Active_Calls' from items where hostid = (select hostid from hosts where name = 'MIAGATE01') and key_ like '%activeChannels[%' and key_ not like '%SNMPINDEX%' order by name";
$result1=mysql_query($query1) or die(mysql_error());
$result2=mysql_query($query2) or die(mysql_error());
$result3=mysql_query($query3) or die(mysql_error());
$result4=mysql_query($query4) or die(mysql_error());
$result5=mysql_query($query5) or die(mysql_error());

while($row1 = mysql_fetch_array($result1)){
while($row2 = mysql_fetch_array($result2)){
while($row3 = mysql_fetch_array($result3)){
while($row4 = mysql_fetch_array($result4)){
while($row5 = mysql_fetch_array($result5)){
echo "<tr>";
echo "<td>" . $row1['Port'] . "</td>";
echo "<td>" . $row2['Layer1'] . "</td>";
echo "<td>" . $row3['Layer2'] . "</td>";
echo "<td>" . $row4['Framing_Errors'] . "</td>";
echo "<td>" . $row5['Active_Calls'] . "</td>";
echo "</tr>";
}
}
}
}
}
mysql_close($con);
?>

首先嘗試此查詢,看看它是否一次獲得了所有必需的結果。

SELECT h.hostid, 
  RIGHT(port.name,10) AS 'Port', 
  l1.lastvalue AS 'Layer1', 
  l2.lastvalue AS 'Layer2', 
  fe.lastvalue AS 'Framing_Errors', 
  ac.lastvalue AS 'Active_Calls' 
FROM hosts h 
INNER JOIN items port 
  ON port.hostid = h.hostid 
    AND port.key_ LIKE '%activeChannels[%' 
    AND port.key_ not LIKE '%SNMPINDEX%' 
INNER JOIN items l1 
  ON l1.hostid = h.hostid 
    AND l1.key_ LIKE '%statusLayer1[%' 
    AND l1.key_ not LIKE '%SNMPINDEX%'
INNER JOIN items l2 
  ON l2.hostid = h.hostid 
    AND l2.key_ LIKE '%statusLayer2[%' 
    AND l2.key_ not LIKE '%SNMPINDEX%'
INNER JOIN items fe 
  ON fe.hostid = h.hostid 
    AND fe.key_ LIKE '%frameErrors[%' 
    AND fe.key_ not LIKE '%SNMPINDEX%'
INNER JOIN items ac 
  ON ac.hostid = h.hostid 
    AND ac.key_ LIKE '%activeChannels[%' 
    AND ac.key_ not LIKE '%SNMPINDEX%'
WHERE h.name = 'MIAGATE01'
ORDER BY h.name;

如果可行,則只需要一個while循環即可填充表格。

while ( $row = mysql_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['Port'] . "</td>";
  echo "<td>" . $row['Layer1'] . "</td>";
  echo "<td>" . $row['Layer2'] . "</td>";
  echo "<td>" . $row['Framing_Errors'] . "</td>";
  echo "<td>" . $row['Active_Calls'] . "</td>";
  echo "</tr>";
}

首先:

您的預期HTML表的每個ROW是否都具有所有五個數據項? 問另一種方式,每個端口是否只有一行? 從您的問題中看不出來。 從您的邏輯中肯定可以使不同參數的出現次數不同。

無論如何...您需要將一堆包含hostid,parameter的虛擬表連接在一起,以將您的顯示組合在一起。 我想是這樣。 (您的數據非常復雜)。

select hostid.hostid, port.Port, layer1.Layer1, layer2.Layer2
  from
            (select hostid, name
             from hosts
            where name = 'MIAGATE01'
            ) hostid
  left join
            (select hostid, right(name,10) as 'Port'
               from items
              where key_ like '%activeChannels[%' and key_ not like '%SNMPINDEX%'  
            ) port 
        on hostid.hostid = port.hostid
  left join
           (select hostid, lastvalue as 'Layer1'
              from items
             where key_ like '%statusLayer1[%' and key_ not like '%SNMPINDEX%'  
           ) layer1 
        on hostid.hostid = layer1.hostid
  left join
           (select hostid, lastvalue as 'Layer2'
              from items
             where key_ like '%statusLayer2[%' and key_ not like '%SNMPINDEX%'  
           ) layer2 
        on hostid.hostid = layer2.hostid
order by hostid.name, port.Port

看看情況如何? 您有很多選擇,它們會生成一個hostid,值項列表,其中值項與您的key_選擇條件相匹配。 然后,將它們全部合並到其hostid值上。 第一個選擇只是為您想要的特定主機提供正確的主機ID。

我沒有做所有的參數,但是完成您的查詢應該不難。 至於調試它....很難知道沒有看到您的items在其所有的榮耀表。 這可能是一個很大的毛發。 但是你已經知道了。

網絡設備不是只充滿樂趣的大桶嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM