0

I'm using pure php for the moment and I'm having a problem on what to do next
using the command DESCRIBE calls all the header of my database but I cannot call those
datas under the headers I have to manually put it.

What I want to know is:

  • Can I populate the Table data automatically?
  • How should I do the coding?
  • Is there any tutorials for that?

This is my code so far:

    <table class="table">
        <tr>

            <?php 
                $sql = "DESCRIBE `employee`";
                $query = $conn->query($sql);
                while ($row = $query->fetch()) {   
            ?>
                <th><?=  $row['Field']; ?></th>
            <?php } ?>

        </tr>

        <?php 
            $sql_datas = "SELECT * From `employee` ";
            $query = $conn->query($sql_datas);
            while ($row_data = $query->fetch()) {

        ?>
        <tr>
            <td><?=  $row_data['id'] ?></td>
            <td><?=  $row_data['last_name'] ?></td>
            <td><?=  $row_data['given_name'] ?></td>
            <td><?=  $row_data['middle_name'] ?></td>
            <td><?=  $row_data['name'] ?></td>
        </tr>        
        <?php } ?>

    </table>
2
Contributors
6
Replies
25
Views
2 Years
Discussion Span
Last Post by joshuajames.delacruz
0

If you are using pure php, I suggest you add a form with method="post" outside your table tag and a submit button. Then your display code can be changed from <td><?= $row_data['last_name'] ?></td> into <td><input type="text" name="last_name[<?= $row_data['id'] ?>]" value="<?= $row_data['last_name'] ?>"/></td> where I assume the id field in your database is unique.
Change similar fields except for id as employee id should not be changed. But add name for the td for id in order for the field to be submit to backend <td name="id[<?= $row_data['id'] ?>]"><?= $row_data['id'] ?></td>

Then after submit, you can get the data and update the table using

<?php
if(!empty($_POST['id'])){
    foreach($_POST['id'] as $id){
        //do your verifications here
        //modify sql script according to what you need
        $sql = "update employee set last_name = '".$_POST['last_name[$id]."']' where id = '$id'";
        //execute update
    }
}
?>

If you don't wish to use form submit, maybe you need to consider to use ajax to do it. The html still modify to add in the text input fields. When trigger the ajax request to update the field with relevant $id during the text-change event.

0

Sorry @lps you must have mis-understood the question. the problem I'm having is populating the data under the table header. I'm not yet done on populating the tables.

although your ahead of me coding the update buttons and delete

At the moment im using pure php for analysis of the flow I will migrate that on ajax codes once this problem is solve

Thanks

0

Wonder if I missing your meaning again now. Maybe what you wish for is:

<tr>
    <?php
    foreach($row['Field']){
        echo "<td>$row_data[$row['Field']]</td>";
    }
    ?>
</tr>

which this code populate the table of <td> with data from tables without manually to set the <td> manually

Edited by lps

0

that code can populate the table's apparently it has a downside to it instead of by row it adds by columns please see the picture

here's the code

 <table class="table">
                <!--Table header-->
                <tr>
                    <?php
                    $sql = "DESCRIBE `employee`";
                    $query = $conn->query($sql);
                    while ($row = $query->fetch()) {
                        $d = $row['Field'];
                        ?>
                        <td><?= $d; ?></td>
                    <?php } ?>
                </tr>





    <!--Table data-->
            <?php

                $q_cnt = $conn->query("DESCRIBE `employee` ");
                while($row_cnt = $q_cnt->fetch()){
                $d_cnt = $row_cnt['Field'];

                    echo " <tr>";
                $sql_data = "SELECT * From `employee` ";
                $query_data = $conn->query($sql_data);
                while ($row_data = $query_data->fetch()){
                    ?>
                            <td>
                                <?= $row_data[ $d_cnt ] ?>
                            </td>
                    <?php
                    }
                    echo "</tr>";
                    }
                    ?>

        </table>

Edited by joshuajames.delacruz

Attachments 1.jpg 58.27 KB
1

see your code

<!--Table data-->
            <?php
                $q_cnt = $conn->query("DESCRIBE `employee` ");
                while($row_cnt = $q_cnt->fetch()){
                $d_cnt = $row_cnt['Field'];
                    echo " <tr>";
                $sql_data = "SELECT * From `employee` ";
                $query_data = $conn->query($sql_data);
                while ($row_data = $query_data->fetch()){
                    ?>
                            <td>
                                <?= $row_data[ $d_cnt ] ?>
                            </td>
                    <?php
                    }
                    echo "</tr>";
                    }
                    ?>
        </table>

The first while loop create the <tr> which create a row, and for each row, the second while loop populate the row with 2 <td>. What your can do is

<table class="table">
        <tr>
            <?php
                $sql = "DESCRIBE `employee`";
                $query = $conn->query($sql);
                $columns = array();
                while ($row = $query->fetch()) {   
                array_push($columns,$row['Field']);
            ?>
                <th><?=  $row['Field']; ?></th>
            <?php } ?>
        </tr>
        <?php 
            $sql_datas = "SELECT * From `employee` ";
            $query = $conn->query($sql_datas);
            while ($row_data = $query->fetch()) {
        ?>
        <tr> 
            <?php 
            foreach($columns as $column){ 
                echo "<td>$row_data[$column]</td>"; 
            } 
            ?> 
            </tr>     
        <?php } ?>
    </table>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.