1

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.

  • 1. Move and elements that you render inside the loop now into three different partial template files. 2. Use php ob_start() and ob_get_clean() functions to add each row content into three different variable inside one loop. 3. In the main template just echo each of the variable that contains table row content inside their row element. – Ivan Ovcharenko Sep 15 '20 at 16:20
  • What do you mean for a partial template? –  Sep 15 '20 at 16:43
  • I mean that you could move it to a separate file. The first file content will be i.e. the following $singledata And then "render" it to the variable like here: https://stackoverflow.com/questions/34593130/render-php-file-into-string-variable – Ivan Ovcharenko Sep 15 '20 at 17:00

1 Answers1

1

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>
Nick
  • 138,499
  • 22
  • 57
  • 95