【问题标题】:How to display data from an array in a table in the vertical way如何以垂直方式在表格中显示数组中的数据
【发布时间】:2015-10-30 02:17:46
【问题描述】:

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

Row 1  Row 2 Row 3 Row 4

a       e      i     m

b       f      j     n

c       g      k     o

d       h      l     p

其中表的rowscolums 的数量可能因变量$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[],其中包含学生的卷号列表。

我想在带有vertical displayevery row should display row number in the table head(顶部)的表格中显示这些roll numbers

【问题讨论】:

  • 你可能要手动做...意思就是获取数据的总数,然后看你想显示多少行/列,然后做一个二维数组,然后显示它随意使用第一个键作为列或行,取决于你做什么......
  • 你可以用你的 mysql 查询来做到这一点。 stackoverflow.com/questions/1241178/mysql-rows-to-columns
  • 在您的示例中,您有许多学生产生了一个均匀的网格。如果您添加了另一个学生q,并且您指定了四行,那么哪一行应该有一个额外的学生?
  • @不要惊慌,因为这张桌子是为学生有限座位的教室准备的。但是如果有学生 q 那么我们可以扩展列。我的意思是“第 1 行”可以有学生“abcde”和第 2 行“fghij”等等。

标签: php html mysql arrays html-table


【解决方案1】:

这是一种可能的方式:

$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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    相关资源
    最近更新 更多