Welcome Guest, Not a member yet? Register   Sign In
config database.php file
#1

[eluser]cinewbie81[/eluser]
Hi guys,

IN CI, we need to pre-define database setting in database config file before we can actually use that database.

It's rather troublesome especially for those users without the programming background. Thus i was thinking to create a GUI and let the user to type in the database name. From user input, I will then append the content to the database config file automatically.

Here's how the database config file looks like:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$active_group = "default";
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "mysql";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['active_r'] = TRUE;
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";

$db['dell']['hostname'] = "localhost";
$db['dell']['username'] = "root";
$db['dell']['password'] = "";
$db['dell']['database'] = "dell_db";
$db['dell']['dbdriver'] = "mysql";
$db['dell']['dbprefix'] = "";
$db['dell']['active_r'] = TRUE;
$db['dell']['pconnect'] = FALSE;
$db['dell']['db_debug'] = TRUE;
$db['dell']['cache_on'] = FALSE;
$db['dell']['cachedir'] = "";

// New database create by user will append here. For eg:
// $db['new']['hostname'] = "localhost";
// $db['new']['username'] = "root";
// $db['new']['password'] = "";
// $db['new']['database'] = "new_db";
// $db['new']['dbdriver'] = "mysql";
// $db['new']['dbprefix'] = "";
// $db['new']['active_r'] = TRUE;
// $db['new']['pconnect'] = FALSE;
// $db['new']['db_debug'] = TRUE;
// $db['new']['cache_on'] = FALSE;
// $db['new']['cachedir'] = "";
?>


My problem now is how can I append the new content just right before the php closing tag '?>'. Any idea ?
#2

[eluser]Chris Newton[/eluser]
You could write this info to a database table.

or

You could write it to a text file and include it.

or

You could open this file, and overwrite it with the new data.
#3

[eluser]cinewbie81[/eluser]
Hi, i choose to open this file, and overwrite it with the new data.

I found this on some other site:

Code:
$f="myfile.txt";
$content = file_get_contents($f);
file_put_contents($f,"Add This line\n" . substr($content,0,strrpos($content,"\n")));

this fill append the new data into the 1st line and remove the last line of the file..

In my case i need to append the new data into the 2nd last line of the file (just right before php closing tag '?>' ... how can i do it ?
#4

[eluser]ejangi[/eluser]
Instead of trying to add it before the current ?>, why not just add a new PHP block:
Code:
$f="myfile.txt";
$content = file_get_contents($f);
file_put_contents($f, $content."<?php /* PUT NEW SETTINGS HERE... */ ?>");
#5

[eluser]Grahack[/eluser]
I think that you can forget this closing tag, it's even better without it: see Zend coding standards at B.2.1.




Theme © iAndrew 2016 - Forum software by © MyBB