我的主要问题是 range() 在我的双循环中似乎无法正常工作......这是我处理的代码:
function my_function()
{
$states = array('schwarz', 'rot', 'blau');
$transactionIDs = range(1,10);
foreach($transactionIDs as $transactionID) {
foreach ($states as $state) {
$result = "<img src=\"inventory_images/8.jpg\" onclick=\"changecolor(this)\" name=\"number.".$transactionID."\" />";
$testPath = "transactions/Ordner".$transactionID."/".$state.".png";
if (file_exists($testPath)) {
$result = $testPath;
}
return $result;
break;
}
}
}
$bilderProZeile = array(1=>4,
2=>6);
$angabe = 3;
$eingesetzteBilder = array();
$zeilen = array(1, 2, 3);
$html="";
foreach ($zeilen as $zeile) {
if (!isset($bilderProZeile[$zeile])) {
$bilderProZeile[$zeile] = $angabe;
}
$html.= "<tr>\n";
for ($i = 1; $i <= $bilderProZeile[$zeile]; $i++) {
$html.=sprintf("<td>%s</td>\n", my_function());
}
$html.="</tr>\n";
}
echo $html;
我得到的输出:
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
</tr>
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
</tr>
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="number.1" /></td>
</tr>
一切都很好,但似乎 range() 不起作用。我定义了范围 1-10 但我总是得到 1 (name="number.1") ,为什么?问候和感谢!