Forum Moderators: coopster

Message Too Old, No Replies

reading from a txt file

         

dpolanco

9:53 pm on May 28, 2010 (gmt 0)

10+ Year Member



hi, i'm a newbie and probably there are a thousand more convinient ways of doing this, but this is what I have so far. I need to read some data from a txt file and I have this code mixed with html in order to fill dynamically a table:

<tr>
<?
$lines = file('enfoque.txt');
foreach ($lines as $line_num =>; $line) {
$datos = explode(",", $line);
?>
<td>
<?= $datos[0]?>
<?= $datos[1]?>
</td>
</tr>
<tr>
<td>
<?= $datos[2]?>
</td>
</tr>
<?
}
?>

in some servers works fine, the table appears and makes as many rows as data in the file, but in some other servers I get this in the web page:

; $line) { $datos = explode(",", $line); ?>

all servers have php5
Can you guys please help me to fix it? or plz let me know a better way to do this
Thx

astupidname

10:58 pm on May 28, 2010 (gmt 0)

10+ Year Member



foreach ($lines as $line_num => ; $line) {


See the problem there after the => ? I find it hard to believe it works in any servers, though I have not tried a loop with an errant semi-colon in like there! :) At any rate, try removing the semi-colon from there.
Also, do not use the short-tag style of php opening tags--> <? always use the long opening tags--> <?php then you never need mess with the .ini settings to allow the short tags. Besides, it's kinda stupid that the php language even allows the different tags in the first place, based on a setting, should just stick with the one main kind (the long tags) and that's that then.

p.s. Welcome to webmasterworld!

dpolanco

3:04 am on May 29, 2010 (gmt 0)

10+ Year Member



hi, thx for your answer, i didnt have the semi-colon in first place but reading another post here someone had it and no one corrected it so I thought it was ok :)
I removed it already, using the long opening tag but it still doesn't work...

<tr>
<? php
$lines = file('enfoque.txt');
foreach ($lines as $line_num =>; $line) {
$datos = explode(",", $line);
?>
<td>
<?php = $datos[0]?>
<?php = $datos[1]?>
</td>
</tr>
<tr>
<td>
<?php = $datos[2]?>
</td>
</tr>
<?
}
?>

