foreach with range does not work

2019-06-14 18:28发布

问题:

here is my code:

   function my_function()
{
$states = array('schwarz', 'rot', 'blau');
foreach(range(0, 5) as $number) {
  foreach ($states as $state) {
    $result = "<img src=\"inventory_images/8.jpg\" onclick=\"changecolor(this)\" name=\"nummer.$number\" />";
    $testPath = "transactions/Ordner$number/$state.png";
    if (file_exists($testPath)) {
      $result = $testPath;
    }
  }
  break;
}
return $result;
}
$imagesPerLine = array(1=>1, 2=>2); $default = 3;
$lines = array(1, 2, 3);
$html="";
foreach ($lines as $line) {
if (!isset($imagesPerLine[$line])) {
  $imagesPerLine[$line] = $default;
}
$html.= "<tr>\n";
for ($i = 1; $i <= $imagesPerLine[$line]; $i++) {
  $html.=sprintf("<td>%s</td>\n", my_function());
}
$html.="</tr>\n";

}
echo $html;

and this is the output:

<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
</tr>
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
</tr>
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
</tr>

now i dont know how to configure my foreach part to get the name-value = n+1.the output should be:

<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
</tr>
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.2" /></td>
</tr>
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.3" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.4" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.5" /></td>
</tr>

firstly i thought, "break;" or "return $result;" is placed wrong, but i dont think so now. could anybody help me to change my script maybe?? thanks and greetings!!

回答1:

Your logic is flawed. Because you return the string from your function, and print it outside, you need to change how you are doing it. Currently, you're doing this:

for ($i = 1; $i <= $imagesPerLine[$line]; $i++) {
  $html.=sprintf("<td>%s</td>\n", my_function());
}

Sounds to me like you need to make your function wrap its content with <td>...</td>, and also concatenate. So inside your function you should do:

$result .= "<td>etc etc $number etc etc</td>\n";

It would also be good practice to put $result = ""; at the top of that function.

Now, when you return $result, you should get all the stuff you put together inside those loops, and you add that to $html by changing your calling loop:

for ($i = 1; $i <= $imagesPerLine[$line]; $i++) {
  $html .= my_function();
}