Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop will not end 1

Status
Not open for further replies.

percent

IS-IT--Management
Apr 27, 2004
176
US
I have a loop that will not end

in the list.txt file I have 4 lines
text1
text2
text3
text4

in the link.txt file I have 4 lines

when I run the code the last link/text4 is created and is continuing to be created and will not stop unless I stop the browser - I do not see what is wrong with my 'for' code please help

any help would be appreciated


Code:
<?php

$line = 1; 

//load list.txt file into an array each line will be a menu item

$listhandle = fopen("[URL unfurl="true"]http://www.fakedomain.com/list.txt",[/URL] "r"); //open file with readonly access as list handle variable
$linkhandle = fopen("[URL unfurl="true"]http://www.fakedomain.com/link.txt",[/URL] "r"); //open file with readonly access as link handle variable

while (!feof($listhandle)) {

$listbuffer = fgets($listhandle, 4096); //retrive line from file and stores it in the list buffer variable
$linkbuffer = fgets($linkhandle, 4096); //retrive line from file and stores it in the link buffer variable


$zlist[$line] = $listbuffer;
$zlink[$line] = $linkbuffer;





$line++; //increment line variable


}

fclose($listhandle); //Close file
fclose($linkhandle); //Close file


echo "<body>";


for ($i = 1; $i = count($zlist); $i++){

echo "<p><a href=$zlink[$i]>$zlist[$i]</a></p>\n";

}

echo "</body>";

?>
 
You don't have to read line by line. Did you consider the file() function which reads the entire file into an array where each line is an entry?
Also, if the file is on your system I'd recommend to cut out the URL wrappers and load it with the absolute filesystem path. Much faster.
Also note, that feof() with an invalid filehandle does not return false.
 
thanks
I was able to shorten the code and its working now - I guess I needed a push in a different direction to find another solution

Code:
<?php

$zlist = file('../src/list.txt');
$zlink = file('../src/link.txt');

echo "<body>";

foreach ($zlist as $list_num => $zlist) {

    echo "<p><a href=$zlink[$list_num]>$zlist</a></p>\n";

}

echo "</body>";

?>


%, 2004

 
thanks
I was able to shorten the code and its working now - I guess I needed a push in a different direction to find another solution

Code:
<?php

$zlist = file('../src/list.txt');
$zlink = file('../src/link.txt');

echo "<body>";

foreach ($zlist as $list_num => $zlist) {

    echo "<p><a href=$zlink[$list_num]>$zlist</a></p>\n";

}

echo "</body>";

?>
 
I'm glad you solved the problem.
For safety and sanity I would recommend to always include the following checks when reading files:
1. Does the file exist (trigger nice error if not)
2. Was the file read successfully (might be empty for some reason...)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top