drqvr26084 2015-10-29 16:44 采纳率: 100%
浏览 56
已采纳

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

I want to create a table for seating arrangements of students according to roll number in this format(vertical)

Row 1  Row 2 Row 3 Row 4

a       e      i     m

b       f      j     n

c       g      k     o

d       h      l     p

where number of rows and colums of the table can be different depending on the variables $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'];
        }   

Now I have $array[], which is having list of roll numbers of students.

I want to display these roll numbers in a table with vertical display and every row should display row number in the table head (top).

  • 写回答

1条回答 默认 最新

  • duan19911992 2015-10-29 18:17
    关注

    This is a possible way of doing it:

    $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']);
    

    Display the table:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