How to: Load My Configuration Data

What is the best way to load your web application configuration? All web applications today have at least a single configuration file or multiple ones. What is the best way to load your configuration file? from database, from an XML file, from INI file or from SESSIONS??? To find out which method is the best I decided to run a few tests for each method and to see the time that a method required to be executed and how much memory it needed.

My conclusions where that as many times as I run the tests saving configuration data in sessions is the fastest way with a slightly difference in memory usage which increases a bit compares to loading from the INI file. Loading the configuration settings of your application in sessions or INI file saves resources and improves performance. I thing that a good practice is to have configuration in INI file and when ever the file updates to load the configuration for each visitor in its sessions instead of reading from INI. But at what cost?

Below you can see the results generated on the fly.

Write & Read UTF-8 from Mysql

The following php script shows how we can write and read Greek characters in a mysql database. Before starting check the encoding of your file. If you are using Zend studio go to Edit->Set Encoding and select Other: utf-8. Now in your php script after establishing a connection to the database type the following queries in order to set the names and characters equal to utf-8 for the specified connection, for me is just one connection so I do not use the resource as a parameter.

mysql_query(“SET NAMES ‘utf8′”);
mysql_query(“SET CHARACTER SET ‘utf8′”);

Next you can type you insert query:

$query = sprintf(‘INSERT INTO test (text) VALUES (“%s”)’, ‘write greek text here’);

And finally you can start reading from your database:

$q = mysql_query(“select text from test order by text”");
$row = mysql_fetch_object($q);

echo $row->text;
$row = mysql_fetch_object($q);

This small piece of information can save you a lot of time when trying to handle utf-8 characters. Good luck.

Clean-Up your code!

Clean code means readable code by anyone, no duplications, faster performance and the best thing of all is you can edit that code easier.

JAVASCRIPT : a very good website that can tide up your code especially if is a big mess and you cannot read or edit the code use: – Javascript Formatter. Just copy and paste your javascript code inside the textarea and press clean.

CSS : cascading style sheets. No more having the same class more than once or classes that do the same thing. By using the
W3C CSS Validation Tool 1st it will validate your code and 2nd generate for you a clean version
of your css code nice and tide.

If you would like a more sophisticate source code formatter I believe PrettyPrinter is the best I used so far. Visit Site. The reason is that this site enables the user to customize the code format according to its needs. The following parameters can be applied:

  • Add new lines after “{” and before “}”
  • Add new lines before “{“
  • Remove empty lines.
  • Add comment lines before function.
  • Add new lines after “;”
  • Add new lines after “}” (For .css) (Thanks David)
  • Remove new lines (useful only if you add them again with the other functions above)
  • Add new lines after “;” but not in for loops (Don’t check “Remove new lines” but do check “Remove empty lines”)(Thanks Chris) (Experimental, uses a heuristic that might fail )
  • Add new lines after “;” but not in for loops and skip quotes (Don’t check “Remove new lines” but do check “Remove empty lines”) (Thanks Chris) (Experimental, uses a heuristic that might fail )
  • Reduce whitespace
  • Put the code again in the input box above after submit.
  • Skip HTML, give me plain code! (Use Save-As function of your browser)

PN Generator (Pagination)

This class can be used to generate links to browse MySQL database query results split in multiple pages.

It takes as parameters an SQL query and a limit number of results to display per page.

The class executes the query to obtain the total number of results and generates HTML links to browse to the next, previous and other pages over which the query results will be displayed.

Inside the zip file there is a file called example1.php which you need to configure first by setting up your database connection strings, your query and how many results to display in each page.

  • You can download the class file here.

PHP: debugTool

DebugTool v1.0 is a new project that will help php developers to use it on the fly to check if their website is working right. Using this tool it will save them time from trying to figure out why something is not working, are my variables set, clean session and request variables on the fly without closing your browser every time.

Snapshot of debugTool:

Snapshot of debugTool with comments:

  • A Demo for this project can be found here.
  • Download project debugTool v1.0 here.