PHP代码更正

时间:2012-06-28 00:25:57

标签: php html arrays foreach formatting

我有这段代码:

$num_thumbs = 3;
$thumb_p = "<li>\n<div class=\"row-wrapper\">\n";
$i = 0;
$j = 0;
foreach ($thumbs_array as &$thumb_link) {
if ($i == $num_thumbs) {
  $i = 0;
  $thumb_p .= "<div class=\"some-class-2\">" . $thumb_link . "</div>";
  $thumb_p .= "</div>\n";
  if($j == 3)
  {
      $thumb_p .= "</li>\n<li>";
      $j = 0;
  }
  $thumb_p .= "<div class=\"row-wrapper\">\n";
  $j++;
} else {
  $thumb_p .= "\t\n<div class=\"some-class-1\">" . $thumb_link . "</div>";
  $i++;
}
}
$thumb_p .= "\n</div>\n</li>\n";

它应该在每三次出现后<div class=\"row-wrapper\">之间包裹<li></li>

问题是我每次都会在底部多加一行:

<li>
<div class="row-wrapper">
</div>
</li>

1 个答案:

答案 0 :(得分:0)

考虑这一行

$thumb_p .= "</li>\n<li>";

这不会给你带来麻烦吗?我认为应该是

 $thumb_p .= "<li>\n</li>";