0

I have the following code below. The table is displaying directly underneath each other as apposed to the normal way beside each other. I would like the table to display as a normal table but with the below syntax each column is displaying under one another.

The table should look like this:

Sky 1         |   Sky 2        |   Sky 3
sky1result        sky2result       sky3result
sky1result        sky2result       sky3result


<?php

        $f = fopen("/home/app/sky1result.txt", "r");
        echo "<table>";
        echo "<tr><th>Sky 1</th></tr>";
        while(!feof($f)) {
            echo "<tr><td>";
            echo fgets($f),"</td></tr>";
        }

        fclose($f);

        $f1 = fopen("/home/app/sky2result.txt", "r");
        echo "<tr><th>Sky 2</th></tr>";
        while(!feof($f1)) {
            echo "<tr><td>";
            echo fgets($f1),"</td></tr>";
        }

        fclose($f1);

        $f2 = fopen("/home/app/sky3result.txt", "r");
        echo "<tr><th>Sky 3</th></tr>";
        while(!feof($f2)) {
            echo "<tr><td>";
            echo fgets($f2),"</td></tr>";
        }

        fclose($f2);
        echo "</table>";
     ?>
3
  • Whats your question? Commented Sep 14, 2015 at 13:10
  • Please have a look at the HTML output when you view your browser's page source. Copy/paste that output into your question so we can see what markup has actually been produced. Don't use the browser's inspector tool, it has to be from the View Source option Commented Sep 14, 2015 at 13:21
  • 1
    I see that in each loop iteration, you are opening and closing <tr><td></td></tr> so each will definitely result in a new row. Please also show an example of what output you are trying to achieve. Commented Sep 14, 2015 at 13:23

1 Answer 1

1

Edit: Ok, this is a bit tricky. This is the updated version after you comment.

<?php

        // Lets start with collecting the data.
        $column1 = [];
        $f = fopen("/home/app/sky1result.txt", "r");
        while(!feof($f)) {
            $column1[] = fgets($f);
        }
        fclose($f);

        $column2 = [];
        $f = fopen("/home/app/sky2result.txt", "r");
        while(!feof($f)) {
            $column2[] = fgets($f);
        }
        fclose($f);

        $column3 = [];
        $f = fopen("/home/app/sky3result.txt", "r");
        while(!feof($f)) {
            $column3[] = fgets($f);
        }
        fclose($f);

        // We now have three arrays.
        // Lets figure out wich is the largest, so we know how many rows we need.
        $maxRows = max(count($column1), count($column2), count($column3));

     ?>

<table>
    <tr>
        <!-- The headers -->
        <th>Sky 1</th><th>Sky 2</th><th>Sky 3</th>
    </tr>
    <?php for ($i=0; $i < $maxRows; $i++): ?>
        <tr>
            <!-- this is a row. In it we print the column on that row if it exists -->
            <td><?php echo isset($column1[$i]) ? $column1[$i] : '' ?></td>
            <td><?php echo isset($column2[$i]) ? $column2[$i] : '' ?></td>
            <td><?php echo isset($column3[$i]) ? $column3[$i] : '' ?></td>
        </tr>
    <?php endfor ?>
</table>
Sign up to request clarification or add additional context in comments.

4 Comments

It looks like that code is off a little it is not display correctly it keeps looping and making more cells
Can you please show us what the table should look like?
I have edited the question with the updated layout I would like to achieve
Thanks Mikael for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.