FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » table appears with empty rows, and row data appears before table. Code seems ok. What is going on?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
table appears with empty rows, and row data appears before table. Code seems ok. What is going on? [message #178162] Wed, 16 May 2012 14:37 Go to next message
kurtk(at)pobox(dot)com is currently offline  kurtk(at)pobox(dot)com
Messages: 10
Registered: May 2012
Karma: 0
Junior Member
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.
Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on? [message #178163 is a reply to message #178162] Wed, 16 May 2012 14:59 Go to previous messageGo to next message
Paul Herber is currently offline  Paul Herber
Messages: 26
Registered: February 2011
Karma: 0
Junior Member
On Wed, 16 May 2012 07:37:22 -0700 (PDT), "kurtk(at)pobox(dot)com" <kurtk(at)pobox(dot)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.

You need <td> ... </td>
around your data.



--
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/
Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on? [message #178168 is a reply to message #178162] Wed, 16 May 2012 20:23 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Wed, 16 May 2012 07:37:22 -0700, kurtk(at)pobox(dot)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
Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on? [message #178172 is a reply to message #178168] Thu, 17 May 2012 11:27 Go to previous messageGo to next message
IRC is currently offline  IRC
Messages: 6
Registered: April 2012
Karma: 0
Junior Member
On May 16, 9:23 pm, Denis McMahon <denismfmcma...@gmail.com> wrote:
> On Wed, 16 May 2012 07:37:22 -0700, ku...@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


You don't even need to include '\n', its doesn't make any sense
putting outside '</td>\n', if you are using table structure.

Please try this:
try {
$fileh = fopen("data/petition-051512.csv", "r");
while ($text = fgets($fileh)) {
$tok = strtok($text, ",");
echo '<tr>';
while ($tok !== FALSE) {
echo "<td>" . $tok . "</td>";
$tok = strtok(",");
}
echo '</tr>';
}
echo "</tbody></table>\n";
return;
Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on? [message #178174 is a reply to message #178172] Thu, 17 May 2012 14:45 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Thu, 17 May 2012 04:27:27 -0700, IRC wrote:

> You don't even need to include '\n', its doesn't make any sense putting
> outside '</td>\n', if you are using table structure.

Nope, you don't have to include any white space.

But when you need to "view source" in the browser to see what's going
wrong, a table that is delimited by newlines between cells and rows is a
much easier to read than a 500+ row table on a single line.

Rgds

Denis McMahon
Re: table appears with empty rows, and row data appears before table. Code seems ok. What is going on? [message #178175 is a reply to message #178174] Thu, 17 May 2012 17:37 Go to previous message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Denis McMahon wrote:

>
> But when you need to "view source" in the browser to see what's going
> wrong, a table that is delimited by newlines between cells and rows is a
> much easier to read than a 500+ row table on a single line.
+1


--
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Solution Manuals & Test Banks Updated 2012
Next Topic: update mysql without manually tying SET statements
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 04:07:30 GMT 2024

Total time taken to generate the page: 0.03223 seconds