【问题标题】:Max of four <td>'s, then start new <tr> (MySQL Query)最多四个 <td>,然后开始新的 <tr>(MySQL 查询)
【发布时间】:2011-11-16 11:51:25
【问题描述】:
echo '<table>';
echo '<tr>';
$i = '';
while($res = mysql_fetch_assoc($mysqlqry)){
    echo '<td><a href="'. $res['link'] .'"><img src="'. $res['link'] .'" style="border: 0px; width: 150px; height: 150px;" alt="afbeelding"></a></td>';
    $i++;
    if($i = '4'){
        echo '</tr><tr>';
    }
}
echo '</tr>';
echo '</table>';

我想设置最多 4 个&lt;td&gt;s,然后设置一个新的&lt;tr&gt;,当我从 mysql 表中获取某些内容时,这不是 w3 有效且不起作用。

当结果少于 8 个时,我怎样才能完成这项工作,并设置其他 &lt;td&gt;s?

【问题讨论】:

    标签: php mysql html-table


    【解决方案1】:
    if($i == 4){
        echo '</tr><tr>';
        $i = 0;
    }
    

    应该可以正常工作,我之前做过类似的代码,你还需要用空格填写最后一行(或使用 colspan)。

    编辑:

    或者看中:

    echo '<table>';
    echo '<tr>';
    $i = 0;
    while($res = mysql_fetch_assoc($mysqlqry)){
        echo '<td><a href="'. $res['link'] .'"><img src="'. $res['link'] .'" style="border: 0px; width: 150px; height: 150px;" alt="afbeelding"></a></td>';
        if($i++ % 4 == 0){
            echo '</tr><tr>';
        }
    }
    echo '</tr>';
    echo '</table>';
    

    【讨论】:

      【解决方案2】:

      你的问题是这样的:

      if($i = '4'){
      

      这是将'4' 分配给$i。您可能想要执行以下操作:

      if($i % 4 == 0){
      

      换句话说,每次$i除以四是一个偶数除法(没有余数),开始一个新的&lt;tr&gt;。

      【讨论】:

        【解决方案3】:
        echo '<table>';
        for ($i=0, $max = 4; $res = mysql_fetch_(...); ) {
            if ($i++ % $max == 0)
                echo '<tr>';
        
            echo '<row stuff>';
        
            if ($i % $max == 0)
                echo '</tr>';
        }
        
        echo '</table>';
        

        如果您愿意,也可以硬编码$max。这也不会生成“幻像”行。

        提示:如果您的值是数字,请不要使用 'string'。

        【讨论】:

          【解决方案4】:

          这就是我多年来一直这样做的方式......

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <title>4 Column Display Table</title>
          </head>
          <body>
          
          <?php 
          //connection
          
          $con = mysqli_connect("Host", "UserName", "Password", "TableName");
          if (!$con)
            {
            die('Database connection is - <strong>unsuccessful</strong> ' . mysqli_error());
            }
          //The Query
           $get_query_sql = "SELECT * FROM Query";
           $get_query_res = mysqli_query($con, $get_query_sql) or die(mysqli_error($con));
          
          //The Results
          while  ($res = mysqli_fetch_assoc($get_query_res)) {
          
          $Column1 = $res['Column1'];
          $Column2 = $res['Column2'];
          
          //Table display
          echo '<table align="center" border="1px" bordercolor="#999999" cellpadding="5px"     cellspacing="0" bgcolor="#CCCCCC" style="vertical-align:middle">';
          echo '<tr>';
           $i = 1;
          
          echo '<td align="middle" height="200px" width="200px"><strong>ID =</strong> '.$Column1.' and the <strong>Number =</strong>'.$Column2.' </td>'; 
          if($i++ % 4 == 0){
              echo '</tr><tr>';
             }
          }
           echo '</tr>';
           echo '</table>';
          ?>
          </body>
          </html>
          

          【讨论】:

            猜你喜欢
            • 2015-12-24
            • 1970-01-01
            • 2017-03-26
            • 1970-01-01
            • 2011-12-09
            • 2015-09-02
            • 1970-01-01
            • 1970-01-01
            • 2019-06-27
            相关资源
            最近更新 更多