#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    9
    Rep Power
    0
    I have a file with content like:
    John|10|5|7
    Alec|22|9|31
    Mike|23|42|1

    And I can retrieve all the values using explode and a loop. But for what I'm wanting to do is be able to get the values for one particular line rather than the entire.

    So how can I retrieve the values for one particular line?

    Thanks in advance, Dave.
  2. #2
  3. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    28
    Rep Power
    0
    You're already loading all of the lines into an array aren't you? Just use the index of the line you want...
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Aurora, CO USA
    Posts
    4
    Rep Power
    0
    use the function fgetcsv(), it is able to read information from a comma delimmited file, or I have used it to read information such as what you have with the "|" seperating it. This function extracts information into an array, each field is put into a seperate array, and you can specify the delimmiter. Look in the PHP manual to see exactly how to use it. Remember this function only pulls one line at a time, so you will have to use some kind of while(), or for() loop, to get all of the information from the file.
    manual: http://www.zend.com/manual/

    [This message has been edited by mikeyt_333 (edited October 17, 2000).]
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    9
    Rep Power
    0
    Right, I tried using the fgetcsv()example:

    $row = 1;
    $fp = fopen ("test.csv","r");
    while ($data = fgetcsv ($fp, 1000, ",")) {
    $num = count ($data);
    print "<p> $num fields in line $row: <br>";
    $row++;
    for ($c=0; $c<$num; $c++) {
    print $data[$c] . "<br>";
    }
    }
    fclose ($fp);

    With a test file with some of the content that shall be going in it.

    And I got the result:

    11 fields in line 1:
    Dave Bett
    Reaper
    Hellfire
    Fury
    Elite
    Outlaw
    Outlaw
    Retreat hell! We're just attacking in another direction!
    See the History section
    reaper@vebein.co.uk


    11 fields in line 2:
    John Naughton
    Zombie
    Hellfire
    Trident III
    Elite
    Outlaw
    Outlaw
    This is the last thing you'll hear.
    See the History section
    zombie@vebein.co.uk

    Which is fine if I was wanting to have the entire thing outputted at once, which I don't.

    I'm planning on using switch() to display just one profile at a time. Like with a link leading to ?callsign=Reaper

    But I have no idea how to do this using the loops
    It's times like this that makes me wish I had taken that programming course I could've, then maybe I'd have more of a clue how to do this.
  8. #5
  9. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    Hi
    This should do da trick

    $name = "Reaper";
    $filename = "yourfile.txt";


    $hits = file($filename); //* $hits includes the whole text

    $totaal = count($hits);

    for($i=0 ; $i < $totaal +1 ; $i++)
    {

    $line = $hits[$i]; //* $line includes one line
    $instukjes = split("|" , $line); //* $instukjes[0] include the first string
    //* untill ":" is reached


    if($instukjes[0] == $name)
    {
    echo "instukjes[0]";
    echo "instukjes[1]";
    echo "instukjes[2]";
    //This wil show the line with the value Reaper!
    }
    }

    Note: $name could be assigne using a name in the url, like : http://yourdomain.com/index.phtml?name=Reaper

    hope this solves your question

    laterz

    ------------------
    Quote the Dokus:"NeverMore!"

Similar Threads

  1. Pointer problem in opening a file
    By LAKI in forum C Programming
    Replies: 13
    Last Post: February 11th, 2004, 08:27 AM
  2. Pointer problems opening file *formatted*
    By LAKI in forum C Programming
    Replies: 4
    Last Post: February 11th, 2004, 06:06 AM
  3. PHP file upload script trouble
    By casbboy in forum PHP Development
    Replies: 9
    Last Post: February 5th, 2004, 07:42 PM
  4. .SQL file :: Uploading values
    By RuFFy in forum MySQL Help
    Replies: 1
    Last Post: January 28th, 2004, 08:26 AM
  5. C++ programming help.
    By Programming1 in forum C Programming
    Replies: 1
    Last Post: January 20th, 2004, 10:47 AM

IMN logo majestic logo threadwatch logo seochat tools logo