tenten
tenten

Reputation: 1274

foreach creates duplicate tables in HTML

I have a query that gives this array,

Array ( 
    [0] => Array ( 
        [id] => 1 
        [name] => name1 
    )

    [1] => Array ( 
        [id] => 2 
        [name] => name2 
    ) 

    [2] => Array ( 
        [id] => 3 
        [name] => name3 
    )
 )

When this is returned to a HTML page and made a foreach, It duplicates the table into 3 tables .

<?php foreach ($types AS $row_types) { ?>
<table class="table_style1">
    <tr>
        <th>Title</th>
        <th>Title2</th>
    </tr>
    <tr>
        <td><?php $row_types['name'][0]?></td>
        <td>data 1</td>
    </tr>
    <tr>
        <td><?php $row_types['name'][1]?></td>
        <td>data 2</td>
    </tr>
    <tr>
        <td><?php $row_types['name'][2]?></td>
        <td>data 3</td>
    </tr>
</table>
<?php } ?>

please help me to show only 1 table here.

Upvotes: 2

Views: 540

Answers (3)

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

Error: You have used the table to repeat inside the forach() loop and hence it is the error in your script.

Needed Corrections: <table> and <tr> should start outside and end outside the loop to avoid the duplicates that you face now. </tr> and </table>

Hence i will provide you with the Example Below:

Example:

<table>
<?php
foreach()
{
}
?>
</table>

Solution for: foreach creates duplicate tables in HTML

Your entire code will look like as follows as i have told above the table and tr will start before the foreach loop.

Code:

<table class="table_style1">
<tr>
  <th>ID</th>
  <th>NAME</th>
</tr>
<?php foreach ($types AS $row_types) { ?>        
    <tr>
        <td><?php echo $row_types['id']?></td>
        <td><?php echo $row_types['name']?></td>
    </tr>        
<?php } ?>
</table>

Upvotes: 0

Styphon
Styphon

Reputation: 10447

The problem is you're not understanding how the foreach works. It takes your array and runs through the code inside the foreach once per entry in your array.

With your array structure the foreach will go through each element of your array $types one at a time. As the elements in $types are arrays themselves (i.e. you have a multi-dimensional array) $row_types will be an array containing the keys id and name. As we only want one table with one row per array we need to initiate the table and it's title outside of the foreach loop.

<table class="table_style1">
    <tr>
        <th>Title</th>
        <th>Title2</th>
    </tr>

Then we can have our foreach output each row for us

<?php foreach ( $types as $row_types ) { ?>
    <tr>
        <td><?= $row_types['id'] ?></td>
        <td><?= $row_types['name'] ?></td>
    </tr>
<?php } ?>

If you notice I'm using <?= tags, this is a shortcut for <?php echo because at the moment you're not echoing out what is in row, you've just put the variable and done nothing with it.

Finally we can close the table

</table>

The whole code will look like this

<table class="table_style1">
    <tr>
        <th>Title</th>
        <th>Title2</th>
    </tr>
    <?php foreach ( $types as $row_types ) { ?>
    <tr>
        <td><?= $row_types['id'] ?></td>
        <td><?= $row_types['name'] ?></td>
    </tr>
    <?php } ?>
</table>

Upvotes: 4

Murad Hasan
Murad Hasan

Reputation: 9583

Put the table tag and the heading tr th outside from the loop.

$types = array ( 
            array ( 
                "id" => 1,
                "name" => "name1"
            ),
            array ( 
                "id" => 2,
                "name" => "name2"
            ),
            array ( 
                "id" => 3,
                "name" => "name3"
            )
        );
?>
<table class="table_style1">
    <tr>
        <th>Title</th>
        <th>Title2</th>
    </tr>
<?php 
foreach ($types AS $row_types) { ?>
    <tr>
        <td><?php echo$row_types['name'];?></td>
        <td>data <?php echo $row_types['id']?></td>
    </tr>
<?php }?>
</table>

Upvotes: 2

Related Questions