Aureole Posted November 29, 2007 Share Posted November 29, 2007 If you look at any forum software or a lot of other things there are always settings say for example on IPB or VBulletin you might have: Board Title: ______________ ...with a text area to store whatever you enter... now what I'm wondering is how do they do this? If they store it in a database then that means a query every time they need to fetch the board title... I really can't think how they do it, I mean I guess a query isn't so bad when you want to fetch a board title or something but what if there are a LOT of settings, do they still store them in a database? Do they pull them on every page load? Do they have some kind of settings cache? This is why I'm asking... I don't how to approach this.. I'm not too sure what is considered an acceptable amount of queries per page load... as far as not killing the server goes. Just wondering on the best way to approach things... I use constants mostly, problem being you can't have a nice pretty admin interface for people to edit them... they have to delve into a config file and muck around... Thanks a lot. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 29, 2007 Share Posted November 29, 2007 The form where you update the settings likely generates a config.php. At least, that's probably how I'd do it. Quote Link to comment Share on other sites More sharing options...
Aureole Posted November 29, 2007 Author Share Posted November 29, 2007 Hmm... sounds interesting... so it would create variables or constants, arrays (or whatever is required) then that would be included on every page? Do you know of any tutorials or at least something to get me started? I've been searching like crazy for the last Hour or so and I can' t find anything... I don't really have any experience with creating files/editing files and what-not. Thanks a lot. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 29, 2007 Share Posted November 29, 2007 Remember that PHP primarily outputs text. Just because we mostly use PHP to output [X]HTML doesn't mean there's any reason we can't use PHP to output other types of plain text, including a new PHP file. write_config.php <?php /** * write_config.php * This PHP file outputs a new PHP file that defines a CConfig object. class CConfig { var $property1 = 'a value'; var $property2 = 'another value'; var $property3 = true; // etc. } * In order to do this, the file expects a $properties array where * 'property_name' => 'config_value' */ // First we must detect that $properties is an array and contains values if(empty($properties)){ return; } // Begin our class echo 'class CConfig {' . "\n"; // Here we loop over each value and convert it into something we can place // into a class definition. For simplicity's sake, I'm only handling bool // properties, anything else will just be skipped. You can work on that as // an exercise of your own. foreach($properties as $prop => $val){ if(!is_bool($val)){ continue; // only handling bools } echo 'var $' . $prop . ' = ' . ($val ? 'true' : 'false') . ';' . "\n"; } // End our class echo '}' . "\n"; ?> sample script <?php /** * Here is a sample of how to exec our write_config.php */ $properties = Array(); $properties['require_login'] = true; $properties['write_logs'] = true; // Next property is a string, so will be skipped, you'll have to modify // write_config.php to make it work. $properties['board_name'] = 'Developer\'s Board'; // We can execute the file by include()'ing it, but that will cause it to // write output to the screen. So we will capture it in a buffer ob_start(); include('write_config.php'); // this writes to the buffer $file_contents = ob_get_contents(); // this gets buffer contents ob_end_clean(); // this ends and erases buffer // $file_contents now contains our class definition, we can write it to // a file // file_put_contents is PHP 5, use fopen, fwrite, and fclose if php4 file_put_contents('config.php', $file_contents); ?> That should help. Quote Link to comment Share on other sites More sharing options...
Aureole Posted November 30, 2007 Author Share Posted November 30, 2007 Hmm interesting, thanks a lot. I'll leave this unsolved for now just in case I come across any problems when playing around with this. Quote Link to comment Share on other sites More sharing options...
Aureole Posted November 30, 2007 Author Share Posted November 30, 2007 EDIT: What the heck is wrong with the code parser thing on this forum? ??? Well I modified both of the examples a tiny little bit and got it working for how I'd like to use it... writeconfig.php if(empty($properties)) { return; } echo("<?php\n"); foreach($properties as $prop => $val) { if(!is_bool($val)) { continue; } echo('define(\''.strtoupper($prop).'\', '.($val ? 1 : 0).');'."\n"); } echo('?>'); sample.php $properties = array(); $properties['require_login'] = true; $properties['write_logs'] = true; $properties['board_name'] = 'Developer\'s Board'; ob_start(); include('writeconfig.php'); $file_contents = ob_get_contents(); ob_end_clean(); $file = "config.swr3"; $des = fopen($file, 'w'); fwrite($des, $file_contents); The only thing that really changed is echo('define(\''.strtoupper($prop).'\', '.($val ? 1 : 0).');'."\n"); Anyway, it works great so far... now I'm going to try to figure out how to have it so it will work with booleans and strings... and make it so I can change the properties with a form. I'll keep you updated... =) Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 30, 2007 Share Posted November 30, 2007 You just change the property loop to include cases for: is_bool is_string is_int, is_float is_null etc. and print the appropriate thing for each one. The only reason you have to do this BTW, is because you want to make sure strings are escaped, null usually prints blank, etc. I noticed on mine I left out printing the <?php and ?> tags, nice catch. But on yours, did you forget a backslash in here: echo("<?phpn"); Quote Link to comment Share on other sites More sharing options...
Aureole Posted November 30, 2007 Author Share Posted November 30, 2007 Oh yeah, I didn't forget the backslash it's just the way the forum parses the content within code tags... it doesn't play well with me sometimes and it messes up my posts. I'm not sure exactly what causes it. Thanks a lot anyway, this has opened up a few doors for me... gave me some ideas etc. 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.