У меня есть контроллер, отображающий рекламу по категориям и подкатегориям ...
public function cat( $cat_alias, $subcat_alias = null)
{
$where = array( 'catalogue' => $cat_alias );
$ad_category = $this->Site_model->get_single( 'categories' , $where );
$children = $this->Site_model->get_childrens($ad_category->id);
foreach ($children as $child)
{
$childs[] = $child->id;
}
if ( !empty( $ad_category ) )
{
if (!empty($subcat_alias))
{
$where = array( 'catalogue' => $subcat_alias );
$ad_subcategory = $this->Site_model->get_single( 'categories' , $where );
$where = array('category_id' => $ad_subcategory->id);
}
else
{
$where = array( 'category_id' => $childs );
}
$ads = $this->Site_model->get( 'ads' , $where );
if ( !empty( $ads ) )
{
foreach ( $ads as $ad )
{
$ad_ids[] = $ad->id;
}
$where = array( 'id' => $ad_ids );
$data['ads'] = $this->Site_model->get( 'ads' , $where );
$data['categories'] = $this->Site_model->get_categories();
$this->load->view( 'cat' , $data );
}
else
{
$this->session->set_flashdata( 'alert' , 'There are no ads in this category.' );
redirect( '/' );
}
}
else
{
$this->session->set_flashdata( 'alert' , 'Invalid category name.' );
redirect( '/' );
}
}
И у меня есть функции в модели, которые отображают дерево категорий
public function get_categories()
{
$this->db->where('parent_id', 0);
$this->db->order_by('on_homepage', 'asc');
$query = $this->db->get('categories');
$return = array();
foreach ($query->result() as $category)
{
$return[$category->id] = $category;
$return[$category->id]->subs = $this->get_sub_categories($category->id); // Get the categories sub categories
}
return $return;
}
public function get_sub_categories($category_id)
{
$this->db->where('parent_id', $category_id);
$this->db->order_by('on_homepage', 'asc');
$this->db->limit(5);
$query = $this->db->get('categories');
return $query->result();
}
И в представленииЯ отображаю дерево категорий
<?php foreach ($categories as $category): ?>
<?php echo $category->name; ?>
<?php
if(!empty($category->subs)) {
echo '<ul>';
foreach ($category->subs as $sub) {
echo '<li><a href="'. base_url() . $sub->full_catalogue .'">' . $sub->name . '</a></li>';
}
echo '</ul>';
}
?>
Я не знаю, как изменить это дерево вместе, когда я перемещаюсь по разным категориям, так что категория, в которой я сейчас нахожусь, выбрана.
И база данных ...
CREATE TABLE `categories` (
`id` int(11) NOT NULL,
`name` tinytext CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
`catalogue` tinytext CHARACTER SET utf8 COLLATE utf8_polish_ci,
`full_catalogue` varchar(140) CHARACTER SET utf8 COLLATE utf8_polish_ci DEFAULT NULL,
`on_homepage` int(2) NOT NULL DEFAULT '0',
`lvl` tinyint(4) NOT NULL DEFAULT '0',
`parent_id` smallint(6) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin2;
INSERT INTO `categories` (`id`, `name`, `catalogue`, `full_catalogue`, `on_homepage`, `lvl`, `parent_id`) VALUES
(1, 'Praca', 'praca', 'praca', 1, 1, 0),
(2, 'Motoryzacja', 'motoryzacja', 'motoryzacja', 2, 1, 0),
(4, 'Nieruchomości', 'nieruchomosci', 'nieruchomosci', 3, 1, 0),
(5, 'Dam pracę', 'dam-prace', 'praca/dam-prace', 1, 2, 1),
(8, 'Imprezy i wydarzenia', 'imprezy', 'imprezy', 12, 1, 0),
(9, 'Usługi', 'uslugi', 'uslugi', 13, 1, 0),
(16, 'Turystyka', 'turystyka', 'turystyka', 4, 1, 0);
CREATE TABLE `ads` (
`id` int(11) NOT NULL,
`category_id` int(11) NOT NULL DEFAULT '0',
`city_id` smallint(6) NOT NULL DEFAULT '0',
`user_id` int(11) NOT NULL,
`off_id` smallint(6) DEFAULT '0',
`title` text CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
`description` longtext CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
`price` bigint(9) DEFAULT '0',
`contact` tinytext CHARACTER SET utf8 COLLATE utf8_polish_ci,
`email` tinytext CHARACTER SET utf8 COLLATE utf8_polish_ci,
`phone` tinytext CHARACTER SET utf8 COLLATE utf8_polish_ci,
`photo1` tinytext CHARACTER SET utf8 COLLATE utf8_polish_ci,
`photo2` tinytext CHARACTER SET utf8 COLLATE utf8_polish_ci,
`photo3` tinytext CHARACTER SET utf8 COLLATE utf8_polish_ci,
`user_ip` varchar(16) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
`promoted` int(1) NOT NULL DEFAULT '0',
`promoted_end_date` int(11) NOT NULL DEFAULT '0',
`active` tinyint(4) DEFAULT '0',
`activate_code` varchar(24) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
`views` int(11) NOT NULL DEFAULT '0',
`created` int(11) NOT NULL,
`updated` int(11) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin2;