Multi grids sql settings handling

Talk about programming tricks and examples

Moderator: alexandrleonenko

lambro
Master
Master
Posts: 102
Joined: Sep 13th, '10, 14:31
Location: Metz, France

Multi grids sql settings handling

Postby lambro » May 14th, '11, 08:11

Hi !
Sometimes you can have several datagrids for a single site. What happens when you change sql settings (for testings or backup reasons)? You end up copy/pasting your settings in all of them.
A good way to make it waaayyyyyyyyyy easier is to use this code at the beginning of your dg file :

Code: Select all

$Arrval = dgtablepointer();
$DB_USER=$Arrval[0];
$DB_PASS=$Arrval[1];
$DB_HOST=$Arrval[2];
$DB_NAME=$Arrval[3];

instead of

Code: Select all

   //  $DB_USER='name';            /* usually like this: prefix_name             */
    //  $DB_PASS='';                /* must be already encrypted (recommended)    */
    //  $DB_HOST='localhost';       /* usually localhost                          */
    //  $DB_NAME='dbName';          /* usually like this: prefix_dbName           */

The function reads as such and will simply do the copy/paste for you in all the grids. You can define it in afunction.inc.php file and "require_once"(it) at the beginning of each of your dg file, preferably before creating the dg object.
function :

Code: Select all

function dgtablepointer(){
$DB_USER='__USER__';
$DB_PASS='__PASS__';
$DB_HOST='__HOST__';
$DB_NAME='__DB_NAME__';
$Arrval = array ($DB_USER,$DB_PASS,$DB_HOST,$DB_NAME);
return($Arrval);
}

Hope it helps! @+

Return to “Programming Tricks”