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 11 of 11

Thread: Reading File

  1. #1
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts

    Reading File

    Ok here's what I need to do. I need to read a file that looks like this:

    Code:
    g1.jpg::Description #1
    g2.jpg::Description #2
    I need each side of that "::" to be a different variable and I need it in some kind of while loop till the end of the file. I've tried a coupld of split and explode versions and can't get it going, basically because of that end of the line when it starts a new one. This file displays descriptions for pictures in another script, and it splits it by of course as you can see the file name then the "::". But since I'm not reading the file list I can't get that other splitter. I don't want to read the directory list because in case the file isn't there anymore I don't want that to conflict with reading the file out and displaying the contents. Basically what I want to do is have an input box for each one, and then at the end of the php file allow the user to click on save to save it back to the file if they edited anything.

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Explode by /n first then by ::

  • #3
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    1. spool the file into and array using file
    2. loop thru the array using foreach
    3. on each iteration run each value thru trim
    4. explode each value by "::"

  • #4
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Ok I got that working just fine, thanks for the help. Now I got another question for you. I have all of this put into an array that displays it all in their seperate little input box's for changing. I know how to add a new one to the end of the file, I just append it, not big problem. But how do I delete one of the lines entirely?? This is my script as it stands now:

    Code:
    $lines = file('description.txt');
    $i = 0;
    
    foreach ($lines as $line) {
       $trimmed = trim($line);
       $wordChunks = explode("::", $trimmed);
       $i++;
       print"Name of File: <input name=\"part1_$i\" type=\"text\" value=\"$wordChunks[0]\"><br>";
       $i++;
       print"Description for File: <input name=\"part1_$i\" type=\"text\" value=\"$wordChunks[1]\"><br><br>";
    
    }

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Post up how you are putting the file back together after editing it.

  • #6
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Haven't gotten that far yet in the scripts. Should I of??

  • #7
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well you said you can edit and are looking for a way to delete... You can use a checkbox to mark as delete when you update the edits. And then when you build your file back out with the changes, you can remove the ones you want deleted.

  • #8
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Ok thanks for the idea of the checkbox, works great. Now I've got a loop going and then the write info for the file. That all works fine, but I can't get a carriage return(new line) at the end of everyline, here's what I got:

    Code:
    $i2 = 1;
    while ($i2 <= $_POST['parts']) {
    if(!$_POST["delete_$i2"]){
    $totalstring = $totalstring.$_POST["part1_$i2"]."::".$_POST["part2_$i2"]."\n";
    }
    $i2++;
    }
    And this is what it's giving me:

    Code:
    g1.jpg::Description #1 g2.jpg::Description #2
    Single line.

  • #9
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    /r/n

    That should work for u

  • #10
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Awsome works perfectly now thanks for all the help.

  • #11
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    was wondering if there was room for flexibility in the entry of the data to the text file instead of appending new data to the end of the file, could you add it in order for example within the file

    e.g.

    g1.jpg:escription #1
    g2.jpg:escription #2
    g4.jpg:escription #4

    g3.jpg:escription #3

    so that they are in order?


  •  

    Posting Permissions

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