简体   繁体   中英

Populate HTML table with SQL data and PHP

I am trying to populate a table using aa few loops.

First loop fills up the table headers with users in a row, no problem with that.

The problem is populating tds as rows with the right ticket number.

I can get all numbers in an order column in a single td, which is not how it should work

Something like this,

---------------------
user1 | user2 | user3
---------------------
00001 | 00004 | 00007
00002 | 00005 | 00008
00003 | 00006 | 00009
---------------------

and it should be,

---------------------
user1 | user2 | user3
---------------------
00001 | 00004 | 00007
---------------------
00002 | 00005 | 00008
---------------------
00003 | 00006 | 00009
---------------------

You get the idea.

The this is the code I am using for that,

<table class="table table-hover">
    <thead>
        <tr>
            <th scope="row">
                <?php
                $userName = functionName();
                for ($userName->rewind(); $userName->pointer < $userName->size; $userName->next()) {
                    $record = $userName->current();
                    $firstName = $record->fields->FirstName;
                    ?>
                <th><?php echo $firstName; ?></th>
<?php } ?>
            </th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <th scope="row">
                <?php
                for ($userName->rewind(); $userName->pointer < $userName->size; $userName->next()) {
                    $record = $userName->current();
                    $firstName = $record->fields->FirstName;
                    ?>

                <td>
                    <?php
                    $userCase = functionCase($firstName);
                    for ($userCase->rewind(); $userCase->pointer < $userCase->size; $userCase->next()) {
                        $record = $userCase->current();
                        $caseNumber = $record->fields->CaseNumber;
                        $status = $record->fields->Status;

                        echo $caseNumber;
                        ?>
                    <?php }
                ?>
                </td>
<?php } ?>
            </th>
        </tr>   
    </tbody>
</table>

I think I might be using the for wrong logic and loops for that part.

Any ideas how to achieve the proper table result?

I think you are using an incorrect HTML table layout.

You have a td inside a th, which is not allowed as far as I know. If you want to display your data in several rows, you have to create a tr for every row. The trs can then contain th and td items. For a valid table layout, please refer to http://www.w3schools.com/tags/att_th_scope.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM