vulcandth Posted August 25, 2008 Share Posted August 25, 2008 I've scoured places and it seems that I must ask someone if they might know what i'm doing wrong. I am creating a Clan Roster Table using php and text documents. The php File would pull the information and build the table based off the contents of the text documents. When I stuck with one fgets() it worked.. However when I added the second and attempted to load the page, it wouldn't ever load. I am very new at coding so it could be something simple... or something big. If anyone would be gracious enough to take a peak at my code I would be greatly appreciated! Basically its supposed to first open the command.txt file to retrieve a list of every gamer text file. Which is ouputed in that order. I'll include a example of both files also. command.txt: vulcan01s.txt topdog01s.txt vulcan01s.txt: Vulcandeath Field Marshall CoD4, BF2, Runescape Unknown Join Date USA index.php: <!--Start Table--> <table width="100%" align="center" border="1" cellpadding="2" cellspacing="1" bordercolor="#0000FF" bgcolor="#000000" id="bcolor"> <tr> <td align="center" colspan="5" class="option"><font color="#66FF00">Active Members</font></td> </tr> <!--Define Columns--> <tr id="rcolor2"> <td id="descripcolor" width="16.6666666667%}%"><font color="#66FF00">GamerName</font></td> <td id="descripcolor" width="16.6666666667%}%"><font color="#66FF00">Rank</font></td> <td id="descripcolor" width="16.6666666667%}%"><font color="#66FF00">Game(s)</font></td> <td id="descripcolor" width="16.6666666667%}%"><font color="#66FF00">Join Date</font></td> <td id="descripcolor" width="16.6666666667%}%"><font color="#66FF00">Country</font></td> </tr> <?php $commandFile = "command.txt"; // Load Main Gamer File List EX: vulcan01s.txt $commandhandle = fopen($commandFile, 'r'); while (!feof($commandhandle)) // Loop Until out of Gamers { // Loop Command Start echo '<tr id="rcolor">'; // Table Row Begin $gamerFile = fgets($commandhandle, 512); // Pull Gamer File $gamerhandle = fopen($gamerFile, 'r'); while (!feof($gamerhandle)) // Loop Until End of Row { // Loop Gamer Start $data = fgets($gamerhandle, 512); echo '<td width="16.6666666667%}%" align="center">'; // Block Begin echo '<font color="#66FF00">'; echo $data; // Line Contents Spew echo '</font>'; echo '</td>'; // Block End } // Loop Gamer End echo '</tr>'; // Table Row End fclose($gamerhandle); // Close Game File } // Loop Command End fclose($commandhandle); // Close Command File ?> <!--End Table--> </table> Link to comment https://forums.phpfreaks.com/topic/121210-multiple-fgets/ Share on other sites More sharing options...
xsist10 Posted August 25, 2008 Share Posted August 25, 2008 Lets simplify your code and add some error checking. <?php $commandFile = "command.txt"; // Load Main Gamer File List EX: vulcan01s.txt // Check if we can read the file if (is_readable($commandFile)) { $gamerFiles = file($commandFile); foreach ($gamerFiles as $gamerFile) { if (is_readable($gamerFile)) { echo '<tr id="rcolor">'; // Table Row Begin $gamerDetails = file($gamerFile); foreach ($gamerDetails as $detail) { echo '<td width="16.6666666667%}%" align="center">'; // Block Begin echo '<font color="#66FF00">'; echo $detail; // Line Contents Spew echo '</font>'; echo '</td>'; // Block End } // Loop Gamer End echo '</tr>'; // Table Row End } else { echo 'Could not read '. $gamerFile .'<br/>'; } } // Loop Command End } else { echo 'Could not read command.txt<br/>'; } ?> Link to comment https://forums.phpfreaks.com/topic/121210-multiple-fgets/#findComment-624832 Share on other sites More sharing options...
nrg_alpha Posted August 25, 2008 Share Posted August 25, 2008 What xsist10's code here does essentially is quite simple.. by using $gamerDetails = file($gamerFile); this is creates the $gamerDetails array and simply loops through using foreach()... What I tend to do with files like this is exactly as mentioned.. then I go through the array keys and make any adjustments to their values..(you can add "\r\n" at the end of each value), then implode the array back into a string when you're done. To finish it off, use file_put_contents() to access, write to, save and close file in one fell swoop (requires PHP 5.0.0 or higher).. just be warned that this function overwrites the entire file.. so if you go this route, make sure your array keys have their values set properly before calling in this last function. Link to comment https://forums.phpfreaks.com/topic/121210-multiple-fgets/#findComment-624851 Share on other sites More sharing options...
vulcandth Posted August 25, 2008 Author Share Posted August 25, 2008 Thanks a bunch! The Check allowed me to determine the problem. Basically every time it returns it adds an unwanted white space at the end when reading the values of the command list file. I fixed this by using the rtrim() function. I appreciated your help! Link to comment https://forums.phpfreaks.com/topic/121210-multiple-fgets/#findComment-625124 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.