Tuesday, 27 November 2018

Building html table using php

I am looking to have a table built using php but with a twist. I want to only limit it to 10 rows. If the variable is greater than 10, then a new column is added. IE:

If the variable is 9 then the table will have 1 column and 9 rows. If the number is 19 then the table will contain 10 rows and 2 columns with the second columns containing numbers 11 - 19. And so on Similar to below

+----+-------+-------+----+-------+-------+----+-------+-------+
| Q  | Tally | Total | Q  | Tally | Total | Q  | Tally | Total |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 1  |       |       | 11 |       |       | 21 |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 2  |       |       | 12 |       |       | 22 |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 3  |       |       | 13 |       |       | 23 |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 4  |       |       | 14 |       |       | 24 |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 5  |       |       | 15 |       |       | 25 |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 6  |       |       | 16 |       |       |    |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 7  |       |       | 17 |       |       |    |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 8  |       |       | 18 |       |       |    |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 9  |       |       | 19 |       |       |    |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 10 |       |       | 20 |       |       |    |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+

Any idea how I can achieve this?

I have gotten this far

$table_string = 'Table Name 1:45,Table Name 2:20,Table Name 3:9';

function foo()
{
    $tables = explode(',', $table_string);
    $output = '';

    foreach ($tables as $table)
    {
        $table_name = explode(':', $table)[0];
        $table_rows = explode(':',$table)[1];
        $table_columns = ceil($table_rows/10);

        $output .= "<br>

        <table class='table table-bordered table-small'>
            <tr>
                <th scope='col' colspan='99'>{$table_name}</th>
            </tr>";

        for ($x = 1; $x <=10; $x++)
        {
            $output .= "<tr>";

            for ($y = 1; $y <= $table_columns * 3; $y++)
            {
                if ($y % 3 == 1) {
                    $output .= "<td scope=\"col\">Q</td>";
                } else if ($y % 3 == 2) {
                    $output .= "<td scope=\"col\">Tally</td>";
                } else  {
                    $output .= "<td scope=\"col\">Total</td>";
                }
            }

            $output .= "</tr>";
            $output .= "<tr>";

            for ($y = 1; $y <= $table_columns * 3; $y++)
            {
                if ($y == 1 || $y % 4 == 0) {
                    $z = ceil($y / 4);
                    $output .= "<td scope=\"row\">{$z}</td>";
                } else {
                    $output .= "<td></td>";
                }
            }

            $output .= "</tr>";
        }

        $output .= "</table>";
    }

    return $output;
}

To help answer the question more: Here is the current output I get:

+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| Q | Tally | Total | Q | Tally | Total | Q | Tally | Total | Q | Tally | Total | Q | Tally | Total |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+



from Building html table using php

No comments:

Post a Comment