【问题标题】:How give index number to table cell using row number and column number如何使用行号和列号为表格单元格提供索引号
【发布时间】:2013-07-07 11:07:30
【问题描述】:

我有 10*10 的固定表,可以打印两个 for 循环。 一些图像显示在一些给定的table cell 中,与管理员给定的行 ID 和列 ID 匹配。

$table .= '<table border="1" cellspacing="0" cellpadding="2" class="banner_table">';
    for($x=1; $x<=10; $x++) {
       $table .= "<tr>";
       for($y=1; $y<=10; $y++) {
           $table .= "<td class='thumbnail'>";

                if (isset($temp[$x][$y])) {
                    $count++;  
                    $table .= "<a target='_blank' href='" . ($temp[$x][$y]['img_url'] ? $temp[$x][$y]['img_url'] : "#") . "'>
                    <img id='imgid' src='admin/small-image/" . $temp[$x][$y]['img_title'] . "' width='20' height='20' /></a>";                   
                }  
           else $table .="&nbsp;";
           $table .= "</td>"; 
        }
        $table .= "</tr>";
   }
   $table .= '</table>';

如果没有给出row idcolumn id,我应该如何为表格单元格提供索引号,例如 1,2,2...?我想同样给出索引号 1(ROW 1 和 COL1),索引号 2(ROW1 和 COL2)。

是否有任何方法可以从行 ID 和列 ID 获取表格单元格编号?

【问题讨论】:

    标签: php html arrays for-loop html-table


    【解决方案1】:
    // Index number to row and column:
    $index_no = 13; // Number from 1 to 100
    $y = floor(($index_no-1)/10)+1;
    $x = $index_no-($y-1)*10;
    
    // Row and column to index number:
    $x = 4;
    $y = 3;
    $index_no = ($y-1)*10+$x; // Number from 1 to 100
    

    【讨论】:

    • 为了在第一种情况下得到$x,我认为先得到$y然后减去$y*10到$index_no会更快。 IE。 $y = floor(($index_no-1)/10)+1; $x = $index_no - $y*10;
    • 你说得对,我进行了速度测试,结果似乎快了 9%。 (至少在我的服务器上使用 PHP 5.3.3)我会编辑我的答案。
    • 似乎在第一种情况下得到 $x 不正确。如果 index_no 为 13,则 $y=1 且 $x= -37
    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 2015-03-14
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多