[SOLVED] possible array problem.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

[SOLVED] possible array problem.

Post by alix »

Im trying to view information stored in just .txt files on a server, as well as count the number of rows in each .txt file... place the row number beside the row of information.. etc. this is what i have but only the last record in the locations.txt file is showing up.. locations.txt being the where the URL to all the files are. this is the code..

Code: Select all

$Array = file("locations.txt");
for ($x=0; $x < count($Array); $x++) {
echo $Array[$x] . "\n";
$lines = file("$Array[$x]");
echo "<table border=1><tr><td>\n";
foreach ($lines as $line_num => $line) {
echo "<a name=".$line_num."><a href="#".$line_num."">".$line_num."</a><br>\n";
}
echo "</td><td>\n";
foreach ($lines as $line_num => $line) {
echo $line."<br />\n";
} 
echo "</td></tr></table>\n";
}

?>
so in my locations.txt file it looks this
http://myserver.com/folder1/text1.txt
http://myserver.come/folder2/text2.txt
http://myserver.come/folder3/text3.txt

only http://myserver.come/folder3/text3.txt
returns data..

i get this error with the others
Warning: file(http://myserver.come/folder2/text2.txt ): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /folder/count2.php on line 5

which causes problems with the foreach functions..
Anyone know?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there's extra bytes in the filename that you are requesting that make it fail, I'd bet. use [php_man]trim()[/php_man] on each url before requesting it, or better yet, directly access the file to save on bandwidth and time spent waiting for transfers to occur.
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post by alix »

hey what do ya know, i was thinking its giving it an extra space at the end of the url or something.. but yeah, didnt know how to correct it.. Thanks :)
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post by alix »

I was thinking, is it possible for me to make it simply display the text file url and the number of rows of data beside it so i can just click on the fine url beide it to bring up the actual file?

I mean, this works... but it counts with 0 being the first line.. and its hard to read.. this is the code i've been using..

Code: Select all

<?php
$Array = file("locations.txt");
for ($x=0; $x < count($Array); $x++) {
echo "<b>".$Array[$x] . "</b>\n";

$lines = file(trim("$Array[$x]"));
echo "<table border=1><tr><td>\n";
foreach ($lines as $line_num => $line) {
echo "<font color="#FF0000">".$line_num."</face><br>\n";
}
echo "</td><td>\n";
foreach ($lines as $line_num => $line) {
echo htmlspecialchars($line)."<br />\n";
} 
echo "</td></tr></table>\n";
}


?>
its pretty much the same as before just with the trim() function.. i dunno, maybe someone knows a good tutorial where i could possibly learn to do this..
-thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's fairly simple:

Code: Select all

// assume $files is your file list (trimmed already) ::
$output = '&lt;ol&gt;';
foreach($files as $num =&gt; $file)
  $output .= '&lt;li&gt;&lt;a href="' . $_SERVER&#1111;'SCRIPT_NAME'] . '?load=' . $num . '"&gt;' . $file . '&lt;/a&gt; - ' . count( file($file) ) . ' lines&lt;/li&gt;';
$output .= '&lt;/ol&gt;';
echo $output;
now, your script will need some $_GET['load'] handling to know which file to load, which you just need to load the file list, check that the index number requested exists and attempt to load it as you already have..
Post Reply