Home > database >  How to Create Table on Html with PHP data Looping
How to Create Table on Html with PHP data Looping

Time:12-02

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

CodePudding user response:

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>    

CodePudding user response:

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> 

CodePudding user response:

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

  • Related