嘿,我的脚本有点问题……这是第一部分:
$imagesPerLine = array(1=>2, 2=>3); $default = 4;
$lines = array(1, 2, 3);
foreach ($lines as $line) {
if (!isset($imagesPerLine[$line])) {
$imagesPerLine[$line] = $default;
}
echo "<tr>\n";
for ($i = 1; $i <= $imagesPerLine[$line]; $i++) {
echo "<td>[img]</td>\n";
}
echo "</tr>\n";
}
结果我得到以下信息:
<tr>
<td>[img]</td>
<td>[img]</td>
</tr>
<tr>
<td>[img]</td>
<td>[img]</td>
<td>[img]</td>
</tr>
<tr>
<td>[img]</td>
<td>[img]</td>
<td>[img]</td>
<td>[img]</td>
</tr>
但我希望它显示的不是“[img]”:
function getTransactionStatusImagePath($transactionId)
{
$states = array('schwarz', 'rot', 'blau');
$path = '';
foreach ($states as $state) {
$testPath = sprintf('transactions/Ordner%s/%s.png', $transactionId, $state);
if (file_exists($testPath)) {
$path = $testPath;
break;
}
}
return $path;
}
$transactionIds = range(1,9);
foreach ($transactionIds as $transactionId) :
if (($transactionId % $imagesPerLine) == 1)
echo "<tr>\n";
$path = getTransactionStatusImagePath($transactionId);
$imageHtml = ('' !== $path)
? '<img src="' . $path . '" />'
: '<img name="nummer.' . $transactionId . '" src="inventory_images/8.jpg" onclick="changecolor(this)" />' ;
echo "<td>$imageHtml</td>\n";
if (! ($transactionId % $imagesPerLine))
echo "</tr>\n";
endforeach;
所以应该是这样的:
<tr>
<td><img name="nummer.1" src="inventory_images/8.jpg" onclick="changecolor(this)" /></td>
<td><img name="nummer.1" src="inventory_images/8.jpg" onclick="changecolor(this)" /></td>
</tr>
<tr>
<td><img name="nummer.1" src="inventory_images/8.jpg" onclick="changecolor(this)" /></td>
<td><img name="nummer.1" src="inventory_images/8.jpg" onclick="changecolor(this)" /></td>
<td><img name="nummer.1" src="inventory_images/8.jpg" onclick="changecolor(this)" /></td>
</tr>
<tr>
<td><img name="nummer.1" src="inventory_images/8.jpg" onclick="changecolor(this)" /></td>
<td><img name="nummer.1" src="inventory_images/8.jpg" onclick="changecolor(this)" /></td>
<td><img name="nummer.1" src="inventory_images/8.jpg" onclick="changecolor(this)" /></td>
<td><img name="nummer.1" src="inventory_images/8.jpg" onclick="changecolor(this)" /></td>
</tr>
我应该如何结合这两个 foreache 来达到我的目标?问候!