2013-07-09 31 views
0

我有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>'; 

我應該如何給索引號,如1,2,2 ......爲表單元格沒有給出row idcolumn id?我想給索引號1(行1和COL1),索引號2(行1和COL2)同樣。

是否有任何方法從行ID和列ID獲取表格單元號?

回答

2
// 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 
+1

爲了在第一種情況下獲得$ x,我認爲先獲得$ y然後減去$ y * 10到$ index_no會更快。即 '$ y = floor(($ index_no-1)/ 10)+1; $ x = $ index_no - $ y * 10;' – Sikian

+0

你是對的,我跑了一次速度測試,看起來好像快了9%。 (至少在我的服務器上使用PHP 5.3.3) 我會編輯我的答案。 –

+0

似乎在第一種情況下得到$ x不正確。如果index_no是13,$ y = 1和$ x = -37 – Miuranga