0
嘿我買了我的腳本豆蔻問題...... 這是第一部分:如何結合兩個foreache's
$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>
但不是「[圖]」我希望它顯示:
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's達到我的目標? 問候!