Входные данные $ids= 1,3,3,8,5,8
public function updateCounts ($ids) {
$ids = explode(",", $ids);
$products = $this->getAllOnIDs($ids);
$result = array();
for ($i = 0; $i < count($products); $i++) {
$result[$products[$i]["id"]] = $products[$i];
}
$ids_unique = array_unique($ids);
$i = 0;
foreach ($ids_unique as $v) {
$cart[$i]["id"] = $result[$v]["id"];
$cart[$i]["count"] = $this->getCountInArr($v, $ids);
$i++;
}
$count = array();
$new_count = array();
for ($i = 0; $i < count($cart); $i++){
$count[$i] = $this->getAllOnField("id", $cart[$i]["id"]);
$new_count[$i][$cart[$i]["id"]] = $count[$i][0]["count"] - $cart[$i]["count"];
if ($new_count[$i][$cart[$i]["id"]] < 0){
exit;
}
}
for ($j = 0; $j < count($new_count); $j++)
foreach ($new_count[$j] as $id => $count) {
$this->upNewCount($count, $id);
}
return true;
}
public function getAllOnIDs ($ids) {
$query_ids = "";
$params =array();
for ($i = 0; $i < count($ids); $i++) {
$query_ids .= $this->config->sym_query.",";
$params[] = $ids[$i];
}
$query_ids = substr($query_ids, 0, -1);
$query = "SELECT * FROM `".$this->table_name."` WHERE `id` IN ($query_ids)";
return $this->transform($this->db->select($query, $params));
}
protected function transform($element){
if (!$element) return false;
if (isset($element[0])) {
for ($i = 0; $i < count($element); $i++)
$element[$i] = $this->transformElement($element[$i]);
return $element;
}
else return $this->transformElement($element);
}
protected function transformElement($products) {
$products["link_cart"] = $this->url->addCart($products["id"]);
$products["link_delete"] = $this->url->deleteCart($products["id"]);
return $products;
}
private function getCountInArr($v, $array) {
$count = 0;
for ($i = 0; $i < count($array); $i++) {
if ($array[$i] == $v) $count++;
}
return $count;
}
public function select($query, $params = false) {
$result_set = $this->mysqli->query($this->getQuery($query, $params));
if (!$result_set) return false;
return $this->resultSetToArray($result_set);
private function getQuery($query, $params) {
if ($params) {
for ($i = 0; $i < count($params); $i++) {
$pos = strpos($query, $this->config->sym_query);
$arg = "'".$this->mysqli->real_escape_string($params[$i])."'";
$query = substr_replace($query, $arg, $pos, strlen($this->config->sym_query));
}
}
return $query;
}
private function resultSetToArray($result_set) {
$array = array();
while (($row = $result_set->fetch_assoc()) != false) {
$array[] = $row;
}
return $array;
}
Проблема в том, что в $new_count не передавалось переменные, точнее передавалась, только число $cart[$i]["count"] со знаком минус. Когда вывел через print_r увидел, что $count выводитс как трехмерный массив(или не пойму как).
Array ( [0] =>
Array ( [0] =>
Array ( [id] => 2 [prod_title] => Куртка 18-02 [barcode] => w14-02red [description] => [sku] => 14-02 [size_id] => 6 [color_id] => 2 [colorOt_id] => [sleeve_id] => 1 [density_id] => [cloth_id] => 2 [sklad_id] => 3 [cat_id] => 4 [price] => 433 [count] => 2 ) )
[1] =>
Array ( [0] => Array ( [id] => 3 [prod_title] => Куртка 14-04 [barcode] => w14-04white [description] => [sku] => 14-04 [size_id] => 8 [color_id] => 6 [colorOt_id] => [sleeve_id] => 3 [density_id] => [cloth_id] => 3 [sklad_id] => 2 [cat_id] => 4 [price] => 356 [count] => 256 ) ) )
И когда исправить
$count[$i]["count"]
на
$count[$i][0]["count"]
, то все работает, но почему? Может кто-то сталкивался? Могу привести код остальных функций через которые проходит результат.
Остальные функции: