R1123
R1123

Reputation: 11

populate html table in PHP

I am at the end of my project, I have all the data from my database and everything is working fine I just can not work out how to display it in a table.

I have generated a table which is the (number of projects) * (the number of people).

The data I have collected is user_id, project_id and hours.

But how do I insert '6' (hours) into user_x's row in the column of the correct project?

I can only think to make x arrays (for the number of projects) of the length for the number of users and evaluate the project code to select the correct array and then use the user id to get the correct position to place the value and simply spit out the array into the td tag

This is incredibly messy, I wonder if I'm going about it completely wrong.

If this is indeed the best way I need to recursively create arrays and write code which references variables that might not even exist. Sounds insane to me

//for the length of projects create arrays that are the length of users and fill with 0's
for ($v = 0; $v < $rows_x; $v++){
$name = "variable{$v}"; 
 $$name = array_fill(0, $rows_u, '0');

EDIT: What I am trying to do is show the number of hours that are booked to projects between two dates. I have gotten all the data correctly but now I need the data to land into a table so you can easily see which user booked to what project.

In an excel world I could use the project number to select the Y axis and the user id to select the X axis. However I don't know the best way to do this in php.

Surely creating an array for each project the length of the users and filling with data if there is data is not the best way.

Upvotes: 1

Views: 1763

Answers (1)

user3574640
user3574640

Reputation:

I don't know if this is what you're looking for, but I insert my values into a table in the process of retrieving them.

You can echo the values into a <td> as long as they are in your SQL SELECT statement.

<table>
    <thead>
            <tr>
                <th>Values1</th>
                <th>Values2</th>
            </tr>
    </thead>
    <tbody>
        <?php

        $sql = "SELECT value1, value2
                FROM tbl_Values";

        if (!$res = $link->query($sql)) {
            trigger_error('Error in query ' . $link->error);
        } else {
            while ($row = $res->fetch_assoc()) {
                ?>
                <tr>
                    <td>
                       <?php echo $row['value1']; ?>
                    </td>
                    <td>
                       <?php echo $row['value2']; ?>
                    </td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
</table> 

This is the way I put data into my tables, without using arrays.

Upvotes: 1

Related Questions