Php 如何使用行号和列号为表单元格指定索引号

Php 如何使用行号和列号为表单元格指定索引号,php,html,arrays,for-loop,html-table,Php,Html,Arrays,For Loop,Html Table,我有一个10*10的固定表,可以打印两个for循环。 一些图像显示在一些给定的表格单元格中,这些单元格与管理员提供的给定行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*10的固定表,可以打印两个for循环。 一些图像显示在一些给定的
表格单元格中,这些单元格与管理员提供的给定行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>';
$table.='';

对于($x=1;$xIn)为了在第一种情况下得到$x,我认为先得到$y,然后减去$y*10到$index\u no会更快。也就是说,
$y=floor(($index\u no-1)/10)+1;$x=$index\u no-$y*10;
你说得对,我做了一个速度测试,看起来快了9%。(至少在使用PHP5.3.3的服务器上)我将编辑我的答案。在第一种情况下,似乎得到$x不正确。如果索引为13,$y=1,$x=-37
// 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