Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    156
    Thanks
    47
    Thanked 0 Times in 0 Posts

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

    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

    PHP Code:
    $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 != && $i != && $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); 
    Last edited by Daniellez; 03-26-2009 at 09:06 PM.

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,293
    Thanks
    4
    Thanked 202 Times in 199 Posts
    Add a single call to fgets outside of your while loop.

    PHP Code:
    $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 != && $i != && $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); 
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    Daniellez (03-26-2009)

  • #3
    Regular Coder
    Join Date
    Jan 2009
    Posts
    156
    Thanks
    47
    Thanked 0 Times in 0 Posts
    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. Can it be explained further?

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,293
    Thanks
    4
    Thanked 202 Times in 199 Posts
    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.
    Dave .... HostMonster for all of your hosting needs

  • #5
    Regular Coder
    Join Date
    Jan 2009
    Posts
    156
    Thanks
    47
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,293
    Thanks
    4
    Thanked 202 Times in 199 Posts
    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
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    Daniellez (03-27-2009)

  • #7
    Regular Coder
    Join Date
    Jan 2009
    Posts
    156
    Thanks
    47
    Thanked 0 Times in 0 Posts
    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

  • #8
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,293
    Thanks
    4
    Thanked 202 Times in 199 Posts
    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.
    Dave .... HostMonster for all of your hosting needs


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •