user6818018
user6818018

Reputation:

Create a table within a foreach loop and then print the same html element (input) for every item of the array with php (using the loop only once)

This is my very sloppy code:

<?php 

$data = $_POST['fullName'];
$first = $names[0];

include('header.php');

?>


<form action="other-page.php" method="post" enctype="multipart/form-data">
    <div class="users">
        <table>
            <tr>
                <?php 
                foreach ($data as $key => $singledata) {
                    echo "<th>$singledata</th>";
                }
                ?>
            </tr>
            <tr>
                <?php 
                foreach ($data as $key => $singledata) :
                    ?>
                    <td><input type="checkbox" name="myvalue<?= $key ?>" value="myvalue<?= $key ?>" checked></td>
                    <?php
                endforeach;
                ?>
            </tr>
            <tr>
                <?php 
                foreach ($data as $key => $singledata) :
                    ?>
                    <td><input type="checkbox" name="myother value<?= $key ?>" value="myother value <?= $key ?>"></td>
                    <?php
                endforeach;
                ?>
            </tr>
        </table>

    </div>

</form>

<?php include('footer.php') ?>

It works for sure, but it is silly to repeat every time the same loop. The problem is that I tried many solutions but none of them make me satisfy entirely, for example:

$data = [];
foreach ($names as $key => $name) {

    $data[] = $name;
}

This will create an array to reuse and loop through, but it will be useless to use later on without looping every time my array (my first approach).

I could create a variable and call it $table, and then attach the remaining HTML:

$table = '<tr>';
    foreach ($data as $key => $singledata) {
    $table .= "<td>$key<td>";   
    }
$table .= '</tr>';

But as soon as I will add the tr inside the loop, it will create an additional markup to all of my elements of the array.

So not really useful too.

I just want to print the same input for several usernames and I'm pretty sure that I'm missing something.

Upvotes: 1

Views: 2287

Answers (1)

Nick
Nick

Reputation: 147166

You could generate the contents of each row in an external loop (as you show in your last piece of code) and then simply echo them into the table:

<?php
$headers = '';
$myvalue_row = '';
$myothervalue_row = '';
foreach ($data as $key => $singledata) {
    $headers .= "<th>$singledata</th>";
    $myvalue_row .= "<td><input type=\"checkbox\" name=\"myvalue$key\" value=\"myvalue$key\" checked></td>";
    $myothervalue_row .= "<td><input type=\"checkbox\" name=\"myothervalue$key\" value=\"myothervalue$key\" checked></td>";
}
?>
<form action="other-page.php" method="post" enctype="multipart/form-data">
    <div class="users">
        <table>
            <tr><?= $headers ?></tr>
            <tr><?= $myvalue_row ?></tr>
            <tr><?= $myothervalue_row ?></tr>
        </table>    
    </div>
</form>

Upvotes: 1

Related Questions