php 位置ID更改时为空表行

5us2dqdw  于 2023-03-07  发布在  PHP
关注(0)|答案(2)|浏览(159)

我有一个包含数百条记录的数据表,每条记录都有一个位置ID。我尝试做的是在位置ID更改时用换行符回显所有记录。
SQL语句为:

SELECT LocationID, deviceName, companyname, record_ID,location, location,orderby FROM FIDS_Hardware WHERE deviceTypeCode = 5 AND monitor = 1 ORDER BY hotelID ASC, orderby ASC

我用来生成表的代码看起来可能很奇怪,但这正是我想要它工作的方式。

$Limit2 = 5;
$Count2 = 0;
echo "<table border='0' width='100%' cellspacing='2'>"; 
                  
while ($row_ConfMon = mysqli_fetch_assoc($ConfMon)) { 
    $locationid = $row_ConfMon['hotelID'];
    $RoomNo2 = $row_ConfMon['deviceName'];
    $companyname = $row_ConfMon['companyname'];
    $recordID = $row_ConfMon['record_ID'];
    $location = $row_ConfMon['location'];
    $DownDate = date("d-m-y", $row_ConfMon['uptime']);
    $DownTime = date("H:i", $row_ConfMon['uptime']);

    if($row_ConfMon['NoDevice'] == 1) {
        if($Count2 < $Limit2) {
                            
            if($Count2 == 0) {
                echo "<tr>";
            }
            echo "<a href=\"confmon.php\" target=\"_self\"><td class=\"roomviewpurple\" valign=\"top\"><div class=\"coltitle\"><a href=\"confmon_hist.php?recordID=$recordID\" target=\"_self\">$companyname</a></div> <div class=\"confmontime2\">$location<br />$RoomNo2<br /></div><span class=\"confmontime\">$DownDate<br /><div class=\"confmontime2\">$DownTime</div><br /></span></td></a>";
         } else {
            $Count2 = 0;
            echo "</tr><tr><a href=\"confmon.php\" target=\"_self\"><td class=\"roomviewpurple\" valign=\"top\"><div class=\"coltitle\"><a href=\"confmon_hist.php?recordID=$recordID\" target=\"_self\">$companyname</a></div><div class=\"confmontime2\">$location<br />$RoomNo2<br /></div><span class=\"confmontime\">$DownDate<br /><div class=\"confmontime2\">$DownTime</div><br /></span></td></a>";
        }
        $Count2++;
    } elseif($row_ConfMon['MobileDisplay'] == 1 && $row_ConfMon['messageCount'] == 2) {
        if($Count2 < $Limit2) {
            if($Count2 == 0) {
                echo "<tr>";
            }
            echo "<a href=\"confmon.php\" target=\"_self\"><td class=\"roomviewamber\" valign=\"top\"><div class=\"coltitle\"><a href=\"confmon_hist.php?recordID=$recordID\" target=\"_self\">$companyname</a></div> <div class=\"confmontime2\">$location<br />$RoomNo2<br /></div><span class=\"confmontime\">$DownDate<br /><div class=\"confmontime2\">$DownTime</div><br /></span></td></a>";
        } else {
            $Count2 = 0;
            echo "</tr><tr><a href=\"confmon.php\" target=\"_self\"><td class=\"roomviewamber\" valign=\"top\"><div class=\"coltitle\"><a href=\"confmon_hist.php?recordID=$recordID\" target=\"_self\">$companyname</a></div><div class=\"confmontime2\">$location<br />$RoomNo2<br /></div><span class=\"confmontime\">$DownDate<br /><div class=\"confmontime2\">$DownTime</div><br /></span></td></a>";
        }
        $Count2++;
    } elseif($row_ConfMon['messageCount'] == 2) {
        if($Count2 < $Limit2) {
            if($Count2 == 0) {
                echo "<tr>";
            }
            echo "<a href=\"confmon.php\" target=\"_self\"><td class=\"roomviewred\" valign=\"top\"><div class=\"coltitle\"><a href=\"confmon_hist.php?recordID=$recordID\" target=\"_self\">$companyname</a></div> <div class=\"confmontime2\">$location<br />$RoomNo2<br /></div><span class=\"confmontime\">$DownDate<br /><div class=\"confmontime2\">$DownTime</div><br /></span></td></a>";
        } else {
            $Count2 = 0;
            echo "</tr><tr><a href=\"confmon.php\" target=\"_self\"><td class=\"roomviewred\" valign=\"top\"><div class=\"coltitle\"><a href=\"confmon_hist.php?recordID=$recordID\" target=\"_self\">$companyname</a></div><div class=\"confmontime2\">$location<br />$RoomNo2<br /></div><span class=\"confmontime\">$DownDate<br /><div class=\"confmontime2\">$DownTime</div><br /></span></td></a>";
        }
        $Count2++;
    } else {
        if($Count2 < $Limit2) {
            if($Count2 == 0) {
                echo "<tr>";
            }
            echo "<td class=\"roomviewgreen\" valign=\"top\"><div class=\"coltitle\"><a href=\"confmon_hist.php?recordID=$recordID\" target=\"_self\">$companyname</a></div><div class=\"confmontime2\">$location<br />$RoomNo2<br /></div><span class=\"confmontime\">$DownDate<br /><div class=\"confmontime2\">$DownTime</div><br /></span></td>";
        } else {
            $Count2 = 0;
            echo "</tr><tr><td class=\"roomviewgreen\" valign=\"top\"><div class=\"coltitle\"><a href=\"confmon_hist.php?recordID=$recordID\" target=\"_self\">$companyname</a></div><div class=\"confmontime2\">$location<br />$RoomNo2<br /></div><span class=\"confmontime\">$DownDate<br /><div class=\"confmontime2\">$DownTime</div><br /></span></td></a>";
        }
        $Count2++;  
    }
}
echo "</tr></table>";

