如何使用 php 和 header 在两列中显示数据

how to display data in two column with php with header

我有数据库

---------------------------------------------
no | name   | code |  grade 
---------------------------------------------
1  | john   | A1   | C
2  | john   | A2   | D
3  | john   | A3   | B
4  | tom    | A1   | A
5  | john   | A4   | A
6  | alice  | A1   | C 
7  | alice  | A2   | D
8  | john   | A5   | D
9  | john   | A6   | C
---------------------------------------------

当我想要显示名为 john 的数据时,我想要包含 2 列的结果:

---------------------------------------------------------------
no | name   | code   |  grade | no | name   | code   |  grade |
---------------------------------------------------------------
1  | john   | A1     | C      | 5  | john   | A5     | D      |
2  | john   | A2     | D      | 6  | john   | A6     | C      |      
3  | john   | A3     | B      |    |        |        |        |
4  | john   | A4     | A      |    |        |        |        |
---------------------------------------------------------------

这是我已经尝试过的代码

$result = mysql_query("select * from grade ");

echo "<table border='1'><tr>";
echo "<td>no</td>";
echo "<td>name</td>";
echo "<td>code</td>";
echo "<td>grade</td></tr><tr>";

$no = 1;
$count = 1;

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    extract($row);

    echo "<td>$no</td>";
    echo "<td>$row[name]</td>";
    echo "<td>$row[code]</td>";
    echo "<td>$row[grade]</td>";

    if ($count++ % 2 == 0)
    {
        echo "</tr><tr>";
        $no++;
    }
}

echo "</tr></table>";

但是结果不是我想要的,这是我运行我的代码

时的结果
no | name   | code   |  grade |    |        |        |        |
---------------------------------------------------------------
1  | john   | A1     | C      | 1  | john   | A4     | A      |
2  | john   | A2     | D      | 2  | john   | A5     | D      |      
3  | john   | A3     | B      | 3  | john   | A6     | C      |
---------------------------------------------------------------

有人可以帮助我吗...谢谢

我没有对此进行测试,也没有检查错误,但我认为它符合您要完成的目标。

$result = mysql_query("select * from grade ");
$orig_count = mysql_num_rows($result);

if ( $orig_count % 2 != 0 )
     $count = $orig_count + 1;

$cols = $count / 2; 

//draw header
echo "<table border='1'><tr>";
echo "<td>no</td>";
echo "<td>name</td>";
echo "<td>code</td>";
echo "<td>grade</td></tr><tr>"

for ( $i = 0; $i < $cols; i++ )
{
     if ( $orig_count == 0 )
          break; //zero results

     //first column    
     echo "<td>$i</td>";
     echo "<td>".mysql_result( $result, $i, "name" )."</td>";
     echo "<td>".mysql_result( $result, $i, "code" )."</td>";
     echo "<td>".mysql_result( $result, $i, "grade" )."</td>"; 

     //second column
     if ( $i > $orig_count )
     {
          echo "<td>&nbsp;</td>";
          echo "<td>&nbsp;</td>";
          echo "<td>&nbsp;</td>";
          echo "<td>&nbsp;</td>";   
     }
     else
     {
          echo "<td>".$i+($count/2)."</td>";
          echo "<td>".mysql_result( $result, $i+($count/2), "name" )."</td>";
          echo "<td>".mysql_result( $result, $i+($count/2), "code" )."</td>";
          echo "<td>".mysql_result( $result, $i+($count/2), "grade" )."</td>";   
     }
}

//finish off
echo "</tr></table>";

这是表示问题,所以可以通过两种方式解决。
1. 您可以使用 modern HTML:

创建此列
<section style="-webkit-column-count:2; -webkit-column-gap:15;">
    <div class='columns'>
        <table>
            <tr>
                <th>no</th>
                <th>name</th>
                <th>code</th>
                <th>grade</th>
            </tr>
<?php
$result = mysql_query("select * from grade ");
$no = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo '<tr>';
    echo "<td>{$row['no']}</td>";
    echo "<td>{$row['name']}</td>";
    echo "<td>{$row['code']}</td>";
    echo "<td>{$row['grade']}</td>";
    echo '</tr>';
    $no++;
}
?>
        </table>
    </div>
</section>

2.如果你想坚持使用普通表格,你必须做一些计算: 首先,您需要知道要显示的全部成绩。您可以使用 SQL 查询(更有效的方式)来获取它,我将通过将所有数据预加载到数组来实现。 HTML 为简洁起见,省略了页脚和页眉。

<?php
$data = array();
$result = mysql_query('SELECT * FROM grade;');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $data[] = $row;
}

$rows = (int) ceil(count($data)/2);

for ($i=0; $i < $rows; $i++) {
    $no = $i + 1;
    echo '<tr>';
    echo "<td>{$no}</td>";
    echo "<td>{$data[$i]['name']}</td>";
    echo "<td>{$data[$i]['code']}</td>";
    echo "<td>{$data[$i]['grade']}</td>";
    if (array_key_exists($i + $rows, $data)) {
        $no = $i + $rows + 1;
        echo "<td>{$no}</td>";
        echo "<td>{$data[$i + $rows]['name']}</td>";
        echo "<td>{$data[$i + $rows]['code']}</td>";
        echo "<td>{$data[$i + $rows]['grade']}</td>";
    } else {
        echo "<td>&nbsp;</td>";
        echo "<td>&nbsp;</td>";
        echo "<td>&nbsp;</td>";
        echo "<td>&nbsp;</td>";
    }
    echo "</tr>\n";
}