如何根据行号是否匹配来填充 HTML 表编号的行? [英] How can I populate HTML table numbered rows based on whether they match row number?

查看:53
本文介绍了如何根据行号是否匹配来填充 HTML 表编号的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我问了这个问题 本周早些时候,@newfurniturey 帮助了我,但现在我遇到了一个新问题:我希望能够将设备放置在超过一个 U(因此,设备数据库表中的 usize 列) - 有些设备可以跨越半个机柜.此外,我希望能够将设备标记为位于机柜的前部或后部,但这应该很简单,我可以弄清楚.

So, I asked this question earlier this week, and @newfurniturey helped me out, but now I have a new problem: I'd like to be able to put devices in that span more than one U (hence, the usize column in the devices db table) - some devices can span take up half a cabinet. Also, I'd like to be able to mark devices as being in the front or rear of the cabinet, but that should be simple enough for me to figure out.

这是仅适用于 1U 设备的工作代码(请参阅数据库设置的旧问题):

Here's the working code (see old question for db setup) for just 1U devices:

<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!--
    function clickHandler(e)
    {
        var targetId, srcElement, targetElement;
        if (window.event) e = window.event; 
        srcElement = e.srcElement? e.srcElement: e.target;
        if (srcElement.className == "Outline")
        {
                targetId = srcElement.id + "d";
                targetElement = document.getElementById(targetId);

            if (targetElement.style.display == "none")
                {
                        targetElement.style.display = "";
                        srcElement.src = "images/minus.gif";
                    } 
            else
                {
                    targetElement.style.display = "none";
                    srcElement.src = "images/plus.gif";
                }
        }
    }
    document.onclick = clickHandler;
-->
</SCRIPT>
<noscript>You need Javascript enabled for this page to work correctly</noscript>
<?
function sql_conn()
{
    $username="root";
    $password="root";
    $database="racks";
    $server="localhost";

    @mysql_connect($server,$username,$password) or die("<h2 align=\"center\" class=\"red\">[<img src=\"images/critical.gif\" border=\"0\">] Unable to connect to $server [<img src=\"images/critical.gif\" border=\"0\">]</h2>");
    @mysql_select_db($database) or die("<h2 align=\"center\" class=\"red\">[<img src=\"images/critical.gif\" border=\"0\">] Unable to select $database as a database [<img src=\"images/critical.gif\" border=\"0\">]</h2>");
}

sql_conn();
$sql_datacenters="SELECT * FROM `datacenters`";
$result_datacenters=mysql_query($sql_datacenters);
$j=0;
echo "<table border='1' style='float:left;'>";
while ($datacenters_sqlrow=mysql_fetch_array($result_datacenters))
{
    echo "<tr><td>";
    echo "<h2 class='black' align='left'>";
    echo "<IMG SRC='images/plus.gif' ID='Out" . $j . "' CLASS='Outline' STYLE='cursor:hand;cursor:pointer'>"; // fancy icon for expanding-collapsing section
    echo " " . $datacenters_sqlrow['rack'] . ": " . $datacenters_sqlrow['cagenum'] . "</h2>"; // datacenter name and cage number
    echo "<div id=\"Out" . $j . "d\" style=\"display:none\">"; // opening of div box for section that is to be expanded-collapsed
    echo $datacenters_sqlrow['notes'] . "<br /><br />"; // datacenter notes
    $sql_cabinets="SELECT * FROM `cabinets` WHERE `datacenter` = '$datacenters_sqlrow[0]' ORDER BY `cabinetnumber` ASC";
    $result_cabinets=mysql_query($sql_cabinets);
    while ($cabinets_sqlrow=mysql_fetch_array($result_cabinets))
    {
        $sql_devices="SELECT * FROM `devices` WHERE `datacenter` = '$datacenters_sqlrow[0]' AND `cabinet` = '$cabinets_sqlrow[1]' ORDER BY `ustartlocation` ASC";
        $result_devices=mysql_query($sql_devices);
        echo "<table border='1' style='float:left;'>"; // opening of table for all cabinets in datacenter
        echo "<tr><td colspan='2' align='middle'>" . $cabinets_sqlrow[1] . "</td></tr>"; // cabinet number, spans U column and device name column
        $devices = array();
        while($row = mysql_fetch_array($result_devices)) {
            $devices[$row['ustartlocation']] = $row['devicename'];
        }
        for ($i = 0; $i < $cabinets_sqlrow[2]; $i++) // iterates through number of U in cabinet     
        {   
            $u = $cabinets_sqlrow[2] - $i; // subtracts current $i value from number of U in cabinet since cabinets start their numbers from the bottom up
            echo "<tr>";
            echo "<td width='15px' align='right'>$u</td>"; // U number
            echo (isset($devices[$u]) ? "<td width='150px' align='middle'>$devices[$u]</td>" : "<td width='150px' align='middle'>empty</td>");
            echo "</tr>";
        }
        echo "</table>"; // closes table opened earlier
    }
    echo "</td></tr>";
    echo "</div>"; // close for div box that needs expanding-collapsing by fancy java
    $j++; // iteration for the fancy java expand-collapse
}
echo "</table>";
mysql_close();
?>

推荐答案

根据您之前的问题,每个 ustartlocation 都是唯一的(因此您可以将其用作 $devices 数组).使用相同的概念,您可以将 $devices 数组从ustartlocation 填充到 (ustartlocation + (usize - 1))".>

Based on your previous question, each ustartlocation is unique (hence why you can use it as an index in your $devices array). Using this same concept, you could populate the $devices array from "ustartlocation to (ustartlocation + (usize - 1))".

