循环语句时

时间:2017-08-07 18:12:21

标签: php sql database

这是我的代码块

</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>

我要做的是创建一个表格行,填充数据,行中有10列我附加了当前的图片。

Image of generated table

你可以看到它没有循环10次。我需要做些什么来实现这一目标。我对构建循环感到困惑。从第3行到第3行应该在第3行。我理解为什么它会这样运作。但是,如果没有数据可以将它放在正确的单元格中并将框更改为黑色?任何和所有的帮助都会很棒。这是数据库表

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>

3 个答案:

答案 0 :(得分: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>';
  } 
}

您可以使用嵌套for循环遍历每个linenum和lineorder以更好地控制。 希望这可以帮助。

答案 1 :(得分: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 />';
}

答案 2 :(得分:0)

我认为你可以采取另一种方法。 您可以尝试将所有数据保存在二维数组中,然后循环遍历它。

将数据保存在二维数组中:

// 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>";

}
...

希望代码清楚。让我知道。

相关问题