我有一个用于批量更新/添加折扣价格的 php 函数。
代码运行良好,直到我
在选择语句中添加了 p.sku的 sku 上添加了额外的 $new_price 条件和 substr 检查。
它现在给了我一个空白屏幕,当我尝试执行它时查询没有运行。
if ($user_group == 15 && $percent == 0) {
$this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE customer_group_id = '".$user_group."' AND quantity = '".$quantity."'");
$query = $this->db->query("SELECT p.product_id, p.sku, p.cost, p.price FROM product p");
foreach ($query->rows as $result) {
if ((substr($result['sku'],0,3) == 'ACA')) {
$new_price = ($result['cost'] * 32.5 / 100 + $result['cost']);
} else {
$new_price = ($result['cost'] * 40 / 100 + $result['cost']);
}
$this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET
`product_id` = '" . $result['product_id'] . "',
`customer_group_id` = '15',
`quantity` = '".$quantity."',
`priority` = '".$priority."',
`price` = '".$new_price."',
`date_start` = '".$start_date."',
`date_end` = '".$expire_date."'");
}
}
我在这里想念什么?我一直在搜索论坛和谷歌等,整个下午都在尝试各种事情,我受够了!请帮助任何人!
在 index.php 中添加错误报告后,我终于得到了一个线索:
“致命错误:允许的 134217728 字节的内存大小用尽(试图分配 32 字节)”
这是函数的完整代码,根据评论进行了一些细微的更改,我不知道为什么所有内存都用完了,希望有人能提供帮助:
<?php
class ModelBulkDiscount extends Model {
public function updateDiscount($user_group = null, $percent = 0, $quantity = 1, $priority = 0, $start_date = null, $expire_date = null) {
if($user_group != 15 && $percent > 0) {
$this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE customer_group_id = '".$user_group."' AND quantity = '".$quantity."'");
$query = $this->db->query("SELECT p.product_id, p.price FROM " . DB_PREFIX . "product p");
foreach ($query->rows as $result) {
$new_price = $result['price'] - (($result['price'] / 100) * $percent);
$this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET
`product_id` = '" . $result['product_id'] . "',
`customer_group_id` = '" . $user_group . "',
`quantity` = '".$quantity."',
`priority` = '".$priority."',
`price` = '".$new_price."',
`date_start` = '".$start_date."',
`date_end` = '".$expire_date."'");
}
} else if ($user_group == 15 && $percent == 0) {
$this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE customer_group_id = '".$user_group."' AND quantity = '".$quantity."'");
$query = $this->db->query("SELECT p.product_id, p.sku, p.cost, p.price FROM " . DB_PREFIX . "product p");
foreach ($query->rows as $result) {
if ((substr($result['sku'],0,3) == 'ACA')) {
$new_price = ($result['cost'] * 1.325);
} else {
$new_price = ($result['cost'] * 1.4);
}
$this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET
`product_id` = '" . $result['product_id'] . "',
`customer_group_id` = '15',
`quantity` = '".$quantity."',
`priority` = '".$priority."',
`price` = '".$new_price."',
`date_start` = '".$start_date."',
`date_end` = '".$expire_date."'");
}
} else if ($user_group == 15 && $percent > 0) {
$this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE customer_group_id = '".$user_group."' AND quantity = '".$quantity."'");
$query = $this->db->query("SELECT p.product_id, p.cost, p.price FROM " . DB_PREFIX . "product p");
foreach ($query->rows as $result) {
$temp_price = ($result['cost'] * 1.325);
$new_price = $temp_price - (($temp_price / 100) * $percent);
$this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET
`product_id` = '" . $result['product_id'] . "',
`customer_group_id` = '15',
`quantity` = '".$quantity."',
`priority` = '".$priority."',
`price` = '".$new_price."',
`date_start` = '".$start_date."',
`date_end` = '".$expire_date."'");
}
}
}
}
?>