SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrays in seperate files

    Im doing a simple staff management system for a site which has no mysql. I am storing all the details in an multi-dimensional array, in a seperate php file. I have been include it on each page I need it, but now I need to be able to change the information in it, and save it. Any ideas how to do this? (The answer is probably really simple but I havent dont any php for ages and Ive forgotten loads)
    Thanks in advance
    Andy
    Last edited by AndyXL; May 17, 2003 at 08:42.
    Andy: Always Morphin Dwarfs

  2. #2
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'd look at the serialize() and unserialize() functions.

    (You can make changes to the array in your app (new keys, unsetting others) and then serialize it into a string, store it in a file, and then on the next page load, you load the contents of the file and unserialize() it, and you have your exact array back in usable form.)

    Hope this helps.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting... Thanks!
    Andy: Always Morphin Dwarfs

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its not working. In main.php, I have this:
    PHP Code:
    $accounts file("accounts.dat");
    unserialize($accounts); 
    And in accounts.dat, I have this:
    Code:
    a:1:{s:5:"test1";a:7:{s:4:"name";s:10:"testingman";s:8:"password";s:12:"testpassword";s:7:"favgame";s:19:"testing: the tester";s:9:"worstgame";s:21:"testing: the tester 2";s:16:"currentlyplaying";s:21:"testing: the tester 3";s:7:"comment";s:47:"I hate testing! Kill testing! Death to testing!";s:3:"job";s:13:"Tester (duh!)";}}
    (created using serialize())
    However, in main.php, after unserializing the array, nothing appears when I echo some of the values. Do you know why?
    Thanks again
    Andy
    Andy: Always Morphin Dwarfs

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    file() returns the result in an array, if you are using PHP 4.3.0 or higher, use file_get_contents(), or you can implode('', file()), or something like the following:

    PHP Code:
    if (!function_exists('file_get_contents'))
    {
       function 
    file_get_contents($filename$int_include=NULL)
       {
          
    ob_start();
             
    readfile($filename$int_include);
             
    $return ob_get_contents();
          
    ob_end_clean();
          return 
    $return;
       }

    And then use file_get_contents() and it will work with previous versions of PHP as well.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much! Ive finished writing it now, everything works perfectly!!! Thankyou!!!!
    Andy: Always Morphin Dwarfs


Bookmarks

Posting Permissions

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