使用循环显示默认表中的数据数组

I have 1 array data may also be more and want to present it on the table, I tried to make the default table by 5 column, but when I try the same result 5 column all its data. should have 3 column 2 column is empty, what is lacking in my script?

This myscript

<?php
$no = 1;
for($x=1; $x<=5; $x++) {
    foreach ($mydata as $row) {
        echo '<tr>';
        echo '<td>'.$no.'</td>';
        echo '<td>'.$row->id.'</td>';
        echo '<td>'.$row->name.'</td>';
        echo '<td>'.$row->class.'</td>';
        echo '</tr>';
        $no++;
    }
}
?>

the results of the script

NO  | ID  | NAME | ClASS |
____|____ |______|_______|
 1  | 001 | Paul |    x  |
 2  | 001 | Paul |    x  |
 3  | 001 | Paul |    x  |
 4  | 001 | Paul |    x  |
 5  | 001 | Paul |    x  |

I expected

NO  | ID  | NAME | ClASS |
____|____ |______|_______|
 1  | 001 | Paul |   x   |
 2  |     |      |       |
 3  |     |      |       |
 4  |     |      |       |
 5  |     |      |       |

Try this:

<?php
$no = 1;
for ($x=1; $x <= 5; $x++) {
    if (isset($mydata[$x-1])) {
        $row = $mydata[$x-1];
        echo '<tr>';
        echo '<td>'.$no.'</td>';
        echo '<td>'.$row->id.'</td>';
        echo '<td>'.$row->name.'</td>';
        echo '<td>'.$row->class.'</td>';
        echo '</tr>';
    } else {
        echo '<tr>';
        echo '<td>'.$no.'</td>';
        echo '<td></td>';
        echo '<td></td>';
        echo '<td></td>';
        echo '</tr>';
    }
    $no++;
}
?>

You need to check if the data is set based off $x var and then render empty row or populated.

You don't need nested loops. Just a single loop from 1 to 5, and each iteration shows the corresponding element of the array, or empty cells if there's no such element.

for ($x = 1; $x <= 5; $x++) {
    echo "<tr>";
    echo "<td>" . $x . "<td>";
    if (isset($mydata[$x-1])) {
        $row = $mydata[$x-1];
        echo '<td>'.$row->id.'</td>';
        echo '<td>'.$row->name.'</td>';
        echo '<td>'.$row->class.'</td>';
    } else { // show empty fields
        echo "<td></td><td></td><td></td>"; 
    }
    echo "</tr>";
}

There's also no need for separate variables $x and $no.