2017-08-07 116 views
0

這裏是我的代碼塊雖然循環語句

</tr> 
<tr> 
    <td bgcolor="aabbcc"><center>Line 1</td> 
    <?php 
    //Connect to mysql server 
    $con = mysqli_connect('localhost', 'User', 'Pass', 'Database'); 
    if (!$con) { 
     die ("connection error". mysqli_connect_error()); 
    } 
    $results = mysqli_query($con, " SELECT * FROM PMAsset where  LineNum='1' Order by Linenum, LineOrder ") or die(mysqli_error($con)); 
    while($row = mysqli_fetch_array($results)) { 
     ?> 
     <td Bgcolor=<?php echo $row['PMStatus']?>>Line <?php echo  $row['LineNum']?> PM <?php echo $row['LineOrder']?></td> 

     <?php 
    } 
    ?> 
</tr> 

我所要做的就是創建一個填充有行中我重視的是我所得到目前是PIC 10列中的數據錶行。

Image of generated table

正如你可以看到它不是循環的10倍。我需要做些什麼才能做到這一點。我對構建循環感到困惑。從圖片3號線應該在3號線PM3之下。我明白爲什麼這樣工作。然而,如果沒有數據要去那裏,我該如何把它放在正確的單元格中,並將這些框改爲黑色?任何和所有的幫助將是偉大的。這裏是數據庫表

Image of database table

<center> 
<table border="1" Width="80%"> 
    <tr bgcolor="5DF588"> 
    <th>Line Number</th> 
    <th>PM 1</th> 
    <th>PM 2</th> 
    <th>PM 3</th> 
    <th>PM 4</th> 
    <th>PM 5</th> 
    <th>PM 6</th> 
    <th>PM 7</th> 
    <th>PM 8</th> 
    <th>PM 9</th> 
    <th>PM 10</th> 
    </tr> 
       <tr> 
        <td bgcolor="aabbcc"><center>Line 1</td> 
        <td Bgcolor=Red>Line 1 PM 1</td> 
        <td Bgcolor=green>Line 1 PM 2</td> 
        <td Bgcolor=Yellow>Line 1 PM 3</td> 
        <td Bgcolor=Green>Line 1 PM 4</td> 
</tr> 
       <tr> 
     <td bgcolor="E5F29D"><center>Line 2</td> 
       <td Bgcolor=Red>Line 2 PM 2</td> 
</tr> 
      <tr> 
     <td bgcolor="aabbcc"><center>Line 3</td> 
       <td Bgcolor=Yellow>Line 3 PM 3</td> 
</tr> 
+1

您還可以通過檢查源代碼或查看頁面源,向我們展示生成HTML的方式。 另外我還想補充一點,既然你想循環10次,那麼你可能應該循環使用for循環,這樣如果沒有值的話你可以顯示一個空單元格。 –

+0

tHIS是查看部分 –

回答

0

哦,我看,你爲什麼不創造,您可以通過行號和lineorder並運行給定的行號和lineorder查詢的功能。該函數因此可以從數據庫返回null或值,並且可以通過在for循環中循環來使用它來填充單元格。

功能可以是這樣的:

function getcell($linenumber, $lineorder){ 
    global $con; 
    $r = mysqli_query($con, "SELECT * FROM PMAsset where LineNum='$linenumber' AND `LineOrder` = '$lineorder' LIMIT 1") or die(mysqli_error($con)); 

    if($r->num_rows == 1){ 
    $row = $r->fetch_assoc(); 
    return '<td> ... HTML HERE .. </td>'; 
    } else { 
    return '<td> &nbsp; </td>'; 
    } 
} 

而且可以遍歷每個LINENUM和lineorder使用嵌套爲更好的控制迴路。 希望這有助於。

0

您需要知道的是您輸出了多少個元素,並將其與剩餘的插槽一起填充。

$col = 0; 
while($row = mysqli_fetch_array($results)) { 
    ?> 
    <td Bgcolor=<?php echo $row['PMStatus']?>>Line <?php echo  $row['LineNum']?> PM <?php echo $row['LineOrder']?></td> 

    <?php 
    $col++; 
} 
while ($col++ < 10) { 
    echo '<td />'; 
} 
0

我認爲你可以做另一種方法。 您可以嘗試將所有數據保存在二維數組中,然後循環使用。

在2維數組保存數據:

// your new array 
$data = []; 

$sql = " 
    SELECT * FROM PMAsset 
     ORDER BY Linenum, LineOrder 
;"; 

$result = mysqli_query($con, $sql) or die(mysqli_error($con)); 

while($row = mysqli_fetch_array($result)) { 

    // use the LineNum and LineOrder as Keys 
    $data[ $row[LineNum] ][ $row[LineOrder] ] = $row['PMStatus'];  

} 

您現在在陣列和卡納環完整的數據提供10列和行:

... your table head  

// loop LINENUMBERS -> $ln 
for($ln = 1; $ln < 10; $ln++) { 

    // loop LineOrder -> $lo 
    for($lo = 1; $lo < 10; $lo++) { 

     echo "<tr>"; 

     if(isset($data[$ln][$lo]) { 

      // key exists and is not empty or NULL 
      echo "<td Bgcolor=\"{$data[$ln][$lo]}\"> Line {$ln} PM {$lo} </td>"; 

     } else { 

      // key does not exists 
      echo "<td>no data</td>"; 

     } 

    } 

    echo "</tr>"; 

} 
... 

希望的代碼清楚了。讓我知道。

+0

$ sql =「選擇*從PMAsset ORDER BY Linenum,LineOrder」; \t $ result = mysqli_query($ con,$ sql)或die(mysqli_error($ con)); \t而($行= mysqli_fetch_array($結果)){ > ?} //循環LINENUMBERS - > $ LN 爲($ LN = 1; $ LN <10; $ LN ++){ 回聲「 「; echo「> Line <?php echo $ row ['LineNum']?> PM <?php echo $ row ['LineOrder']?> 「; }其他{ //顏色黑 回聲 」? 「; } } 回聲」「; } –

+0

這是您的工作解決方案嗎?在評論中很難閱讀 – Michael

+0

我不確定我是否正確閱讀它,但是如果缺少一行,可能會遇到麻煩。 – Michael