Genger
Мастер
- Регистрация
- 23 Фев 2009
- Сообщения
- 225
- Реакции
- 32
Для просмотра ссылки Войди или Зарегистрируйся, лишь на тебя надежда
Follow along with the video below to see how to install our site as a web app on your home screen.
Примечание: This feature may not be available in some browsers.
ТриСколько уровней вложенности в каталоге?
/**
* Функция возвращает количество товаров
* Возможные значения фильтра:
* category_id - id категории или их массив
* brand_id - id бренда или их массив
* keyword - ключевое слово для поиска
* features - фильтр по свойствам товара, массив (id свойства => значение свойства)
*/
public function count_products($filter = array())
{
$category_id_filter = '';
$brand_id_filter = '';
$keyword_filter = '';
$visible_filter = '';
$is_featured_filter = '';
$discounted_filter = '';
$features_filter = '';
if(!empty($filter['category_id']))
$category_id_filter = $this->db->placehold('INNER JOIN __products_categories pc ON pc.product_id = p.id AND pc.category_id in(?@)', (array)$filter['category_id']);
if(!empty($filter['brand_id']))
$brand_id_filter = $this->db->placehold('AND p.brand_id in(?@)', (array)$filter['brand_id']);
if(isset($filter['keyword']))
{
$keywords = explode(' ', $filter['keyword']);
foreach($keywords as $keyword)
$keyword_filter .= $this->db->placehold('AND p.name LIKE "%'.mysql_real_escape_string(trim($keyword)).'%" ');
}
if(!empty($filter['featured']))
$is_featured_filter = $this->db->placehold('AND p.featured=?', intval($filter['featured']));
if(!empty($filter['discounted']))
$discounted_filter = $this->db->placehold('AND (SELECT 1 FROM __variants pv WHERE pv.product_id=p.id AND pv.compare_price>0 LIMIT 1) = ?', intval($filter['discounted']));
if(!empty($filter['visible']))
$visible_filter = $this->db->placehold('AND p.visible=?', intval($filter['visible']));
if(!empty($filter['features']) && !empty($filter['features']))
foreach($filter['features'] as $feature=>$value)
$features_filter .= $this->db->placehold('AND p.id in (SELECT product_id FROM __options WHERE feature_id=? AND value=? ) ', $feature, $value);
$query = "SELECT count(distinct p.id) as count
FROM __products AS p
$category_id_filter
WHERE 1
$brand_id_filter
$keyword_filter
$is_featured_filter
$discounted_filter
$visible_filter
$features_filter ";
$this->db->query($query);
return $this->db->result('count');
}
SELECT t1.category_id, (COUNT(t1.product_id)+COUNT(t2.product_id)+COUNT(t3.product_id)) AS goods_count
FROM __products_categories AS t1
LEFT JOIN __products_categories AS t2 ON t2.parent = t1.category_id
LEFT JOIN __products_categories AS t3 ON t3.parent = t2.category_id
GROUP BY t1.category_id, t2.category_id, t3.category_id