ExecuteS(' SELECT `id_category` FROM `'._DB_PREFIX_.'category` WHERE `active` = 1 ') as $c) { $categories[] = $c['id_category']; } foreach(Db::getInstance()->ExecuteS(' SELECT `id_discount` FROM `'._DB_PREFIX_.'discount` WHERE `date_from` <= NOW() AND `date_to` > NOW() AND `quantity` > 0 AND `active` = 1 ') as $voucher) { $voucher_categories = array(); foreach(Db::getInstance()->ExecuteS(' SELECT `id_category` FROM `'._DB_PREFIX_.'discount_category` WHERE `id_discount` = '.(int) $voucher['id_discount'].' ') as $vc) { $voucher_categories[] = $vc['id_category']; } $diff = array_diff($categories, $voucher_categories); $query = array(); foreach($diff as $d) { $query[] = '('.$d.', '.(int) $voucher['id_discount'].')'; } Db::getInstance()->Execute(' INSERT INTO `'._DB_PREFIX_.'discount_category` VALUES '.implode(', ', $query).' '); }