my question is why it is returning in some servers this line
$line) { $datos = explode(",", $line); ?>

it is obvious that it is not processing this as code but as text and printing it...

tangor

3:59 am on May 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Be careful about tags... don't insert spaces!

<? php should be <?php

rocknbil

4:08 am on May 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've no idea why that's printing to the screen, but it's probably got to do with switching in and out of PHP and HTML. Working test for you.

Contents of test2.txt. Note the empty lines.

this,is, some

comma,separated

text,we're,going
to,read

Working script, file() is acting hinky (see my comments)


<?php
$file = "test2.txt";
header("content-type:text/html");
echo "<p>reading in comma separated file $file</p>\n";
$lines = file($file);
foreach ($lines as $line_num=>$line) {
if (preg_match('/^\s+$/',$line)) { continue; } // skip blank lines
// I could not get FILE_IGNORE_NEW_LINES or rtrim
// to kill newlines. So . . .
$line = preg_replace('/[\n\r]/','',$line);
$fields = explode(',',$line);
$len = count($fields); // So Readie doesn't yell at me. :-)
echo "<p>line $line_num ";
for ($i=0;$i<$len;$i++) {
echo " field $i: '" . $fields[$i] . "'";
}
echo "</p>\n";
}
?>


Previous was tested and works.

The ; has no place in your foreach, people often typo here trying to help. When in doubt, see the documentation:

File() [us.php.net]

dpolanco

12:35 pm on May 29, 2010 (gmt 0)

10+ Year Member



hi Rock I test ur code and got the same, it prints

$line) { if (preg_match('/^\s+$/',$line)) { continue; } // skip blank lines // I could not get FILE_IGNORE_NEW_LINES or rtrim // to kill newlines. So . . . $line = preg_replace('/[\n\r]/','',$line); $fields = explode(',',$line); $len = count($fields); // So Readie doesn't yell at me. :-) echo "
line $line_num "; for ($i=0;$i<$len;$i++) { echo " field $i: '" . $fields[$i] . "'"; } echo "

\n"; } ?>

somehow just after foreach "($lines as $line_num=>" it dont recognize this as code and print it as text, so I'm wondering that I need to change some config in the server to make php able to process this... what is it?

Alcoholico

3:12 pm on May 29, 2010 (gmt 0)

10+ Year Member



Try this:

<table>
<?php
$lines = file('enfoque.txt');
foreach ($lines as $line) {
$datos = explode(',', $line);
?>
<tr>
<td><?php echo $datos[0] ?><?php echo $datos[1] ?></td>
</tr>
<tr>
<td><?php echo $datos[2] ?></td>
</tr>
<?php
}
?>
</table>

dpolanco

6:17 pm on May 29, 2010 (gmt 0)

10+ Year Member



thx Alcoholico, but this didnt print anything

dpolanco

8:06 pm on May 29, 2010 (gmt 0)

10+ Year Member



I improved it a little:

<tr>
<?php foreach (file('enfoque.txt') as $line_num => $line): ?>
<?php $datos = explode(",", $line); ?>
<td>
<?php echo $datos[0]?>
<?php echo $datos[1]?>
</td>
</tr>
<tr>
<td>
<?php echo $datos[2]?>
</td>
</tr>
<?php endforeach ?>

but i'm still getting "$line): ?>" as the ouput

astupidname

8:34 pm on May 29, 2010 (gmt 0)

10+ Year Member



Is there more php code before this block of code somewheres? It's starting to sound almost as if there might be a previous block of code which is missing the closing tag perhaps? Does this work?:
<?php
echo 'Hello world!';
?>


Or how about this?:
?>
<?php
echo 'Hello world';
?>


Or how about this, does this display all php configuration info for you?(do not post full results of this here if it works):
<?php
phpinfo();
?>

dpolanco

9:00 pm on May 29, 2010 (gmt 0)

10+ Year Member



no only html code

dpolanco

9:01 pm on May 29, 2010 (gmt 0)

10+ Year Member



sry, i only have html code before this and phpinfo() works:
Apr 28 14:57:29 UTC 2010 i686
Build DateMay 13 2010 19:49:13
Server APIApache 2.0 Handler
Virtual Directory Supportdisabled
Configuration File (php.ini) Path/etc/php5/apache2
Loaded Configuration File/etc/php5/apache2/php.ini
Scan this dir for additional .ini files/etc/php5/apache2/conf.d
...

astupidname

9:24 pm on May 29, 2010 (gmt 0)

10+ Year Member



Try this: (do not copy and paste from Internet Explorer, may lose formatting, do so in Firefox or other):

<table>
<?php
$lines = file('enfoque.txt');
foreach ($lines as $line) {
$line = preg_replace("/[\r\n]/", '', trim($line));
if (strlen($line)) {
$datos = explode(',', $line);
if (count($datos) >= 3) {
echo '<tr><td>'.$datos[0].$datos[1].'</td></tr>';
echo '<tr><td>'.$datos[2].'</td></tr>';
}
}
}
?>
</table>

dpolanco

9:54 pm on May 29, 2010 (gmt 0)

10+ Year Member



thx a...name :D
but it prints this now:

= 3) { echo ''; echo ''; } } } ?>
'.$datos[0].$datos[1].'
'.$datos[2].'

astupidname

9:59 pm on May 29, 2010 (gmt 0)

10+ Year Member



Try the latest example I posted on a page all by it's self, with nothing else on the page whatsoever. Must be a problem elsewhere or something, other than that, the example does work here. And make sure the 'enfoque.txt' file is in same directory. Are you receiving any error messages or warnings from php?

astupidname

10:23 pm on May 29, 2010 (gmt 0)

10+ Year Member



Should also ask you about your manner of accessing the file. Do you have it named with a .php extension such as 'test.php'? Also assuming you do have it on a web server or local wamp server and not just a local file viewed in browser...

dpolanco

10:29 pm on May 29, 2010 (gmt 0)

10+ Year Member



i found out the problem... i feel like the noobest guy in the world... but i'm gonna post it just in case some1 makes the same mistake: didnt know some servers read the php code inside a html file and some need some sort of config to do that... so i renamed the file to .php and it worked
thx to everybody for the help

rocknbil

11:55 pm on May 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<smacks forehead> Ah we should have all guessed that was it . . . . LOL . . . yeah PHP extension. Good for you!

tangor

4:33 am on May 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Beauty of this resolution (and thanks dpolanco for fessing up to the solution!) is that even the experts need a constant reminder to "check the basics" as well as the arcane!