We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Read File And Output in Table

deharvy
deharvy asked
on
Medium Priority
258 Views
Last Modified: 2012-06-22
I am currently reading a file and displaying using this:

        $file = fopen($filename.".txt","r");
            while(! feof($file)) {
              echo fgets($file). "<br />";
        }

I need to modify this logic to support a new output format that would include a table (rather than just raw string).

The contents of the uploaded file will be like this::

team=nyy,player=arod,position=3b
team=nym,player=jreyes,position=ss
team=stl,player=apujols,position=1b
player=cargo,position=cf
team=pit,player=gjones

The expected HTML output should be in a table with header / value. I have provided an example JPG attached.

I also want to support an export feature that would output into a CSV format. I have provided an example CSV file.

Lines which are missing a header/value should simply be converted to empty (if possible).

This would be a great help for me. Please let me know what needs to be done.
example.JPG
example.csv
Comment
Watch Question

Commented:
$data = array();
$file = fopen($filename.".txt","r");
while(! feof($file)) {
$line = fgets($file);
$row = explode(',', $line);
$t = array();
foreach($row as $e) {
$r = explode('=',$e, 2);
$t[ $r[0] ] = $r[1];
}
$data[] = $t;
}

echo '<table>';
foreach( $data as $row ) {
echo '<tr>';
foreach($row as $k => $e) {
 echo '<td>',$e,'</td>';
}
echo '</tr>';
}
echo '</table>';

Open in new window

Author

Commented:
Thank you for the quick response. I'm trying to test this but not getting good results. Will try further and let you know.
Aaron TomoskyDirector, SD-WAN Solutions
CERTIFIED EXPERT

Commented:
Just a suggestion, switch to file_get_contents. Much easier than fopen

Author

Commented:
4e4en,

Your suggested code works well for the most part as I'm able to see the table with values. I am only missing the top headers.

If you can help with that, would be greatly appreciated.

Commented:
$data = array();
$file = fopen($filename.".txt","r");
while(! feof($file)) {
$line = fgets($file);
$row = explode(',', $line);
$t = array();
foreach($row as $e) {
$r = explode('=',$e, 2);
$t[ $r[0] ] = $r[1];
}
$data[] = $t;
}

echo '<table>';

if( isset($data[0] ) ) {
echo '<tr>';
foreach($data[0] as $k => $e) {
 echo '<td>',$k,'</td>';
}
echo '</tr>';
}

foreach( $data as $row ) {
echo '<tr>';
foreach($row as $k => $e) {
 echo '<td>',$e,'</td>';
}
echo '</tr>';
}
echo '</table>';

Open in new window

Author

Commented:
4e4en,

Your latest update works great and provides most header columns.

But it looks like it's looking at the first message to build the header columns and not the message that has most of the headers.

For example, if I was to switch my table to this, the information would end up in wrong column and blank headers. :

team=pit,player=gjones
player=cargo,position=cf
team=nyy,player=arod,position=3b
team=nym,player=jreyes,position=ss
team=stl,player=apujols,position=1b

What is the best way to ensure that all column headers are available and information is populated in the right column?

I can open up a new question if you like so your code is working for the format I originally provided.

Let me know.
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Ray_Paseur,

Thanks, this would probably be a good solution for the my csv output; so that's a piece of the puzzle.

Just think I need a little update to 4e4en's solution to be finished.

Will continue trying to tweak in the meantime.

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Click the link I posted above, then use "view source" to see what the script created.  Thanks.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
4e4en,

Is there a way to do this without defining $data_tpl with static values? Meaning, is there a way to create the array dynamically?

I ask because some data might introduce new columns such as:

team=tb,player=sfuld,rank=2

If too much work (or will totally change the code), this will work for me as is and I'll use static columns. Just let me know if it can be done dynamically.

Commented:
to do what you are suggesting would require loop trought $data to collect all column names.

$data_tpl = array();
$data = array();
$lines = array();

$file = fopen($filename.".txt","r");
while(! feof($file)) {
  $line = fgets($file);
  $row = explode(',', $line);

  $t = array();
  foreach($row as $e) {
    $r = explode('=',$e, 2);
    $t[ ] = $r;
  }

  $lines[] = $t;
}

foreach( $lines as $line ) {
  $data_tpl = array_merge( $data_tpl, array_keys( $line ) );
}

foreach( $lines as $line ) {
$t = $data_tpl;
foreach( $line as $r) {
 $t[ $r[0] ] = $r[1];
}
$data[] = $t;
}


echo '<table>';

if( isset($data[0] ) ) {
echo '<tr>';
foreach($data[0] as $k => $e) {
 echo '<td>',$k,'</td>';
}
echo '</tr>';
}

foreach( $data as $row ) {
echo '<tr>';
foreach($row as $k => $e) {
 echo '<td>',$e,'</td>';
}
echo '</tr>';
}
echo '</table>';

Open in new window

Most Valuable Expert 2011
Author of the Year 2014

Commented:
some data might introduce new columns

This question has become a moving target and it's now something you might want to hire a developer to build for you.  We can answer questions that have some fairly well-bounded scope but we cannot write your programming for you for free.  I showed you a generalized solution based on the original question.  Using that you can modify the $columns array to meet your requirements.  But if you do not know how to do that immediately, you either need a developer or you need to take a month or three to learn PHP.  I highly recommend this book:
http://www.sitepoint.com/books/phpmysql4/

Anyway your original question is answered with a code example and a link to an online demonstration script that shows you how the code example works.  Best of luck with your project. ~Ray

Author

Commented:
Thanks for all your help!

Author

Commented:
Ray,

I understand your stance and therefore closed the question. You are right, it has been answered and the assistance given has put me in a good position.

---------------

4e4en,

Appreciate all your assistance these last couple days.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.