-3

我正在尝试使用 php 格式化我的数据库中的一些 html 输出,这是我的问题:

应该如何形成:

...
<li>
    <div class="row-wrapper">
       <div class="some-class-1">ARRAY-ELEMENT-1</div>
       <div class="some-class-1">ARRAY-ELEMENT-2</div>
       <div class="some-class-1">ARRAY-ELEMENT-3</div>
       <div class="some-class-2">ARRAY-ELEMENT-4</div>
    </div>
    <div class="row-wrapper">
      <div class="some-class-1">ARRAY-ELEMENT-5</div>
      <div class="some-class-1">ARRAY-ELEMENT-6</div>
      <div class="some-class-1">ARRAY-ELEMENT-7</div>
      <div class="some-class-2">ARRAY-ELEMENT-8</div>
    </div>
    <div class="row-wrapper">
      <div class="some-class-1">ARRAY-ELEMENT-9</div>
      <div class="some-class-1">ARRAY-ELEMENT-10</div>
      <div class="some-class-1">ARRAY-ELEMENT-11</div>
      <div class="some-class-2">ARRAY-ELEMENT-12</div>
    </div>
</li>
<li>
  <div class="row-wrapper">
    <div class="some-class-1">ARRAY-ELEMENT-13</div>
    <div class="some-class-1">ARRAY-ELEMENT-14</div>
    <div class="some-class-1">ARRAY-ELEMENT-15</div>
    <div class="some-class-2">ARRAY-ELEMENT-16</div>
  </div>
  <div class="row-wrapper">
    <div class="some-class-1">ARRAY-ELEMENT-17</div>
    <div class="some-class-1">ARRAY-ELEMENT-18</div>
    <div class="some-class-1">ARRAY-ELEMENT-19</div>
    <div class="some-class-2">ARRAY-ELEMENT-20</div>
  </div>
  <div class="row-wrapper">
    <div class="some-class-1">ARRAY-ELEMENT-21</div>
    <div class="some-class-1">ARRAY-ELEMENT-22</div>
    <div class="some-class-1">ARRAY-ELEMENT-23</div>
    <div class="some-class-2">ARRAY-ELEMENT-24</div>
  </div>
</li>
... etc.

数据保存在从 mysql db 收集的数组中,到目前为止我得到了这个:

$num_thumbs = 4; //Number od elemets in a row
$result = myQueryFunction("SELECT * FROM table_name ORDER BY Id ASC"); 
$num_rows = mysql_num_rows($result); 
if (!empty($num_rows)) {
  while ($row = mysql_fetch_array($result)) {
    $thumbs_array[] = "<a href=\"" . $row[0] . "\"><img src=\"" . $row[1] . "\"";
  }
  $thumb_p = "<div>\n";
  mysql_free_result($result);
  $i = 1;
  foreach ($thumbs_array as &$thumb_link) {
    if ($i == $num_thumbs) {
        $i = 1;
        $thumb_p .= "<div class=\"some-class-2\">" . $thumb_link . "</div>";
        $thumb_p .= "</div>\n<div class=\"row-wrapper\">\n";
    } else {
            $thumb_p .= "\t\n<div class=\"some-class-1\">" . $thumb_link . "</div>";
        $i++;
    }

  }
  $thumb_p .= "\n</div>\n";
}

print($thumb_p);

这仅涵盖代码格式的这一部分:

  <div class="row-wrapper">
    <div class="some-class-1">ARRAY-ELEMENT-13</div>
    <div class="some-class-1">ARRAY-ELEMENT-14</div>
    <div class="some-class-1">ARRAY-ELEMENT-15</div>
    <div class="some-class-2">ARRAY-ELEMENT-16</div>
  </div>
  <div class="row-wrapper">
    <div class="some-class-1">ARRAY-ELEMENT-17</div>
    <div class="some-class-1">ARRAY-ELEMENT-18</div>
    <div class="some-class-1">ARRAY-ELEMENT-19</div>
    <div class="some-class-2">ARRAY-ELEMENT-20</div>
  </div>
  <div class="row-wrapper">
    <div class="some-class-1">ARRAY-ELEMENT-21</div>
    <div class="some-class-1">ARRAY-ELEMENT-22</div>
    <div class="some-class-1">ARRAY-ELEMENT-23</div>
    <div class="some-class-2">ARRAY-ELEMENT-24</div>
  </div>

我一直在考虑使用一些变量 $newList = null; 这将在每一行更新,所以我可以检查它是否改变了下一个。有任何想法吗?提前致谢 :)

4

1 回答 1

0

看来您正试图每三个 div 放一个 li 。在这种情况下,您可以这样做:

$thumb_p = "<li>\n<div class=\"row-wrapper\">\n";
mysql_free_result($result);
$i = 1;
$j = 0;
foreach ($thumbs_array as &$thumb_link) {
  if ($i == $num_thumbs) {
      $i = 1;
      $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";

我实际上无法尝试,但我认为没关系。每打印三个行包装器就会添加一个 li 。

于 2012-06-27T21:39:25.800 回答