Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL
Author Message
Guardian2003
Site Admin



Joined: Aug 28, 2003
Posts: 6799
Location: Ha Noi, Viet Nam

PostPosted: Sat Jun 15, 2013 8:32 am Reply with quote

I have a simple table with the following schema
Code:


`ca_modules_config` (
  `module_name` varchar(30) NOT NULL DEFAULT '',
  `config_name` varchar(255) NOT NULL DEFAULT '',
  `config_value` longtext,
  UNIQUE KEY `config_name` (`config_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


Basically this will hold key/value pairs for a given modules configuration so if there were say 3 configuration options the raw inserted data would be something like this;

Code:


'FAQ', 'show_per_page', '10'
'FAQ', 'show_category_limit', '5'
'FAQ', 'show_only_recent', '1'


I want to retrieve this data and return it as an object but couldnt get mysqli fetch_object() to work so came up with this

Code:


function getModuleConfigs($module_name) {
   global $db, $m_setting, $module_name;
   $configresult = $db->sql_query('SELECT config_name, config_value FROM ca_modules_config WHERE module_name = \''.$module_name.'\'');
   while (list($config_name, $config_value) = $db->sql_fetchrow($configresult)) {
      $m_setting[$config_name] = $config_value;
   }
   $m_setting = (object)$m_setting;
   return $m_setting;
}


Given the above code sample , I should be able to do
Code:
echo $m_setting->show_category_limit;

Which should display the correct value of "5". My problem is, sometimes it works fine and other times it gives me an error

Quote:
Cannot use object of type stdClass as array


Anyone have any suggestions?
 
View user's profile Send private message Send e-mail
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Sat Jun 15, 2013 9:15 am Reply with quote

Be careful G. You have globalized $m_setting within the function and you are also returning it as a part of the function return. Since I am not seeing your call of getModuleConfigs(), I am not sure of the issue, but that just caught my eye.

Another thing is that if you really want/need to be using an object as apposed to an array, why not create an appropriate object class and physically create the object yourself? Wink

OffTopic
To be honest, I've wanted to create for RN for years a standard API for a configuration table, usable by the system and any module and also a special caching API that could be used to cache anything, including objects (serialized?)... Just haven't gotten around to proposing anything yet. But the thinking was to go ahead and store the configuration data in the table, which would mean multiple row fetching (many calls upon return) and let it be maintained there, but then once it is saved, also save a more "static" configuration object in the cache that a module can pull in ONE call... if its not in the cache, then it needs to go get it from the configuration table and it can get cached then... You can see how a simple Configuration class could be used for this on top of a standard Cache mechanism....

_________________
Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! 
View user's profile Send private message Visit poster's website
Guardian2003







PostPosted: Sat Jun 15, 2013 3:41 pm Reply with quote

Good points, thank you.
After reading your reply, I decided to go ahead and write a config Class. I'm still working out the detail to see if there is anything I need to refine but essentially (working code);
Code:


$mc = new Config($module_name);   
$mc->config_get_all(); // returns an array of all key/value pairs
$mc->getConfig('config_key', $default); // returns the value for the config_key for the current module

The second parameter, $default, will return the value assigned to it if the key didnt exist or had no value (not sure if I really need it but....).
Already done the functions for creating/deleting the table and also updating the value of a key.
For faster development I've also added a function to add new key/value pairs dynamically which can be disabled for production environments.

I made a cache class a while ago, primarily for storing the results of DB queries that should fit the bill for having a more static data set - I will need to dust it off and go through the code again though because it's been a while.
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©