【问题标题】:display data in multiple columns在多列中显示数据
【发布时间】:2013-05-04 08:55:56
【问题描述】:

您好,我需要从 mySQL 表中构建一个包含四列的表。 这是我现在拥有的:

 <?php

 "<table>";
     while ( $row = mysql_fetch_array( $result , MYSQL_ASSOC) )
     {
         "<tr>";
         "<td>";         
           print("<p><img id='g1' src='/$row[img]' width=130 height=130 onClick='f1()'>" );  
           print( "<p> Name: $row[name] <br>");    
          "</td>";
      "</tr>";
        "</table>";
           }


  ?>

输出将是这样的一个列表:

我需要表格在四列中,如下所示:

如果我该怎么做,这可能吗?

【问题讨论】:

  • 表格用于表格数据,而不是布局。您必须每行放置一本书才能适当地使用表格。

标签: php javascript sql css


【解决方案1】:

只需将&lt;tr&gt;s 移动到while 之外。

"<table><tr>";
while ($row = ...) {
    "<td>";
    ...
    "</td>";
}
"</tr></table>";

我认为这是某种伪代码,因为您实际上并没有将字符串写入任何内容。您也不应该在新代码中使用 mysql_ 函数,而应使用 PDO 或 mysqli。

【讨论】:

    【解决方案2】:
    <?php
    $items_in_row = 4 ;
    $index = 0 ;
    ?>
    
    <table>
      <tr>
    
    <?php
    while ($row = mysql_fetch_array( $result , MYSQL_ASSOC)){ 
      $index++ ; ?>
      <td>       
        <p>
          <img id='g1' src='/<?php echo $row["img"] ;?>' width=130 height=130 onClick='f1()'>" ; 
        </p>
        <p> Name: <?php echo $row['name'] ; ?> </p>
        <br>
      </td>
    <?php if ($index%$items_in_row == 0){ ?>
      </tr>
      <tr>
    <?php }
    } ?>
    </tr>
    </table>
    

    已编辑

    【讨论】:

    • 当我在浏览器中呈现页面时,我收到警告:在第 110 行除以零,第 110 行是
    【解决方案3】:
    "<table>";
    "<tr>";
     $i=1;
     while ( $row = mysql_fetch_array( $result , MYSQL_ASSOC) )
     {         
          "<td>";         
           print("<p><img id='g1' src='/$row[img]' width=130 height=130 onClick='f1()'>" );  
           print( "<p> Name: $row[name] <br>");    
          "</td>";
          if ($i%4 == 0){
              "</tr><tr>"
          }
          $i++;
        "</tr>"
        "</table>";
           }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2021-09-13
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      相关资源
      最近更新 更多