如何以垂直方式显示表中数组的数据

时间:2015-10-29 16:44:30

标签: php html mysql arrays html-table

我想根据卷号创建一个学生座位安排表 以这种格式(垂直)

Row 1  Row 2 Row 3 Row 4

a       e      i     m

b       f      j     n

c       g      k     o

d       h      l     p

根据变量rows

,表格中colums$rows and $cols的数量可能会有所不同
    $sql= "SELECT rollno FROM student WHERE batch= '".mysqli_real_escape_string($con, $_POST['batch'])."'";
    $result = mysqli_query($con, $sql);

    if(!$result)
    {

        echo 'Something went wrong. Please try again';

    }
    else
    {
        while($resulrow = mysqli_fetch_assoc($result))
        {
            $array[]=$resultrow['rollno'];
        }   

现在我有$array[],其中包含学生名单的列表。

我想在roll numbersvertical display(顶部)的表格中显示这些every row should display row number in the table head

1 个答案:

答案 0 :(得分:0)

这是一种可行的方法:

$sql = "SELECT col_a, col_b, ... FROM student WHERE batch=?";
$stmt = mysqli_prepare($con, $sql);
mysqli_stmt_bind_param($stmt, 's', $_POST['batch']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

$a = array();
$i = 1;
while ($row = mysqli_fetch_assoc($result)) {
    $a['col_a'][$i] = $row['col_a'];
    $a['col_b'][$i] = $row['col_b'];
    // .... other columns
    ++$i;
}
$rows = count($a['col_a']);

显示表格:

<table><thead><tr>
<?php for ($j = 1; $j <= $rows; ++$j) {
    ?><th>Row <?php echo $j; ?></th><?php
} ?>
</tr></thead><tbody>
<?php foreach ($a as $col) {
    ?><tr><?php
    foreach ($col as $row) {
        ?><td><?php echo $row; ?></td><?php
    }
    ?></tr><?php
} ?>
</tbody></table>