Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6086

Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on?

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on?
Date 2012-05-16 20:23 +0000
Organization A noiseless patient Spider
Message-ID <jp12c3$u8v$1@dont-email.me> (permalink)
References <15449691.1592.1337179042368.JavaMail.geo-discussion-forums@ynja13>

Show all headers | View raw


On Wed, 16 May 2012 07:37:22 -0700, kurtk@pobox.com wrote:

> I have code which displays a HTML <table>. The data in the rows comes is
> read from a file. But the row data is appearing (minus the <tr></tr>
> tags) on the web page before the table. The table displays with empty
> rows below what was supposed to be the row data.
> 
> In other words this php...
> 
> echo "<table>\n<thead>\n<tr><th>number</th><th>date</th><th>First
> Name</th><th>Last
> Name</th><th>state</th><th>country</th><th>remarks</th></tr>\n</thead>
\n<tbody>\n";
> 
> try {
>       
>    $fileh = fopen("data/petition-051512.csv", "r");
>        
>    while ($text = fgets($fileh)) {
>        
>         $tok = strtok($text, ",");
> 
>         while ($tok !== FALSE) {
>             
>              echo "<tr>" . $tok . "</tr>\n";
> 
>              $tok = strtok(",");
>         }
>    }
>    echo "</tbody></table>\n";
>    return;
> 
> produces html that looks like one big mass of text, consisting of the
> 560 rows of data, minus any table tags, followed by a table with 560
> empty rows.
> 
> I can't figure this out.

The content of each "$tok" is being wrapped by a "tr" element. A "tr" 
element in html can not contain content directly, just table cell 
elements (td or th usually). So your browser finds text in an illegal 
place, and places it before the table (other browsers might do other 
things with it).

Try on of the following:

while ( $arr = fgetcsv( $fileh ) ) {
  echo "<tr>";
  for ( $tok in $arr )
    echo "<td>$tok</td>"
  echo "</tr>";
}

or

>    while ($text = fgets($fileh)) {
          echo "<tr>\n";
>         $tok = strtok($text, ",");
>         while ($tok !== FALSE) {
>              echo "<td>" . $tok . "</td>\n";
>              $tok = strtok(",");
>         }
          echo "</tr>\n";
>    }

Note also that your code will split a csv field that contains a comma in 
text, eg for the following two csv fields:

"this is text, and text, and text","this,is,more,text"

Your code will generate 7 table cells from the two csv fields.

This may be what you want, or it may not. Your code will also include the 
" marks in the output, which might not be what you want?

Rgds

Denis McMahon

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

table appears with empty rows, and row data appears before table. Code seems ok. What is going on? "kurtk@pobox.com" <kurtk@pobox.com> - 2012-05-16 07:37 -0700
  Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on? Paul Herber <paul@pherber.com> - 2012-05-16 15:59 +0100
  Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on? Denis McMahon <denismfmcmahon@gmail.com> - 2012-05-16 20:23 +0000
    Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on? IRC <ircadhikari@gmail.com> - 2012-05-17 04:27 -0700
      Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on? Denis McMahon <denismfmcmahon@gmail.com> - 2012-05-17 14:45 +0000
        Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on? The Natural Philosopher <tnp@invalid.invalid> - 2012-05-17 18:37 +0100

csiph-web