【问题标题】:Query result records to column for comparison查询结果记录到列进行比较
【发布时间】:2012-11-14 07:21:26
【问题描述】:

有没有办法实现将查询结果显示到列中并将记录转到下一行?

这是我的 SQL 表:

------------SQL TABLE -----------

    id   |    product_name    | product_type
    0    |    Lorem           | Table   
    1    |    Ipsum           | Chair
    2    |    Dolor           | Lamp
    3    |    Sit             | Chair

这就是我想要的。

        Lorem    |  Ipsum      |    Dolor   |   Sit       |
------------------------------------------------------------
id    | 0        |  1          |    2       |   3         |
type  | Table    |    Chair    |    Lamp    |    Chair    |

期望的输出:

    <table>
  <tr>
    <td>&nbsp;</td>
    <td>Lorem</td>
    <td>Ipsum</td>
    <td>Dolor</td>
    <td>Sit</td>
  </tr>

  <tr>
    <td>id</td>
    <td>0</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>

    <tr>
    <td>type</td>
    <td>Table</td>
    <td>Chair</td>
    <td>Lamp</td>
    <td>Chair</td>
  </tr>

</table>

这是我的代码,但我知道这是非常错误的。如果使用 while 和 for 循环有意义,我只是坚持如何为字段创建新行。

    echo '<table><tr>';

        $i=1;

        while($rows = mysql_fetch_array($result)){
            echo '<td><table><tr><th>'.$rows['product_name'].
            '</th></tr><tr><td>'.$rows['product_id'].
            '</td><td>'.$rows['product_type'].'</tr></table></td>';
                if($i %2 == 0) { 
        echo '</tr>
                <tr>'; }
                }    
        echo'
        </tr>
        </table>';

【问题讨论】:

    标签: php mysql while-loop html-table field


    【解决方案1】:

    试试这个

          echo '<table>';
    
    
        while($rows = mysql_fetch_array($result)){
            echo '<tr><th>'.$rows['product_name'].
            '</th></tr><tr><td>'.$rows['id'].
            '</td></tr><tr><td>'.$rows['product_type'].
            '</td></tr>';
    
                }    
        echo' </table>';
    

    【讨论】:

    • 您好,谢谢您的回答。在来这里之前,我实际上尝试了这个解决方案。结果只显示在一个列中,而不是在一个新的产品名称处开始一个新列。
    • 是的,但是您在示例中这样说。如您的示例中的一行中的产品名称
    • 它在同一列中显示这样的结果:product_name \nid \nproduct_type \nproduct_name \nid \nproduct_type \n|我需要它显示在几列中。就像一个产品比较表。
    • 谢谢,它仍然在做同样的事情。在这一行中,它只给出一行,而不是为下一条记录创建一个新列echo '&lt;table&gt;&lt;tr&gt;';它在while loop之外
    • 对不起,我想你不明白我的意思。
    【解决方案2】:

    您需要轮换数据。 你可以使用这样的东西(未经测试):

    // build query
    $query = "SELECT id, product_name, product_type FROM whatevertableyouhave";
    
    // query the database
    $result = mysql_query($query);
    
    // cols we are interested in (from the SQL query)
    $cols = array(
      'id',
      'product_name',
      'product_type';
    );
    
    // initialize rotated result using cols
    $rotated = array();
    foreach($cols as $col) {
      $rotated[$col] = array();
    }
    
    // fill rotated array
    while(($row = mysql_fetch_assoc($result)) !== false) {
      foreach($cols as $col) {
        $rotated[$col][] = $row[$col];
      }
    }
    
    // echo html
    echo "<table>";
    echo "<tr>";
    foreach($cols as $col) {
      echo "<th>{$col}</th>";
    }
    echo "</tr>";
    foreach($rotated as $col => $values) {
      echo "<tr>";
      foreach($values as $value) {
        echo "<td>" . htmlentities($value) . "</td>";
      }
      echo "</tr>";
    }
    echo "</table>";
    

    【讨论】:

    • 感谢您的回答。这更接近我的需要。不过有一件事,我需要产品名称作为列标题。我一直在搞乱代码,但你如何将value =&gt; product_name 设为$col
    • 好的,我想通了。我刚刚取出foreach($cols as $col) { echo "&lt;th&gt;{$col}&lt;/th&gt;"; 并在while 循环中添加了这段代码$name = $row['product_name']; echo '&lt;th&gt;'.$name.'&lt;/th&gt;'; 再次感谢。
    • 不客气。额外提示:如果您刚开始使用 PHP / MySQL,请查看 PDOmysqli 函数,它们都是简单的 mysql_* 函数的更好替代品。尤其是看看参数化查询,这是防止 SQL 注入漏洞的好方法。
    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多