Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #6080 > unrolled thread
| Started by | "kurtk@pobox.com" <kurtk@pobox.com> |
|---|---|
| First post | 2012-05-16 07:37 -0700 |
| Last post | 2012-05-17 18:37 +0100 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.php
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
| From | "kurtk@pobox.com" <kurtk@pobox.com> |
|---|---|
| Date | 2012-05-16 07:37 -0700 |
| Subject | table appears with empty rows, and row data appears before table. Code seems ok. What is going on? |
| Message-ID | <15449691.1592.1337179042368.JavaMail.geo-discussion-forums@ynja13> |
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.
[toc] | [next] | [standalone]
| From | Paul Herber <paul@pherber.com> |
|---|---|
| Date | 2012-05-16 15:59 +0100 |
| Message-ID | <i4g7r79f0k1n9m24csam88vnsddklevvov@news.eternal-september.org> |
| In reply to | #6080 |
On Wed, 16 May 2012 07:37:22 -0700 (PDT), "kurtk@pobox.com" <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.
You need <td> ... </td>
around your data.
--
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2012-05-16 20:23 +0000 |
| Message-ID | <jp12c3$u8v$1@dont-email.me> |
| In reply to | #6080 |
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
[toc] | [prev] | [next] | [standalone]
| From | IRC <ircadhikari@gmail.com> |
|---|---|
| Date | 2012-05-17 04:27 -0700 |
| Message-ID | <7f29497c-7566-4ef8-b812-31c3bca40aad@w13g2000vbc.googlegroups.com> |
| In reply to | #6086 |
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;
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2012-05-17 14:45 +0000 |
| Message-ID | <jp32u4$7ob$1@dont-email.me> |
| In reply to | #6090 |
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
[toc] | [prev] | [next] | [standalone]
| From | The Natural Philosopher <tnp@invalid.invalid> |
|---|---|
| Date | 2012-05-17 18:37 +0100 |
| Message-ID | <jp3d1d$vl6$1@news.albasani.net> |
| In reply to | #6092 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web