skip to Main Content

I just want to create a table on HTML with a PHP loop. So, I try to do this:

<table id="tdesign">
    <thead>
        <tr>
            <th>No</th>
            <th>Nama</th>
            <th>Kelas</th>
        </tr>
    </thead>
    <tbody>
        <?php $no = 1; ?>
        <?php $kls = 10;?> 
        <?php for ($i=1; $i <= 10 ; $i++) :?>
        <tr>
            <td><?php echo $no++; ?></td>
    
            <td>Name <?php echo $i; ?></td> 
            <?php endfor; ?>
            
            <?php for ($j=10; $j >= 1 ; $j--) : ?>
            <td><?php echo "Class ". $j . "n" ;?></td>
            <?php endfor; ?>
        </tr>
        

    </tbody>
            
</table>    

But, why the output becomes this?

enter image description here

3

Answers


  1. It’s because you’ve got a loop inside a loop.

    Try this instead:

    <table id="tdesign">
        <thead>
            <tr>
                <th>No</th>
                <th>Nama</th>
                <th>Kelas</th>
            </tr>
        </thead>
        <tbody>
            <?php $no = 1; ?>
            <?php $kls = 10;?> 
            <?php for ($i=1; $i <= 10 ; $i++) :?>
            <tr>
                <td><?php echo $no++; ?></td>
        
                <td>Name <?php echo $i; ?></td> 
                
                <td><?php echo "Class ". 10-$i . "n" ;?></td>
            </tr>
            <?php endfor; ?>
        </tbody>
    </table>    
    
    Login or Signup to reply.
  2. Assuming that you really need two nested loops.

    You need to move the endfor to the end of the loop, otherwise there will be <tr> before the 2nd loop

    So

    <table id="tdesign">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Nama</th>
                    <th>Kelas</th>
                </tr>
            </thead>
            <tbody>
                <?php $no = 1; ?>
                <?php $kls = 10;?> 
    
                <?php for ($i=1; $i <= 10 ; $i++) :?>
                  <tr>
                    <td><?php echo $no++; ?></td>        
                    <td>Name <?php echo $i; ?></td> 
                  
                    
                    <?php for ($j=10; $j >= 1 ; $j--) : ?>
                       <td><?php echo "Class ". $j . "n" ;?></td>
                    <?php endfor; ?>
    
               <?php endfor; ?>
    
                </tr>
            </tbody>
        </table> 
    
    Login or Signup to reply.
  3. Assuming you have an array like this:

    [0] => Array
        (
            [stud_id] => 1234
            [name] => John Doe
            [class] => Class 1
        )
    
    [1] => Array
        (
            [stud_id] => 2345
            [name] => Jane Doe
            [class] => Class 2
        )
    

    My table loop will be look like this:

    <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>Student ID</th>
                        <th>Student Name</th>
                        <th>Class</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach($array as $data) {
                    ?>
                    <tr>
                        <td><?=$data['stud_id']?></td>
                        <td><?=$data['name']?></td>
                        <td><?=$data['class']?></td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
    

    You must put the tbody contents (tr, td) inside the loop

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search