julia k Posted December 30, 2008 Share Posted December 30, 2008 Hi there! My first post here (the reason I joined your forum!) hehe My problem is that I have set my application's settings in an ini file. I can get and write new values in it without problems only that I cannot update any existing settings anyone can help me with this? please? thank you in advance! Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted December 30, 2008 Share Posted December 30, 2008 Pretty girls don't write code Just kidding... you would probably need to post the file contents, and the script that adds to it so we can get some sort of an idea as to how to accomplish what you're asking. Quote Link to comment Share on other sites More sharing options...
julia k Posted December 30, 2008 Author Share Posted December 30, 2008 Hi Matthew! haha! well...some do :)here is the function that I use to write new values in the file: function write_ini($sSection='app_settings', $sKey, $sValue){ global $settings_file; $sFileHandle = fopen($settings_file, "r"); if (!$sFileHandle) { die("<b>Error:</b> Unable to open ".$settings_file."."); } if (filesize($settings_file) > 0) { $sFileContents = fread($sFileHandle, filesize($settings_file)); } if ($sFileContents === FALSE) { die("<b>Error:</b> Unable to read data from ".$settings_file."."); } $sFileData = preg_split("/ ? /", $sFileContents); $sectionStart = -1; $sectionEnd = -1; $returnLine = -1; for ($i = 0; $i < count($sFileData); $i++) { $x = strpos($sFileData[$i], "[".$sSection."]"); $y = strpos($sFileData[$i], "["); if ($y == 0 && $y !== FALSE && $sectionStart >= 0 && $sectionEnd == -1) { $sectionEnd = ($i - 1); } $z = explode("=", $sFileData[$i]); if (count($z) >= 2) { if ($z[0] == $sKey && $sectionStart >= 0 && $sectionEnd == -1 && substr($sFileData[$i], 0, 1) != ";") { $returnLine = $i; } } if ($x == 0 && $x !== FALSE && $sectionStart == -1) { $sectionStart = ($i + 1); } } if ($sectionEnd == -1) { $sectionEnd = (count($sFileData)-1); } $newfile = ""; if ($returnLine > -1) { $sFileData[$returnLine] = $sKey."=".$sValue; for ($i = 0; $i < count($sFileData); $i++) { $newfile .= $sFileData[$i]." "; } } else { if ($sectionStart > -1) { for ($i = 0; $i < count($sFileData); $i++) { if ($i == $sectionEnd) { $newfile .= $sKey."=".$sValue." "; } $newfile .= $sFileData[$i]." "; } } else { for ($i = 0; $i < count($sFileData); $i++) { $newfile .= $sFileData[$i]." "; } $newfile .= "[".$sSection."] ".$sKey."=".$sValue." "; } } fclose($sFileHandle); $sFileHandle = fopen($settings_file, "w"); if (!$sFileHandle) { die("<b>Error:</b> Unable to open file ".$settings_file." with write access."); } $sFileWrite = fwrite($sFileHandle, trim($newfile)); if ($sFileWrite === FALSE) { die("<b>Error:</b> Unable to write data to file ".$settings_file."."); }} to get the values, I use the $ini = @parse_ini_file($settings_file/*[,true]*/); where $ini would be an array, you know...For the ini file I use the same format as the one used in the php's ini file... whoa! this editor is pretty messy Quote Link to comment Share on other sites More sharing options...
julia k Posted December 30, 2008 Author Share Posted December 30, 2008 The point of all this is that I want to move all the content of my settings.php file into the ini file, so I'll need to update some keys when I change them from the admin section and I don't want to use a table just to have a single row in it. Quote Link to comment Share on other sites More sharing options...
aximbigfan Posted December 31, 2008 Share Posted December 31, 2008 Why not make the app read a serialized array stored in a file? Then to update it all you would simply unserialize it, change the keys you need, then serialize it, and put it back into the file. Chris Quote Link to comment Share on other sites More sharing options...
julia k Posted December 31, 2008 Author Share Posted December 31, 2008 hmmm, never thought of that... [i thought the ini file would be a good option.], but I will do nowthanks [/] Quote Link to comment Share on other sites More sharing options...
julia k Posted December 31, 2008 Author Share Posted December 31, 2008 To be honest, I never used the serialize and unserialize functions 'til now. I've ran a simple test and, yes, this would be my chosen way thank you very much, aximbigfan. see you guys around! [/] Quote Link to comment Share on other sites More sharing options...
aximbigfan Posted January 2, 2009 Share Posted January 2, 2009 To be honest, I never used the serialize and unserialize functions 'til now. I've ran a simple test and, yes, this would be my chosen way thank you very much, aximbigfan. see you guys around! [/] Sure! This is the best way I have found. It also allows me to mod a bunch of keys at once, plus, 3d arrays! Chris Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.