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

    Join Date
    Jul 2000
    Posts
    9
    Rep Power
    0
    A new array problem for me

    I have a file 'profiles.txt' which contains the info on my members, this contains an array:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $privateers = array(
    "tom" => "tom's stuff",
    "jim" => "jim's stuff",
    "andy" => "andy's stuff"
    );
    [/code]

    What I want to do is sort the array alphabetically by the keys. I know how to do this using ksort() but I need to know how to output the revised $privateers array to profiles.txt and overwrite the original array and only that array.

    Also how can I cann I add to the array from a form? I have a form worked out and I know how to output that to a file but it completely overwrites the entire file or adds on to the very end.
    How do I add it to the end of the array?

    Thanks again for your time and help (if any)
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,646
    Rep Power
    4492
    this doesn't really answer your question, sorry, but it looks like using a database would be a whole lot easier for what you are trying to accomplish.

    mysql is free, easy to learn, and works excellently with php. www.mysql.com

    as far as your question...on the page that processes your form, at the beginning read in your file, create your array, then add the form information to the end of the array. you can use count($array) to see how many elements are in the array, then add values with array[$count+1]="john". then save the new array to the file again, overwriting whatever was in there.


    databases are much easier..

    ---John Holmes
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    9
    Rep Power
    0
    Unfortunately my host hasn't got MySQL support setup yet, so I can't use databases for now. I will be looking into it though.

    I don't suppose you'd be able to supply some more sample code for the array[$count+1]="john" thing, as I tried to get it working and couldn't. All I get outputted is the number of values in the array + 1.
    It may be something to do with the code I'm using-
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $count = count($privateers);

    $fp = fopen("profiles.txt", "w+");
    fwrite($fp, "$privateers[$count+1]=$info");
    fclose($fp);
    [/code]

    BTW, the profiles.txt contains other arrays, will that just overwrite the $privateers array or all of them?
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,646
    Rep Power
    4492
    Definetly talk your hosting company into installing mysql. it doesn't cost them anything so it should be an easy argument.

    is there any way you can have each array stored in a different file? that would make things easier. if each array was in a different file, you could open that file, and then just append the new array information onto the end of the file. the next time to load the array from the file, it would include the new information.

    ---john holmes...

Similar Threads

  1. Adding eval()'d content to a var and NOT outputting it
    By ShepMode in forum PHP Development
    Replies: 2
    Last Post: November 28th, 2003, 04:10 PM

IMN logo majestic logo threadwatch logo seochat tools logo