[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Многократное подключение к БД
Страницы: 1, 2
Psychedelic
Привет всем! Я с ООП дело имею впервые.. а вопрос вот в чем, в общем я написал код, и в нем получилось очень много лишних подключений к БД. Вот примерно то что получилось.
<?php
header('Content-Type: text/html; charset=UTF-8;');
class Configuration{

const DB_HOST = 'localhost';
const DB_USER = 'root';
const DB_PASS = '';
const DB_NAME = 'test';

private $sql = Array();
private $res = Array();
private $arr = Array();
private $cnt = Array(); // count
private $cls = Array(); // css
private $obj = Array();
private $id = Array();
private $out;
private $con;

public function __construct(){
if(!isset($_GET['category']))
$_GET['category'] = null;
if(!isset($_GET['element']))
$_GET['element'] = null;
if(!isset($_GET['details']))
$_GET['details'] = null;
}

final protected function database_connect(){
$this -> con = new mysqli(self::DB_HOST, self::DB_USER, self::DB_PASS, self::DB_NAME);
if(!$this -> con)
return false;
$this -> con -> query("/*!40101 SET NAMES 'UTF8' */");
return $this -> con;
}

public function show_data($d){
return $d;
}
}

error_reporting(E_ALL);
$obj_Configuration = new Configuration();

// requires
require_once $path.'/inc/classes/class.Root.php';
require_once $path.'/inc/classes/class.Validation.php';
require_once $path.'/inc/classes/class.Menu.php';
require_once $path.'/inc/classes/class.Content.php';


$obj_Validation = new Validation();
$obj_Menu = new Menu($obj_Validation, $_GET['category'], $_GET['element']);
$obj_Content = new Content($obj_Validation, $_GET['category'], $_GET['element'], $_GET['details']);

$menu = $obj_Configuration -> show_data($obj_Menu -> build_menu());
// $content = $obj_Content -> show_content();
?>

<?php
class
Menu extends Configuration{

public function __construct($obj, $category_id, $element_id){
$this -> obj['Validation'] = $obj;
$this -> id['category'] = $this -> obj['Validation'] -> only_digits($category_id);
$this -> id['element'] = $this -> obj['Validation'] -> only_digits($element_id);
}

private function get_count_element_rows($element_id, $category_id){
$this -> sql['count_element_rows'] = "
SELECT `id`
FROM `kvartira`
WHERE `category_element_id`='"
.$element_id."'
AND `category_id`="
.$category_id."
"
;
$this -> res['count_element_rows'] = $this -> database_connect() -> query($this -> sql['count_element_rows']);
if(!$this -> res['count_element_rows']) return '?';
$this -> cnt['count_element_rows'] = $this -> res['count_element_rows'] -> num_rows;
return $this -> cnt['count_element_rows'];
}

public function build_menu(){
$this -> sql['categories'] = "
SELECT `id`, `title`
FROM `categories`
WHERE `hide`='0'
ORDER BY `order`
ASC
"
;
$this -> res['categories'] = $this -> database_connect() -> query($this -> sql['categories']);
if(!$this -> res['categories']) return false;
while($this -> arr['categories'] = $this -> res['categories'] -> fetch_array()){
$this -> cls = $this -> obj['Validation'] -> identical_values($this -> arr['categories']['id'], $this -> id['category'], true);
$this -> out .= '<li class="'.$this -> cls.' category"><a href="index.php?category='.$this -> arr['categories']['id'].'">'.$this -> arr['categories']['title'].'</a></li>';
$this -> sql['category_elements'] = "
SELECT *
FROM `category_elements`
WHERE `hide`='0'
AND `category_id`='"
.$this -> arr['categories']['id']."'
ORDER BY `order`
ASC
"
;
$this -> res['category_elements'] = $this -> database_connect() -> query($this -> sql['category_elements']);
if(!$this -> res['category_elements']) return false;
if($this -> id['category'] != $this -> arr['categories']['id'])
continue;
while($this -> arr['category_elements'] = $this -> res['category_elements'] -> fetch_array()){
$this -> cls = $this -> obj['Validation'] -> identical_values($this -> arr['category_elements']['id'], $this -> id['element'], true);
$this -> out .= '<li class="'.$this -> cls.' element"><a href="index.php?category='.$this -> arr['categories']['id'].'&element='.$this -> arr['category_elements']['id'].'">'.$this -> arr['category_elements']['title'].' ('.$this -> get_count_element_rows($this -> arr['category_elements']['id'], $this -> arr['category_elements']['category_id']).')</a></li>';
}
}

return $this -> out;
}
}

?>


Можно ли как то обойтись без лишних подключений к БД? То есть не использовать каждый раз функцию database_connect() из родительского класса Configuration в запросах класса Menu?
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.