我现在将所有记录回显到一个表中,但我希望在位置ID更改时在表中开始一个新行,如。

这是当前的输出。

这就是我想要的样子。

这可能吗?

euoag5mw

euoag5mw1#

它应该只是一个循环遍历数据的例子,并跟踪前一行中使用了什么Location ID,这样您就可以将它与当前行进行比较,然后决定是否添加额外的行。
另外,在您的原始代码中也有很多重复的代码,所以我冒昧地删除了重复的代码--更改了创建新行的逻辑,使其只执行一次,并且还使用变量来表示不同echo语句中的更改(这只是一个类名,并添加了一个额外的表行),因此您只需要声明HTML的大部分内容一次,最后重构它,去掉所有的转义字符,这些字符也会使它变得混乱。
哦,你的HTML表格在技术上是畸形的,因为你没有把任何<td>单元格放入行中。
因此,让我们分几个阶段来讨论这个问题,第一个版本相当于产生现在的输出:

$colLimit = 5;
$colCount = 1;
$tableHTML = "<table border='0' width='100%' cellspacing='2'>".PHP_EOL;
                  
while ($row_ConfMon = mysqli_fetch_assoc($ConfMon))
{ 
    $locationid = $row_ConfMon['hotelID'];
    $RoomNo2 = $row_ConfMon['deviceName'];
    $companyname = $row_ConfMon['companyname'];
    $recordID = $row_ConfMon['record_ID'];
    $location = $row_ConfMon['location'];
    $DownDate = date("d-m-y", $row_ConfMon['uptime']);
    $DownTime = date("H:i", $row_ConfMon['uptime']);
    
    $rowHTML = "";
    $className = "";

    if ($colCount == 1)
    {
        $rowHTML .= "<tr>".PHP_EOL;
    }
    
    if($row_ConfMon['NoDevice'] == 1) $className = "roomviewpurple";
    elseif($row_ConfMon['MobileDisplay'] == 1 && $row_ConfMon['messageCount'] == 2) $className = "roomviewamber";
    elseif($row_ConfMon['messageCount'] == 2) $className = "roomviewred";
    else $className = "roomviewgreen";
    
    $linkHTML = '<td><a href="confmon.php" target="_self"><td class="'.$className.'" valign="top"><div class="coltitle"><a href="confmon_hist.php?recordID='.$recordID.'" target="_self">'.$companyname.'</a></div> <div class="confmontime2">'.$location.'<br />'.$RoomNo2.'<br /></div><span class="confmontime">'.$DownDate.'<br /><div class="confmontime2">'.$DownTime.'</div><br /></span></td></a></td>'.PHP_EOL;
    $rowHTML .= $linkHTML;

    if ($colCount == $colLimit)
    {
        $rowHTML .= "</tr>".PHP_EOL;
        $colCount = 1;
    }
    else $colCount++;
    
    $tableHTML .= $rowHTML;
}

