0

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).

RyanS
  • 627
  • 1
  • 10
  • 26
virk
  • 3
  • 2
  • You may have to do it manually... which means get the total number of data, then see how many rows / columns you wish to display, and then make a 2 dimension array, and then display it at will using first key as column or as rows, depends what you do... – Random Oct 29 '15 at 16:51
  • you could do this with your mysql query. http://stackoverflow.com/questions/1241178/mysql-rows-to-columns – devlin carnate Oct 29 '15 at 16:54
  • In your example, you have a number of students that produces an even grid. If you added another student `q`, and you had specified four rows, which row should have an extra student? – Don't Panic Oct 29 '15 at 17:00
  • @ dont panic as this table is for a class room for limited seats for students. but if there is student q then we can expand the colums. i mean 'row 1' can have students 'abcde' and row 2 'fghij' and so on. – virk Oct 29 '15 at 17:27

1 Answers1

0

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>
Tomaso Albinoni
  • 1,003
  • 1
  • 8
  • 19