function addProductsAttributes($arr_insert) { $products_id = $arr_insert['products_id'] ? trim($arr_insert['products_id']) : ''; $attr_price = $arr_insert['attr_price'] ? $arr_insert['attr_price'] : array(); $attr_weight = $arr_insert['attr_weight'] ? $arr_insert['attr_weight'] : array(); $attr_sort = $arr_insert['attr_sort'] ? $arr_insert['attr_sort'] : array(); $attr_options_id_arr = $this->options_values_id_arr; $options_values_id_arr_price = $this->options_values_id_arr_price; $options_id = $this->options_id; //echo '$options_id:'.$options_id;exit; /** if(count($attr_options_id_arr) != count($attr_price)){ print("属性值和价格个数不匹配"); echo '<pre>POST值:<br>';print_r($_POST); echo 'values_id_arr:<br>';print_r($attr_options_id_arr); exit; } /**/ //该产品的该属性,如果以及设置过,则删除掉,重新设置 $sql = "delete from products_attributes where products_id=" . $products_id . " and options_id=" . $options_id . " "; $GLOBALS['db']->query($sql); foreach ($attr_options_id_arr as $key => $options_value_id) { if (empty($attr_price[$key])) { $price_str = 0; } else { $price_str = (double) $attr_price[$key]; } //是否设置了 属性价格数组 if (isset($options_values_id_arr_price[$options_value_id])) { $price_str = $options_values_id_arr_price[$options_value_id]; } if (empty($attr_weight[$key])) { $weight_str = 0; } else { $weight_str = (int) $attr_weight[$key]; } //echo '$options_value_id:'.$options_value_id; list($price_prefix, $options_values_price) = splitNumberStr($price_str); list($products_attributes_weight_prefix, $products_attributes_weight) = splitNumberStr($weight_str); $sql = "select 1 from products_attributes\n\t\t\t\twhere products_id=" . $products_id . " and options_id=" . $options_id . "\n\t\t\t\tand options_values_id=" . $options_value_id . " and options_values_price='" . $options_values_price . "' limit 1"; $a = $GLOBALS['db']->getAll($sql); if (!empty($a)) { //echo $sql; continue; } $arr_insert = array('products_id' => $products_id, 'options_id' => $options_id, 'options_values_id' => $options_value_id, 'options_values_price' => $options_values_price, 'price_prefix' => $price_prefix, 'products_options_sort_order' => $attr_sort[$key], 'products_attributes_weight' => $products_attributes_weight, 'products_attributes_weight_prefix' => $products_attributes_weight_prefix); $GLOBALS['db']->autoExecute('products_attributes', $arr_insert); } $msg .= "<br>属性和产品关联成功。"; return $msg; }
function addProductsAttributes($arr_insert) { $products_id = $arr_insert['products_id'] ? trim($arr_insert['products_id']) : ''; $attr_price = $arr_insert['attr_price'] ? $arr_insert['attr_price'] : array(); $attr_weight = $arr_insert['attr_weight'] ? $arr_insert['attr_weight'] : array(); $attr_sort = $arr_insert['attr_sort'] ? $arr_insert['attr_sort'] : array(); $attr_options_id_arr = $this->options_values_id_arr; $options_id = $this->options_id; //echo '$options_id:'.$options_id;exit; if ($attr_options_id_arr && count($attr_options_id_arr) != count($attr_price)) { print "属性值和价格个数不匹配"; echo '<pre>POST值:<br>'; print_r($_POST); echo 'values_id_arr:<br>'; print_r($attr_options_id_arr); exit; } //该产品的该属性,如果以及设置过,则删除掉,重新设置 $sql = "delete from products_attributes where products_id=" . $products_id . " and options_id=" . $options_id . " "; $GLOBALS['db']->query($sql); foreach ($attr_options_id_arr as $key => $options_value_id) { $price_str = (int) $attr_price[$key]; $weight_str = (int) $attr_weight[$key]; list($price_prefix, $options_values_price) = splitNumberStr($price_str); list($products_attributes_weight_prefix, $products_attributes_weight) = splitNumberStr($weight_str); //把options 和 options_values关联起来,供以后使用 $options_select = "select count(*) as cnt \n\t\t\t\t\t\t\t\tfrom products_options_values_to_products_options \n\t\t\t\t\t\t\t\twhere products_options_id=" . $options_id . " and products_options_values_id=" . $options_value_id; $cnt = $GLOBALS['db']->getOne($options_select); if ($cnt == 0) { $arr_options = array('products_options_id' => $options_id, 'products_options_values_id' => $options_value_id); $GLOBALS['db']->autoExecute('products_options_values_to_products_options', $arr_options); } //产品和属性关联起来 $arr_insert = array('products_id' => $products_id, 'options_id' => $options_id, 'options_values_id' => $options_value_id, 'options_values_price' => $options_values_price, 'price_prefix' => $price_prefix, 'products_options_sort_order' => $attr_sort[$key], 'products_attributes_weight' => $products_attributes_weight, 'products_attributes_weight_prefix' => $products_attributes_weight_prefix); $GLOBALS['db']->autoExecute('products_attributes', $arr_insert); } $msg .= "<br>属性和产品关联成功。"; return $msg; }