📄 functions_categories.php
字号:
<?php
/**
* functions_categories.php
*
* @package functions
* @copyright Copyright 2003-2005 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
* @version $Id: functions_categories.php 3118 2006-03-05 22:36:32Z ajeh $
*/
////
// Generate a path to categories
function zen_get_path($current_category_id = '') {
global $cPath_array, $db;
if (zen_not_null($current_category_id)) {
$cp_size = sizeof($cPath_array);
if ($cp_size == 0) {
$cPath_new = $current_category_id;
} else {
$cPath_new = '';
$last_category_query = "select parent_id
from " . TABLE_CATEGORIES . "
where categories_id = '" . (int)$cPath_array[($cp_size-1)] . "'";
$last_category = $db->Execute($last_category_query);
$current_category_query = "select parent_id
from " . TABLE_CATEGORIES . "
where categories_id = '" . (int)$current_category_id . "'";
$current_category = $db->Execute($current_category_query);
if ($last_category->fields['parent_id'] == $current_category->fields['parent_id']) {
for ($i=0; $i<($cp_size-1); $i++) {
$cPath_new .= '_' . $cPath_array[$i];
}
} else {
for ($i=0; $i<$cp_size; $i++) {
$cPath_new .= '_' . $cPath_array[$i];
}
}
$cPath_new .= '_' . $current_category_id;
if (substr($cPath_new, 0, 1) == '_') {
$cPath_new = substr($cPath_new, 1);
}
}
} else {
$cPath_new = implode('_', $cPath_array);
}
return 'cPath=' . $cPath_new;
}
////
// Return the number of products in a category
// TABLES: products, products_to_categories, categories
function zen_count_products_in_category($category_id, $include_inactive = false) {
global $db;
$products_count = 0;
if ($include_inactive == true) {
$products_query = "select count(*) as total
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
where p.products_id = p2c.products_id
and p2c.categories_id = '" . (int)$category_id . "'";
} else {
$products_query = "select count(*) as total
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
where p.products_id = p2c.products_id
and p.products_status = '1'
and p2c.categories_id = '" . (int)$category_id . "'";
}
$products = $db->Execute($products_query);
$products_count += $products->fields['total'];
$child_categories_query = "select categories_id
from " . TABLE_CATEGORIES . "
where parent_id = '" . (int)$category_id . "'";
$child_categories = $db->Execute($child_categories_query);
if ($child_categories->RecordCount() > 0) {
while (!$child_categories->EOF) {
$products_count += zen_count_products_in_category($child_categories->fields['categories_id'], $include_inactive);
$child_categories->MoveNext();
}
}
return $products_count;
}
////
// Return true if the category has subcategories
// TABLES: categories
function zen_has_category_subcategories($category_id) {
global $db;
$child_category_query = "select count(*) as count
from " . TABLE_CATEGORIES . "
where parent_id = '" . (int)$category_id . "'";
$child_category = $db->Execute($child_category_query);
if ($child_category->fields['count'] > 0) {
return true;
} else {
return false;
}
}
////
function zen_get_categories($categories_array = '', $parent_id = '0', $indent = '', $status_setting = '') {
global $db;
if (!is_array($categories_array)) $categories_array = array();
// show based on status
if ($status_setting != '') {
$zc_status = " c.categories_status='" . $status_setting . "' and ";
} else {
$zc_status = '';
}
$categories_query = "select c.categories_id, cd.categories_name, c.categories_status
from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
where " . $zc_status . "
parent_id = '" . (int)$parent_id . "'
and c.categories_id = cd.categories_id
and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
order by sort_order, cd.categories_name";
$categories = $db->Execute($categories_query);
while (!$categories->EOF) {
$categories_array[] = array('id' => $categories->fields['categories_id'],
'text' => $indent . $categories->fields['categories_name']);
if ($categories->fields['categories_id'] != $parent_id) {
$categories_array = zen_get_categories($categories_array, $categories->fields['categories_id'], $indent . ' ', '1');
}
$categories->MoveNext();
}
return $categories_array;
}
////
// Return all subcategory IDs
// TABLES: categories
function zen_get_subcategories(&$subcategories_array, $parent_id = 0) {
global $db;
$subcategories_query = "select categories_id
from " . TABLE_CATEGORIES . "
where parent_id = '" . (int)$parent_id . "'";
$subcategories = $db->Execute($subcategories_query);
while (!$subcategories->EOF) {
$subcategories_array[sizeof($subcategories_array)] = $subcategories->fields['categories_id'];
if ($subcategories->fields['categories_id'] != $parent_id) {
zen_get_subcategories($subcategories_array, $subcategories->fields['categories_id']);
}
$subcategories->MoveNext();
}
}
////
// Recursively go through the categories and retreive all parent categories IDs
// TABLES: categories
function zen_get_parent_categories(&$categories, $categories_id) {
global $db;
$parent_categories_query = "select parent_id
from " . TABLE_CATEGORIES . "
where categories_id = '" . (int)$categories_id . "'";
$parent_categories = $db->Execute($parent_categories_query);
while (!$parent_categories->EOF) {
if ($parent_categories->fields['parent_id'] == 0) return true;
$categories[sizeof($categories)] = $parent_categories->fields['parent_id'];
if ($parent_categories->fields['parent_id'] != $categories_id) {
zen_get_parent_categories($categories, $parent_categories->fields['parent_id']);
}
$parent_categories->MoveNext();
}
}
////
// Construct a category path to the product
// TABLES: products_to_categories
function zen_get_product_path($products_id) {
global $db;
$cPath = '';
$category_query = "select p2c.categories_id
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
where p.products_id = '" . (int)$products_id . "'
and p.products_status = '1'
and p.products_id = p2c.products_id limit 1";
$category = $db->Execute($category_query);
if ($category->RecordCount() > 0) {
$categories = array();
zen_get_parent_categories($categories, $category->fields['categories_id']);
$categories = array_reverse($categories);
$cPath = implode('_', $categories);
if (zen_not_null($cPath)) $cPath .= '_';
$cPath .= $category->fields['categories_id'];
}
return $cPath;
}
////
// Parse and secure the cPath parameter values
function zen_parse_category_path($cPath) {
// make sure the category IDs are integers
$cPath_array = array_map('zen_string_to_int', explode('_', $cPath));
// make sure no duplicate category IDs exist which could lock the server in a loop
$tmp_array = array();
$n = sizeof($cPath_array);
for ($i=0; $i<$n; $i++) {
if (!in_array($cPath_array[$i], $tmp_array)) {
$tmp_array[] = $cPath_array[$i];
}
}
return $tmp_array;
}
function zen_product_in_category($product_id, $cat_id) {
global $db;
$in_cat=false;
$category_query_raw = "select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . "
where products_id = '" . (int)$product_id . "'";
$category = $db->Execute($category_query_raw);
while (!$category->EOF) {
if ($category->fields['categories_id'] == $cat_id) $in_cat = true;
if (!$in_cat) {
$parent_categories_query = "select parent_id from " . TABLE_CATEGORIES . "
where categories_id = '" . $category->fields['categories_id'] . "'";
$parent_categories = $db->Execute($parent_categories_query);
//echo 'cat='.$category->fields['categories_id'].'#'. $cat_id;
while (!$parent_categories->EOF) {
if (($parent_categories->fields['parent_id'] !=0) ) {
if (!$in_cat) $in_cat = zen_product_in_parent_category($product_id, $cat_id, $parent_categories->fields['parent_id']);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -