Displaying contents of a PHP file inside a table.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Displaying contents of a PHP file inside a table.

Post 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>";

?>
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

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

Post 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>';
?>
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

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

Post 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>";

?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

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

Post 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
Post Reply