Page 1 of 1

Displaying contents of a PHP file inside a table.

Posted: Tue Feb 07, 2012 11:06 pm
by BBoyd
I'm trying to read another PHP file and then take the data that is inside and display it in a table. Below is code that kind of works, but there are a couple of issues.

-First is that it reads only the very first item in the file and just repeats it.
-Second, within the loop I have if going until it reaches 20, but would like the loop to continue until the end of file, but not sure how to do this. Maybe combine both a for and while loop?

Inside the file on each line are 4 separate entries separated by a space to make up one record, then the next row contains a new record and so on.

I've been searching trying to figure this out, but not getting anywhere. I keep reading about using explode instead, but that might be a little down the road right now. This is for an assignment and we are currently studying strtok so I need to stick with it.

Any help is very much appreciated.

Brian

Code: Select all

<?php

    include 'file.php';
    $token = strtok($filename, " \n");

    echo "<table border=\"1\">";
    echo '<tr>';
    for ($i = 1; $i <= 20; $i++) {
        echo "<td>" . $token . "</td>";
        if ($i % 4 == 0) {
        echo '</tr></tr>';
        }
    }
        echo '</tr>';
        echo "</table>";

?>

Re: Displaying contents of a PHP file inside a table.

Posted: Wed Feb 08, 2012 12:23 am
by BBoyd
The following code will include the data in file.php, but the table is just one continuous row.

How can I code this to make another table row after the 4th field?

Code: Select all

<?php
include 'file.php';
$token = strtok($filename, " \n");

    echo '<table border=\"1\">';
    echo '<tr>';
    
    while ($token !== false) {
    echo "<td>$token</td>";
    $token = strtok(" \n");
}
    echo '</tr>';
    echo '</table>';
?>

Re: Displaying contents of a PHP file inside a table.

Posted: Wed Feb 08, 2012 1:01 am
by BBoyd
The following code provides the solution for adding the data to tables, but now I need some advise as to how to limit the columns to just 4.

Code: Select all

<?php
include 'file.php';
$token = strtok($filename, " \n");
$rows = 20;
$columns = 4;

    echo "<table>";

    // loop to create rows
    for ($r = 1; $r <= $rows; $r++) {
        echo "<tr>";

        // loop to create columns
        for ($c = 1; $c <= $columns; $c++) {
            echo "<td>$token</td>";
            $token = strtok(" \n");
        }
    echo "</tr>";
    }
    echo "</table>";

?>

Re: Displaying contents of a PHP file inside a table.

Posted: Wed Feb 08, 2012 1:10 am
by social_experiment

Code: Select all

<?php
 // loop to create columns
        for ($c = 1; $c <= $columns; $c++) {
            echo "<td>$token</td>";
            $token = strtok(" \n");
        }
?>
Doesn't this produce only 4 columns?

Re: Displaying contents of a PHP file inside a table.

Posted: Wed Feb 08, 2012 1:39 am
by BBoyd
social_experiment wrote:

Code: Select all

<?php
 // loop to create columns
        for ($c = 1; $c <= $columns; $c++) {
            echo "<td>$token</td>";
            $token = strtok(" \n");
        }
?>
Doesn't this produce only 4 columns?
Sorry. It's been a long day and I meant to say how to have the rows continue until it reaches the end of the file. Currently the rows are specified.

BTW... after rereading the instructions, I may have to do this a different way. The second field has numbers from 1-12 that represents the month. However, instead of displaying the number, it needs to display the name of the month. So an if statement needs to happen for the switch, but not sure how to do that within the code I posted.

It's several hours past my bedtime. Tomorrow is another day!

Brian