-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

3 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";

実際に試すことはできませんが、大丈夫だと思います。印刷する 3 行ラッパーごとに 1 つの li が追加されます。

于 2012-06-27T21:39:25.800 に答える