0

It's pretty simple to create a dynamic table with horizontal headers OR vertical headers alone. But how can I generate dynamic data for a table with BOTH vertical AND horizontal headers? Is this even possible?

Table:

<table>
    <tr>
        <th></th>
        <th scope="col">Monday</th>
        <th scope="col">Tuesday</th>
        <th scope="col">Wednesday</th>
        <th scope="col">Thursday</th>
        <th scope="col">Friday</th>
        <th scope="col">Saturday</th>
        <th scope="col">Sunday</th>
    </tr>
    <tr>
        <th scope="row">Week 1</th>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>1</td>
    </tr>
    <tr>
        <th scope="row">Week 2</th>
        <td>2</td>
        <td>2</td>
        <td>2</td>
        <td>2</td>
        <td>2</td>
        <td>2</td>
        <td>2</td>
    </tr>
    <tr>
        <th scope="row">Week 3</th>
        <td>3</td>
        <td>3</td>
        <td>3</td>
        <td>3</td>
        <td>3</td>
        <td>3</td>
        <td>3</td>
    </tr>
    <tr>
        <th scope="row">Week 4</th>
        <td>4</td>
        <td>4</td>
        <td>4</td>
        <td>4</td>
        <td>4</td>
        <td>4</td>
        <td>4</td>
    </tr>
</table>
2
  • 1
    Yes it is. It can be done by dynamically generating the table. That is done with actual code, not with a DOM. Commented Mar 25, 2017 at 21:44
  • @Xorifelse: Okay I haven't thought of that. How would you go about this? I have no clue? Commented Mar 25, 2017 at 21:49

2 Answers 2

1

Yes it can be easily done in PHP. I've done a small example using a for-loop to give you an easier idea of how this might be accomplished

<table>
    <tr>
        <th></th>
        <th scope="col">Monday</th>
        <th scope="col">Tuesday</th>
        <th scope="col">Wednesday</th>
        <th scope="col">Thursday</th>
        <th scope="col">Friday</th>
        <th scope="col">Saturday</th>
        <th scope="col">Sunday</th>
    </tr>
    <?php
        // Create week row
        for ($week = 0; $week < 10; $week++) {
            echo "            <tr>";
            echo "                <th scope='row'>Week " . $week . "</th>";

            // Create day cells
            for ($day = 0; $day <= 7; $day++) {
                echo "                <td>" . $day . "</td>";
            }

            echo "            </tr>";
        }
    ?>
</table>
Sign up to request clarification or add additional context in comments.

9 Comments

This explains vertical (rows), not dynamic horizontal (cols).
Ah sorry, give me a second. EDIT: @Xor
@LFlare: so $x represents the variable that would be collected from the while statement or am I being mistaken?
@SebastianFarham Sorry, I made the variable names clearer and answered your question more specifically.
@LFlare: Your answer before that was already pretty legit. Good stuff. I think I'm just not being clear. I'm a total n00b in Php. With the for loop how do I use a statement like this <?php while($timemonday=mysqli_fetch_array($resultsheets)){ ?> ? That's where my confusion is.
|
1

To dynamically generate a table horizontally and vertically, you will need 2 loops, the first one being for the amount of rows and the one inside of it for the cols.

<?php

  $rows = 10;
  $cols = 20;

  echo '<table>';
  for($r=0; $r <= $rows; $c++){
    echo "<tr>";
    for($c=0; $c <= $cols; $r++){
      echo "<td>$r</td>";
    }
    echo "</tr>";
  }
  echo '</table>';

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.