range()不适用于循环

时间:2013-06-24 00:05:30

标签: php loops range

我的主要问题是,在我的双循环中,似乎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”),为什么? 问候和谢谢!

1 个答案:

答案 0 :(得分:1)

输出第一行后,您总是从内部循环中的函数返回:

return $result;
break;

您需要删除上面的两行。