...

View Full Version : Please help modifying fgets so that it skips the 1st entry in a text file



Daniellez
03-26-2009, 08:45 PM
How would I modify the code below so that the first entry in the user_file is skipped so that it begins with the second?

Here's the first two in the user_file:

skipthisentry|21232f297a57a5a743894a0e4a801fc3|1|1|1|1|1|1|1|1|data
beginwiththis|83f1535f99ab0bf4e9d02dfd85d3e3f7|1|0|0|0|0|0|0|0|test


$fd = fopen($user_file, "r");
while (!feof($fd)) {
$userdata = explode("|", fgets($fd,4096));
print "<tr>";
for ($i=0; $i<count($userdata); $i++) {
if ($i == 0)
print "<td><a href='".$_SERVER['PHP_SELF']."?action=admin&amp;todo=editUser&amp;user_name=$userdata[0]'>$userdata[$i]</a></td>";
else if ($i != 1 && $i != 0 && $i != 10) {
if ($userdata[$i] == "1") print "<td><img src='gfx/enabled.gif' width='15' height='15' border='0'></td>";
else if ($userdata[$i] == "0") print "<td><img src='gfx/disabled.gif' width='15' height='15' border='0'></td>";
}
else if ($i == 10) print "<td>$userdata[$i]</td>";
}
print "<td><a href='".$_SERVER['PHP_SELF']."?action=admin&amp;todo=deleteUser&amp;user_name=$userdata[0]'>Remove</a></td></tr>";
}
fclose ($fd);

djm0219
03-26-2009, 10:05 PM
Add a single call to fgets outside of your while loop.



$fd = fopen($user_file, "r");
$trash = fgets($fd,4096); // We don't need the first line
while (!feof($fd)) {
$userdata = explode("|", fgets($fd,4096));
print "<tr>";
for ($i=0; $i<count($userdata); $i++) {
if ($i == 0)
print "<td><a href='".$_SERVER['PHP_SELF']."?action=admin&amp;todo=editUser&amp;user_name=$userdata[0]'>$userdata[$i]</a></td>";
else if ($i != 1 && $i != 0 && $i != 10) {
if ($userdata[$i] == "1") print "<td><img src='gfx/enabled.gif' width='15' height='15' border='0'></td>";
else if ($userdata[$i] == "0") print "<td><img src='gfx/disabled.gif' width='15' height='15' border='0'></td>";
}
else if ($i == 10) print "<td>$userdata[$i]</td>";
}
print "<td><a href='".$_SERVER['PHP_SELF']."?action=admin&amp;todo=deleteUser&amp;user_name=$userdata[0]'>Remove</a></td></tr>";
}
fclose ($fd);

Daniellez
03-26-2009, 10:23 PM
Since the first td determines determines the data for the subsequent tds in each row (which is each line in the text file), I don't understand how a call outside of the while loop would fix it. :confused: Can it be explained further?

djm0219
03-26-2009, 10:34 PM
Your example shows 2 lines of data of some sort and you stated that you wanted to skip the first line. Doing a get outside of the while loop effectively throws the first line in the trash and the logic in your while loop will never see it.

If that isn't what you meant by skip the first line please clarify what you meant and/or what you're trying to do.

Daniellez
03-26-2009, 10:42 PM
That's exactly what I'd like but I need to clarify..it wasn't my intention to construe that your method isn't feasible, just that I have no idea how to implement.

djm0219
03-26-2009, 10:52 PM
I modified the code you posted and added the necessary line with a comment.

$trash = fgets($fd,4096); // We don't need the first line

Daniellez
03-27-2009, 02:22 AM
Your solution worked, thank you! If you have some time, could you tell me how this worked? I'm trying to learn this stuff but there's too much designer in me :)

djm0219
03-27-2009, 12:01 PM
Simple, it grabbed the first line of the your file using fgets but didn't do anything with it. Rather than trying to do checking etc within the while loop it was easier to just grab the line before starting into the while loop. Get the line, trash it, and proceed on with what you wanted to do.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum