Re: [PHP] best way to save program prefs to a file?
In larger applications, I prefer to serialize the array because it allows you to store the data in _any_ variable, not just the pre-named variable. For example, if your page is already using a variable named $user_settings, you would run into problems with your solution. My solution allows you to name the data however you want, however many times you want. Of course, this is usually not necessary for a smaller application where you wouldn't run into such a problem. Sincerely, A.J. Brown BitNotion Technologies [EMAIL PROTECTED] - Original Message - From: "Edward Vermillion" <[EMAIL PROTECTED]> To: "A.J. Brown" <[EMAIL PROTECTED]> Cc: Sent: Tuesday, September 27, 2005 10:48 AM Subject: Re: [PHP] best way to save program prefs to a file? A.J. Brown wrote: Are you wanting the preferences to be real-time changeable? For example, user preferences that can be modified then saved? If so, just store them in an array, then serialize the array and save it to a file. Read the file at every page load. [code] //save the settings $user_settings['setting1'] = 'foo'; $user_settings['setting2'] = 'bar'; $fh = fopen('user_settings.dat'); $serialized = serialize($user_settings); fwrite ($fh, $serialized, strlen($serialized)); fclose($fh); //reload the settings $user_settings = unserialize(file_get_contents('user_settings.dat')); [/code] Hope this helps. I may be showing my ignorance here, but why bother to serialize the array? Why not just write it out to a php file, then all you have to do is include the file when you need it and it's ready to go? $setingsFile = " $val) { $settingsFile .= "$userSettings[$key] = $val\n"; } $settingsFile .= "\n?>"; $fh = fopen('/path/to/settingsFile.php', 'w'); fwrite($fh, $settingsFile); // with error handling of course... fclose($fh); Then in your script, include '/path/to/settingsFile.php'; and you're ready to use $userSettings and any changes get written back to the file. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to save program prefs to a file?
Cool...that's exactly what I was looking for!! "Torgny Bjers" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Chris wrote: >> I'd like to save some program preferences to a txt file where they can be >> recalled and updated at a later time. Basically this will be a variable >> name >> and a value. Can someone suggest a reference or method to best perform >> this >> task? >> >> Thanks >> Chris >> BTW: I do not want to use MySql. > > Write a regular Windows .ini file like: > > ; My INI-file (this is a comment line): > key = value > key2 = value2 > ; It even works with PHP constants (defines) in the ini file > key3 = E_ALL > > Reading it in is simple, use parse_ini_file(): > http://www.php.net/manual/en/function.parse-ini-file.php > > You can also keep sections in the .ini file such as: > > ; a section name uses [] > [section1] > key = value > [section2] > key = value > > To write this down is easy, just do a foreach on your associative array > and write to a string that you then save into a file if you wish to > update the values here. > > Warm Regards, > Torgny -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to save program prefs to a file?
On 9/27/05, Chris <[EMAIL PROTECTED]> wrote: > I'd like to save some program preferences to a txt file where they can be > recalled and updated at a later time. Basically this will be a variable name > and a value. Can someone suggest a reference or method to best perform this > task? While learning Rubyonrails recently I discovered Yaml. It's sorta like .ini files on steroids. There's a PHP implementation available here: http://whytheluckystiff.net/syck/ And the main site is here: http://yaml.org/ -- Greg Donald Zend Certified Engineer MySQL Core Certification http://destiney.com/
Re: [PHP] best way to save program prefs to a file?
A.J. Brown wrote: Are you wanting the preferences to be real-time changeable? For example, user preferences that can be modified then saved? If so, just store them in an array, then serialize the array and save it to a file. Read the file at every page load. [code] //save the settings $user_settings['setting1'] = 'foo'; $user_settings['setting2'] = 'bar'; $fh = fopen('user_settings.dat'); $serialized = serialize($user_settings); fwrite ($fh, $serialized, strlen($serialized)); fclose($fh); //reload the settings $user_settings = unserialize(file_get_contents('user_settings.dat')); [/code] Hope this helps. I may be showing my ignorance here, but why bother to serialize the array? Why not just write it out to a php file, then all you have to do is include the file when you need it and it's ready to go? $setingsFile = " $val) { $settingsFile .= "$userSettings[$key] = $val\n"; } $settingsFile .= "\n?>"; $fh = fopen('/path/to/settingsFile.php', 'w'); fwrite($fh, $settingsFile); // with error handling of course... fclose($fh); Then in your script, include '/path/to/settingsFile.php'; and you're ready to use $userSettings and any changes get written back to the file. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to save program prefs to a file?
Are you wanting the preferences to be real-time changeable? For example, user preferences that can be modified then saved? If so, just store them in an array, then serialize the array and save it to a file. Read the file at every page load. [code] //save the settings $user_settings['setting1'] = 'foo'; $user_settings['setting2'] = 'bar'; $fh = fopen('user_settings.dat'); $serialized = serialize($user_settings); fwrite ($fh, $serialized, strlen($serialized)); fclose($fh); //reload the settings $user_settings = unserialize(file_get_contents('user_settings.dat')); [/code] Hope this helps. -- Sincerely, A.J. Brown "Jay Blanchard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > [snip] > I'd like to save some program preferences to a txt file where they can be > recalled and updated at a later time. Basically this will be a variable > name > > and a value. Can someone suggest a reference or method to best perform > this > task? > [/snip] > > Open a new file, save stuff to it, close the file. > Include the file where you need the prefs. > > http://www.php.net/fopen > http://www.php.net/explode -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to save program prefs to a file?
Chris wrote: > I'd like to save some program preferences to a txt file where they can be > recalled and updated at a later time. Basically this will be a variable name > and a value. Can someone suggest a reference or method to best perform this > task? > > Thanks > Chris > BTW: I do not want to use MySql. Write a regular Windows .ini file like: ; My INI-file (this is a comment line): key = value key2 = value2 ; It even works with PHP constants (defines) in the ini file key3 = E_ALL Reading it in is simple, use parse_ini_file(): http://www.php.net/manual/en/function.parse-ini-file.php You can also keep sections in the .ini file such as: ; a section name uses [] [section1] key = value [section2] key = value To write this down is easy, just do a foreach on your associative array and write to a string that you then save into a file if you wish to update the values here. Warm Regards, Torgny -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] best way to save program prefs to a file?
[snip] I'd like to save some program preferences to a txt file where they can be recalled and updated at a later time. Basically this will be a variable name and a value. Can someone suggest a reference or method to best perform this task? [/snip] Open a new file, save stuff to it, close the file. Include the file where you need the prefs. http://www.php.net/fopen http://www.php.net/explode -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php