//tidy up end of table
if (substr($tableHTML, -4) != "</tr>") $tableHTML .= "</tr>".PHP_EOL;
$tableHTML .= "</table>";

//output the table
echo $tableHTML;

实时演示:https://3v4l.org/HQqL3
现在,下一个函数将在location字段发生更改时开始一个新行。正如您所看到的,我添加了一个变量来跟踪上一行的location ID,以便我们可以比较它,并且还更改了它决定是否结束该行并重置列计数的点-它将延迟到我们处理下一行的开始。因此,有机会比较位置ID。根据您下面的评论,当更改位置ID时,它还添加了另一行作为间距:

$colLimit = 5;
$colCount = 1;
$tableHTML = "<table border='0' width='100%' cellspacing='2'>".PHP_EOL;
$prevLocationID = ""; //to keep track of the location on the preceding data item
                  
while ($row_ConfMon = mysqli_fetch_assoc($ConfMon))
{ 
    $locationid = $row_ConfMon['hotelID'];
    $RoomNo2 = $row_ConfMon['deviceName'];
    $companyname = $row_ConfMon['companyname'];
    $recordID = $row_ConfMon['record_ID'];
    $location = $row_ConfMon['location'];
    $DownDate = date("d-m-y", $row_ConfMon['uptime']);
    $DownTime = date("H:i", $row_ConfMon['uptime']);
    
    $rowHTML = "";
    $className = "";

    if ($colCount == $colLimit || $prevLocationID != $locationid)
    {
        $rowHTML .= "</tr>".PHP_EOL;
        if ($prevLocationID != $locationid) $rowHTML .= "<tr></tr>.PHP_EOL";
        $colCount = 1;
    }
    else $colCount++;

    if ($colCount == 1)
    {
        $rowHTML .= "<tr>".PHP_EOL;
    }
    
    if($row_ConfMon['NoDevice'] == 1) $className = "roomviewpurple";
    elseif($row_ConfMon['MobileDisplay'] == 1 && $row_ConfMon['messageCount'] == 2) $className = "roomviewamber";
    elseif($row_ConfMon['messageCount'] == 2) $className = "roomviewred";
    else $className = "roomviewgreen";
    
    $linkHTML = '<td><a href="confmon.php" target="_self"><td class="'.$className.'" valign="top"><div class="coltitle"><a href="confmon_hist.php?recordID='.$recordID.'" target="_self">'.$companyname.'</a></div> <div class="confmontime2">'.$location.'<br />'.$RoomNo2.'<br /></div><span class="confmontime">'.$DownDate.'<br /><div class="confmontime2">'.$DownTime.'</div><br /></span></td></a></td>'.PHP_EOL;
    $rowHTML .= $linkHTML;

    $prevLocationID = $locationid; //set "previous" location ID as the current one, ready for processing the next data item
    $tableHTML .= $rowHTML;

}

//tidy up end of table
if (substr($tableHTML, -4) != "</tr>") $tableHTML .= "</tr>".PHP_EOL;
$tableHTML .= "</table>";

//output the table
echo $tableHTML;

实时演示:https://3v4l.org/KRiU1

unftdfkk

unftdfkk2#

您可能希望在PHP代码中插入空格--例如,通过foreach循环--而不是在SQL中插入空格。可以用SQL执行此操作吗?可以,但是对于与表示相关的内容,我建议只循环遍历行并在PHP端添加空的<tr>标记。这样做几乎肯定会干净得多。并且它在应用程序的正确层上保持正确的行为。

相关问题