0

データベーステーブルがあり、そのテーブルには6行あります。私が欲しいのは、3列と2行のテーブルを使用してhtmlページにその6行を表示することです。

私はphp配列とwhileループを操作する方法を知っています。私の問題は、配列を制限して最初の行に3つのアイテムを配置し、次の行に他の3つのアイテムを配置する方法です。

これは私が試したがうまくいかなかったものです

<div id="maincontent">
  <!-- class one -->
      <?php 
        $getSection = getSection();
        $i=0;
        while($allSection = mysql_fetch_array($getSection)){
      ?>
      <div class="subconent">
<table width="937" border="0">
  <tr>
    <td>
        <div class="sub_image">
            <a href="section.php?id=<?php echo urlencode($allSection['id']); ?>"><img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink"  onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" /></a>
        </div>
            <div class="cont_txt">
              <h3><?php echo $allSection['name_full']; ?></h3>
                <p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
                <br />
              <a href="section.php?id=<?php echo urlencode($allSection['id']); $i++; ?>"><img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" /></a>
            </div>
    </td>
  </tr>
</table>
</div>
<?php 
if($i==4) { ?>
<table width="937" border="0">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td></tr>
<tr><div class="sub_image">
            <a href="section.php?id=<?php echo urlencode($allSection['id']); ?>"><img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink"  onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" /></a>
        </div>
            <div class="cont_txt">
              <h3><?php echo $allSection['name_full']; ?></h3>
                <p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
                <br />
              <a href="section.php?id=<?php echo urlencode($allSection['id']); ?>"><img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" /></a>
            </div><td>
<?php   }
} ?>
  </div>
4

5 に答える 5

7

モジュロ演算子(%)を使用します。

http://www.devchunks.com/web-development/using-the-php-modulus-operator/

このようなもの:

<table>

<?php

$i = 0;
while ( $row = mysql_fetch_array($result) ){
    if ($i % 3 == 0){
        echo '<tr>';
    }
    echo '<td>'.$row['column_name'].'</td>';
    if ($i % 3 == 2){
        echo '</tr>';
    }
    $i++; 
}

//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0){
    echo '</tr>';
}

?>

</table>
于 2012-08-27T17:20:16.303 に答える
1

基本的に、次のようなものが必要になります。

<table>
<tr>

<?

$count = 0;
foreach ($row) {
    echo "<td>" . $row["value"] ."</td>";
    $count++;

    if (($count % 3) == 0) && ($count > 0) {
        echo ("</tr><tr>");
    }
}

?>

</tr>
</table>

テーブルのヘッダーの印刷を開始してから、データセットの反復処理を開始します。印刷した数を追跡します。これが3番目の場合は、HTMLを印刷してこの行を終了し、次の行を開始します。(私はを使用%したので、最初のエントリだけでなく、3つおきのエントリでラップされます)

于 2012-08-27T17:22:37.490 に答える
0

そうですね、sql-queryでこれらの情報を正しくフェッチできます(http://en.wikibooks.org/wiki/MySQL/Pivot_tableに適合する1つの例にすぎません)。

または、すべてをPHP配列にフェッチするだけです。

Oldschool:mysql_query()およびwhile($ row = mysql_fetch_array())Newchool:PDO(http://de.php.net/manual/en/book.pdo.php

于 2012-08-27T17:18:34.510 に答える
0

素晴らしい!どうもありがとう。それは私のために働く、ゼンド。あなたはこのようなことを試すことができます。

   <table width="1024px" border="0" cellspacing="2" cellpadding="2">  
  <?php
      $i = 0;
      foreach ($this->rows as $row )
      {
          $img = IMAGE_PATH . '/' . 'gallery/' . $row->gly_thumbnail;
          if ($i % 3 == 0)
          {
              echo '<tr>';
          }
  ?>
  <td align="center">
    <img src="<?php echo $img; ?>" width="300" height="215"><br/>
    <?php echo $row->gly_title; ?>
  </td>
  <?php        
          if ($i % 3 == 2)
          {
              echo '</tr>';
          }
          $i++; 
      }

      //here is a check in case you don't have multiple of 3 rows
      if ($i % 3 != 0)
      {
          echo '</tr>';
      }

  ?>
</table>
于 2012-09-06T08:00:46.463 に答える
0
                    <?php if ($i++%$_columnCount==0): ?>
                        <tr>
                    <?php endif ?>
                  
                   
                    <td> <img src="<?php echo site_url('uploads/shelter_images/'.$row->shelter_id."/".$img->imagefile) ?>" alt="" width="300" ></td>
                    
                     <?php if ($i%$_columnCount==0 || $i==$totalImg): ?>
                      </tr>
                   <?php endif; ?> 
            
            <?php  } ?>
于 2021-06-24T07:13:55.503 に答える