$devices = array();
while($row = mysql_fetch_array($result_devices)) {
    $endLocation = ($row['ustartlocation'] + ($row['usize'] - 1));
    for ($location = $row['ustartlocation']; $location <= $endLocation; $location++) {
        $devices[$location] = $row['devicename'];
    }
}

因为您的显示循环已经遍历每个 U 并显示分配的设备,所以您不需要修改任何其他部分.但是,需要注意的是,设备名称将为每个 U 重复重复而不是 span .为了跨越,我们需要做更多的工作.

Because your display-loop already iterates through each U and displays the device assigned, you shouldn't need to modify any other portion. However, the caveat to this is that the device-name will repeat for every U instead of span it. To span it, we'll need to do a little more work.

首先,我们可以将 usize 存储在 $devices 数组中,而不是填充每个单独的位置.此外,为了防止以后进行大量额外的工作/计算,我们还将为每个额外的位置存储一个占位符"设备.

To start, we could just store the usize in the $devices array instead of filling in each individual position. Also, to prevent a lot of extra work/calculations later, we'll also store a "placeholder" device for each additional position.

while($row = mysql_fetch_array($result_devices)) {
    // get the "top" location for the current device
    $topLocation = ($row['ustartlocation'] + $row['usize'] - 1);

    // populate the real position
    $devices[$topLocation] = $row;

    // generate a list of "placeholder" positions
    for ($location = ($topLocation - 1); $location >= $row['ustartlocation']; $location--) {
        $devices[$location] = 'placeholder';
    }
}

接下来,在您的显示循环中,您将检查当前位置是否是占位符(如果是,则只显示 U 并对设备不做任何事情;如果不是,显示设备,或空").为了实现每个设备的跨度"效果,我们将单元格的 rowspan 设置为等于设备的 usize.如果是1,则为单个单元格;2,它将跨越 2 行,等等(这就是为什么对占位符行上的设备什么都不做"会起作用的原因):

Next, in your display-loop, you will check if the current position is a placeholder or not (if so, just display the U and do nothing for the device; if it isn't, display the device, or 'empty'). To achieve the "span" effect for each device, we'll set the cell's rowspan equal to the device's usize. If it's 1, it will be a single cell; 2, it will span 2 rows, etc (this is why "doing nothing" for the device on the placeholder-rows will work):

for ($i = 0; $i < $cabinets_sqlrow[2]; $i++) {   
    $u = $cabinets_sqlrow[2] - $i;
    echo "<tr>";
    echo '<td width="15px" align="right">' . $u . '</td>';
    if (isset($devices[$u])) {
        // we have a "device" here; if it's a "placeholder", do nothing!
        if ($devices[$u] != 'placeholder') {
            echo '<td width="150px" align="middle" rowspan="' . $devices[$u]['usize'] . '">' . $devices[$u]['devicename'] . '</td>';
        }
    } else {
        echo '<td width="150px" align="middle">empty</td>';
    }
    echo "</tr>";
}

因此,可以看出 - 上面的 first 方法简单地为它跨越的每个 U 重复设备.然而,第二种方法将呈现更用户友好的显示.想要使用哪种方法以及您认为将来更易于维护的方法取决于您的偏好.

So, as it can be seen - the first method above that simply repeats the device for each U it spans is much simpler. However, the second method will present a more user-friendly display. It's your preference to which method you want to use and which one you think will be more maintainable in the future.

更新(代码修复和多向生成)
我没有意识到您的表格是按降序构建的,所以我将 ustartlocation 作为顶部位置",这导致了错误的行/单元格移位.我已经修复了上面的代码,以根据 ustartlocationusize 为每个将解决该问题的设备正确设置顶部位置".

UPDATE (code-fix & multi-direction spanning)
I didn't realize that your table was being built in descending-order so I had the ustartlocation as the "top location" which caused an erroneous row/cell shift. I've fixed the code above to properly set a "top location" based on the ustartlocation and usize for each device that will fix that issue.

或者,由于方向可能重要也可能不重要,我自定义了 $devices 填充循环(如下)以支持创建一个行跨度 > 向上或向下,完全取决于您指定的标志.您需要更改的唯一代码(如果您已经有了上面的自定义显示循环)将是填充 $deviceswhile 循环:

Alternatively, as direction may or may not be important, I've customized the $devices-populating loop (below) to support creating a row-span that goes either upwards or downwards, completely depending on the flag you specify. The only code you'll need to change (if you already have the customized display-loop from above) would be the while loop that populates $devices:

$spanDevicesUpwards = true;
while($row = mysql_fetch_array($result_devices)) {
    if ($row['usize'] == 1) {
        $devices[$row['ustartlocation']] = $row;
    } else {
        $topLocation = ($spanDevicesUpwards ? ($row['ustartlocation'] + $row['usize'] - 1) : $row['ustartlocation']);
        $bottomLocation = ($spanDevicesUpwards ? $row['ustartlocation'] : ($row['ustartlocation'] - $row['usize'] + 1));

        $devices[$topLocation] = $row;
        for ($location = ($topLocation - 1); $location >= $bottomLocation; $location--) {
            $devices[$location] = 'placeholder';
        }
    }
}

如果usize 跨度超过1,这个新代码块将确定当前设备的顶部单元格"和底部单元格".如果你向上跨越,顶部单元格是ustartlocation + usize - 1;如果您向下跨越,它只是ustartlocation.底部位置也是通过这种方式确定的.

This new block of code will, if the usize spans more than 1, determine the "top cell" and "bottom cell" for the current device. If you're spanning upwards, the top-cell is ustartlocation + usize - 1; if you're spanning downwards, it's simply ustartlocation. The bottom-location is also determined in this manner.

这篇关于如何根据行号是否匹配来填充 HTML 表编号的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