Home > Back-end >  php - how to display selected options in a table?
php - how to display selected options in a table?

Time:12-06

i have a table with select boxes. i want to display all of selected options same as the table (like matrix, maybe). but it is just displaying one column at the bottom. i've been looking for the solutions for hours but it seems like i need to ask this one here. any suggestion would be great. so, here is the code:

`

<style>
    table, th, td {
  border: 1px solid;
}
</style>

<table>
    <thead>
        <tr>
            <td></td>
            <td>C1</td>
            <td>C2</td>
            <td>C3</td>
        </tr>
    </thead>
    <tbody>
        <form action="" method="POST" id="formid">
            <?php
            for ($i=1; $i <= 3 ; $i  ) {
                ?>
            <tr>
                <td>A<?php echo $i ?></td>

                <?php
                
                for ($k=1; $k <= 3 ; $k  ) {
                ?>

                <td>
                    <select name="selectid[]">
                        <option disabled selected>-Select-</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>
                    <?php } } ?>
                </td>
            </tr>
        </form>
    </tbody>
</table>
        <input type="submit" form="formid" name="submit" value="Submit">



<!-- display selected options -->
<?php if (isset($_POST['submit'])) { ?>
<style>
    table, th, td {
  border: 1px solid;
}
</style>

<table>
    <thead>
    <tr>
        <td>
            <td>C1</td>
            <td>C2</td>
            <td>C3</td>
        </td>
    </tr>
    </thead>
    <tbody>

        <?php
            for ($i=1; $i <= 3 ; $i  ) { ?>
            <tr>
                <td>A<?php echo $i ?></td>

                <?php
            }
                $select = $_POST['selectid'];
                foreach ($select as $value) {
                ?>
            
            <td><?php echo $value ?></td>
        <?php } ?>
            </tr>
        
    </tbody>
</table>

<?php } ?>

`

edit: result from what i'm doing

CodePudding user response:

There was a couple of issues in the code, the first issue is that the post request just pushes data to the array without really acknowledging where this data came from in the page. You can strictly identify the index in a 2d array (seeing how the table is 2d).

Additionally, you were not including the row in the first loop, which was pushing all the data to the last row. Here is a roughly working example that achieves what you want to do.

<style>
    table, th, td {
  border: 1px solid;
}
</style>

<table>
    <thead>
        <tr>
            <td></td>
            <td>C1</td>
            <td>C2</td>
            <td>C3</td>
        </tr>
    </thead>
    <tbody>
        <form action="" method="POST" id="formid">
            <?php
            for ($i=0; $i < 3 ; $i  ) {
                ?>
            <tr>
                <td>A<?php echo $i ?></td>

                <?php
                
                for ($k=0; $k < 3 ; $k  ) {
                ?>

                <td>
                    
                    <select name="selectid[<?php echo $i ?>][<?php echo $k ?>]">
                        <option disabled selected>-Select-</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>
                    <?php } } ?>
                </td>
            </tr>
        </form>
    </tbody>
</table>
        <input type="submit" form="formid" name="submit" value="Submit">



<!-- display selected options -->
<?php if (isset($_POST['submit'])) { ?>
<style>
    table, th, td {
  border: 1px solid;
}
</style>

<table>
    <thead>
    <tr>
        <td>
            <td>C1</td>
            <td>C2</td>
            <td>C3</td>
        </td>
    </tr>
    </thead>
    <tbody>
        <?php
        for ($i=0; $i < 3 ; $i  ) {
            ?>
        <tr>
            <td>A<?php echo $i ?></td>

            <?php
            
            for ($k=0; $k < 3 ; $k  ) {
            ?>

            <td>
                <?php 
                if(isset($_POST['selectid'][$i][$k])){
                    echo $_POST['selectid'][$i][$k];
                }
                ?>
                <?php } } ?>
            </td>
        </tr>

        
        
    </tbody>
</table>
<?php 
} ?>
  • Related