I have 3 classes: This is main DB class - if will be use in other two classes.
class db {
const DBHOST = 'Localhost';
const DBNAME = 'project';
const DBUSER = 'root';
const DBPASS = '';
public $db=array();
private $_dpt = array('fr'=>'','en'=>'_en');
public $db_pref = 'fr';
public $db_name;
public function __construct(){
}
public function connect_db(){
if(!isset($this->db[$this->db_pref])){
$this->db[$this->db_pref] = mysql_connect (self::DBHOST, self::DBUSER, self::DBPASS) or die (mysql_errno.' : Can`t connect to this DB');
mysql_select_db(self::DBNAME.$this->_dpt[$this->db_pref], $this->db[$this->db_pref]) or die (mysql_errno.' : Can`t select this DB');
mysql_query("SET NAMES utf8");
}
}
And this is my crud class:
require_once('db.php');
class crud extends db {
public function __construct(){
$this->lang = $_SESSION['user_lang_type']; // fr or en
parent::__construct();
if(isset($p['db']) && isset($p['db_pref'])){
// I put here old db conn. to new object for economy
$this->db_pref=$p['db_pref'];
$this->db=$p['db'];
if(isset($p['prevent']) && $p['prevent']!=$p['db_pref']){
// if isset db_prevent - I connect to the project_fr db only
$this->db_pref=$p['prevent'];
$this->connect_db();
}
} else $this->connect_db();
}
// reset prevented database
public function resetPreventDB($db=false){
$this->db_pref=$this->lang;
if($db) foreach($db as $k=>$v){ if(!isset($this->db[$k])) $this->db[$k]=$v; }
}
public function getData(){
// this is shorted version of my crud query
// here I use link to selected db $this->db[$this->db_pref]
return mysql_query('select * from advertice',$this->db[$this->db_pref]);
}
} And here are my third class - it load data from selected database using connection of CRUD class:
class Advertice extends crud {
private $route;
public $cont;
public function __construct(){
global $crud;
parent::__construct(array('db'=>$crud->db,'db_pref'=>$crud->db_pref,'prevent'=>'fr'));
$this->getList($p);
}
public function getList(){
$q = $this->getData();
global $crud;
// this didnt work - if db is FR it can be changed to EN
$crud->resetPreventDB($this->db);
}
}
include_once 'crud.php';
include_once 'advertice.php';
$crud = new crud();
$crud->set = array();
// For ex. now I am in project_en database
$crud->getData() - will return data from project_en
$crud->set['ads'] = new Advertice(); // get data from project_fr
$crud->getData() // will return data from project_fr - but I call $crud->resetPreventDB() in Advertice->getList().
I real didnt know why $crud->resetPreventDB() dont working - but I real wont this code, because this bug stop developing of all my startup. Will very Thanks for you understanding & help) Thanks.